]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.server.core/src/org/argeo/server/ServerAnswer.java
[maven-release-plugin] prepare for next development iteration
[lgpl/argeo-commons.git] / org.argeo.server.core / src / org / argeo / server / ServerAnswer.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.server;
17
18 import java.io.PrintWriter;
19 import java.io.StringWriter;
20
21 import org.apache.commons.io.IOUtils;
22 import org.argeo.ArgeoException;
23
24 /** Answer to an execution of a remote service which performed changes. */
25 public class ServerAnswer {
26 public final static String OK = "OK";
27 public final static String ERROR = "ERROR";
28
29 private String status = OK;
30 private String message = "";
31
32 // TODO: add an exception field
33
34 /** Canonical constructor */
35 public ServerAnswer(String status, String message) {
36 setStatus(status);
37 if (message == null)
38 throw new ArgeoException("Message cannot be null");
39 this.message = message;
40 }
41
42 /** Empty constructor */
43 public ServerAnswer() {
44 }
45
46 public String getStatus() {
47 return status;
48 }
49
50 public void setStatus(String status) {
51 if (status == null || (!status.equals(OK) && !status.equals(ERROR)))
52 throw new ArgeoException("Bad status format: " + status);
53 this.status = status;
54 }
55
56 public String getMessage() {
57 return message;
58 }
59
60 public void setMessage(String message) {
61 this.message = message;
62 }
63
64 public Boolean isOk() {
65 return status.equals(OK);
66 }
67
68 public Boolean isError() {
69 return status.equals(ERROR);
70 }
71
72 public static ServerAnswer error(String message) {
73 return new ServerAnswer(ERROR, message);
74 }
75
76 public static ServerAnswer error(Throwable e) {
77 StringWriter writer = new StringWriter();
78 try {
79 e.printStackTrace(new PrintWriter(writer));
80 return new ServerAnswer(ERROR, writer.toString());
81 } finally {
82 IOUtils.closeQuietly(writer);
83 }
84 }
85
86 public static ServerAnswer ok(String message) {
87 return new ServerAnswer(OK, message);
88 }
89
90 @Override
91 public String toString() {
92 return "ServerAnswer{status:" + status + ", message:" + message + "}";
93 }
94
95 }