View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.model.message;
17  
18  /**
19   * A response message, with a status code and message (OK, NOT FOUND, etc).
20   *
21   * @author Christian Bauer
22   */
23  public class UpnpResponse extends UpnpOperation {
24  
25      public static enum Status {
26  
27          OK(200, "OK"),
28          BAD_REQUEST(400, "Bad Request"),
29          NOT_FOUND(404, "Not Found"),
30          METHOD_NOT_SUPPORTED(405, "Method Not Supported"),
31          PRECONDITION_FAILED(412, "Precondition Failed"),
32          UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
33          INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
34          NOT_IMPLEMENTED(501, "Not Implemented");
35  
36          private int statusCode;
37          private String statusMsg;
38  
39          Status(int statusCode, String statusMsg) {
40              this.statusCode = statusCode;
41              this.statusMsg = statusMsg;
42          }
43  
44          public int getStatusCode() {
45              return statusCode;
46          }
47  
48          public String getStatusMsg() {
49              return statusMsg;
50          }
51  
52          static public Status getByStatusCode(int statusCode) {
53              for (Status status : values()) {
54                  if (status.getStatusCode() == statusCode)
55                      return status;
56              }
57              return null;
58          }
59      }
60  
61      private int statusCode;
62      private String statusMessage;
63  
64      public UpnpResponse(int statusCode, String statusMessage) {
65          this.statusCode = statusCode;
66          this.statusMessage = statusMessage;
67      }
68  
69      public UpnpResponse(Status status) {
70          this.statusCode = status.getStatusCode();
71          this.statusMessage = status.getStatusMsg();
72      }
73  
74      public int getStatusCode() {
75          return statusCode;
76      }
77  
78      public String getStatusMessage() {
79          return statusMessage;
80      }
81  
82      /**
83       * @return <code>true</code> if the status code was equal or creater 300.
84       */
85      public boolean isFailed() {
86          return statusCode >= 300;
87      }
88  
89      /**
90       * @return The concatenated string of status code and status message (same as {@link #toString()}.
91       */
92      public String getResponseDetails() {
93          return getStatusCode() + " " + getStatusMessage();
94      }
95  
96      @Override
97      public String toString() {
98          return getResponseDetails();
99      }
100 }