JSON responses implemented and improvements
This commit is contained in:
36
src/com/mesacarlos/webconsole/json/ConsoleOutput.java
Normal file
36
src/com/mesacarlos/webconsole/json/ConsoleOutput.java
Normal file
@ -0,0 +1,36 @@
|
||||
package com.mesacarlos.webconsole.json;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class ConsoleOutput implements JSONOutput{
|
||||
private String message;
|
||||
|
||||
public ConsoleOutput(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getStatusCode() {
|
||||
return 10;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRespondsTo() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJSON() {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("status", getStatusCode());
|
||||
object.addProperty("statusDescription", "Console Output");
|
||||
object.addProperty("message", getMessage());
|
||||
return object.toString();
|
||||
}
|
||||
|
||||
}
|
39
src/com/mesacarlos/webconsole/json/Forbidden.java
Normal file
39
src/com/mesacarlos/webconsole/json/Forbidden.java
Normal file
@ -0,0 +1,39 @@
|
||||
package com.mesacarlos.webconsole.json;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class Forbidden implements JSONOutput{
|
||||
private String message;
|
||||
private String respondsTo;
|
||||
|
||||
public Forbidden(String message, String respondsTo) {
|
||||
this.message = message;
|
||||
this.respondsTo = respondsTo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getStatusCode() {
|
||||
return 403;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRespondsTo() {
|
||||
return respondsTo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJSON() {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("status", getStatusCode());
|
||||
object.addProperty("statusDescription", "Forbidden");
|
||||
object.addProperty("respondsTo", getRespondsTo());
|
||||
object.addProperty("message", getMessage());
|
||||
return object.toString();
|
||||
}
|
||||
|
||||
}
|
28
src/com/mesacarlos/webconsole/json/JSONOutput.java
Normal file
28
src/com/mesacarlos/webconsole/json/JSONOutput.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.mesacarlos.webconsole.json;
|
||||
|
||||
public interface JSONOutput {
|
||||
/**
|
||||
* Gets status code representing this message. See docs for code meanings.
|
||||
* @return Status code representing this message
|
||||
*/
|
||||
int getStatusCode();
|
||||
|
||||
/**
|
||||
* Returns the command sended by client who created this response.
|
||||
* In case of a server-generated response (like ConsoleOutput), this will be null
|
||||
* @return
|
||||
*/
|
||||
String getRespondsTo();
|
||||
|
||||
/**
|
||||
* Explanatory message of this response
|
||||
* @return Explanatory message of this response
|
||||
*/
|
||||
String getMessage();
|
||||
|
||||
/**
|
||||
* Coverts this object into JSON, ready to send it over WS
|
||||
* @return JSON Object Stringified
|
||||
*/
|
||||
String toJSON();
|
||||
}
|
36
src/com/mesacarlos/webconsole/json/LoginRequired.java
Normal file
36
src/com/mesacarlos/webconsole/json/LoginRequired.java
Normal file
@ -0,0 +1,36 @@
|
||||
package com.mesacarlos.webconsole.json;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class LoginRequired implements JSONOutput{
|
||||
private String message;
|
||||
|
||||
public LoginRequired(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getStatusCode() {
|
||||
return 401;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRespondsTo() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJSON() {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("status", getStatusCode());
|
||||
object.addProperty("statusDescription", "Login Required");
|
||||
object.addProperty("message", getMessage());
|
||||
return object.toString();
|
||||
}
|
||||
|
||||
}
|
39
src/com/mesacarlos/webconsole/json/Processed.java
Normal file
39
src/com/mesacarlos/webconsole/json/Processed.java
Normal file
@ -0,0 +1,39 @@
|
||||
package com.mesacarlos.webconsole.json;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class Processed implements JSONOutput{
|
||||
private String message;
|
||||
private String respondsTo;
|
||||
|
||||
public Processed(String message, String respondsTo) {
|
||||
this.message = message;
|
||||
this.respondsTo = respondsTo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getStatusCode() {
|
||||
return 200;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRespondsTo() {
|
||||
return respondsTo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJSON() {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("status", getStatusCode());
|
||||
object.addProperty("statusDescription", "Processed");
|
||||
object.addProperty("respondsTo", getRespondsTo());
|
||||
object.addProperty("message", getMessage());
|
||||
return object.toString();
|
||||
}
|
||||
|
||||
}
|
38
src/com/mesacarlos/webconsole/json/UnknownWSCmd.java
Normal file
38
src/com/mesacarlos/webconsole/json/UnknownWSCmd.java
Normal file
@ -0,0 +1,38 @@
|
||||
package com.mesacarlos.webconsole.json;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class UnknownWSCmd implements JSONOutput{
|
||||
private String message;
|
||||
private String respondsTo;
|
||||
|
||||
public UnknownWSCmd(String message, String respondsTo) {
|
||||
this.message = message;
|
||||
this.respondsTo = respondsTo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getStatusCode() {
|
||||
return 400;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRespondsTo() {
|
||||
return respondsTo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJSON() {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("status", getStatusCode());
|
||||
object.addProperty("statusDescription", "Unknown Command");
|
||||
object.addProperty("respondsTo", getRespondsTo());
|
||||
object.addProperty("message", getMessage());
|
||||
return object.toString();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user