Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
some request/response changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jmazzitelli committed Jul 13, 2015
1 parent 9ea2083 commit e0353be
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.hawkular.feedcomm.ws.command.BasicResponse;
import org.hawkular.feedcomm.ws.command.Command;
import org.hawkular.feedcomm.ws.command.EchoCommand;
import org.hawkular.feedcomm.ws.command.ErrorResponse;
import org.hawkular.feedcomm.ws.command.ErrorDetails;

@ServerEndpoint("/")
public class FeedCommWebSocket {
Expand Down Expand Up @@ -63,7 +64,7 @@ public String clientMessage(String commandAndJsonStr, Session client) {
Class<? extends Command> commandClass = VALID_COMMANDS.get(commandName);
if (commandClass == null) {
MsgLogger.LOG.errorInvalidCommandName(commandName);
return JsonUtil.toJson(new ErrorResponse("Invalid command name: " + commandName));
return new BasicResponse(new ErrorDetails("Invalid command name: " + commandName)).toJson();
}

String response;
Expand All @@ -73,7 +74,7 @@ public String clientMessage(String commandAndJsonStr, Session client) {
response = command.execute(json);
} catch (Throwable t) {
MsgLogger.LOG.errorCommandExecutionFailure(commandName, json, t);
response = JsonUtil.toJson(new ErrorResponse("Failed to execute command [" + commandName + "]", t));
response = new BasicResponse(new ErrorDetails("Command failed[" + commandName + "]", t)).toJson();
}

return response;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.feedcomm.ws.command;

import org.hawkular.feedcomm.ws.JsonUtil;

public class BasicRequest {

/**
* Constructs a basic request.
*/
public BasicRequest() {
}

/**
* Serialize this object into JSON.
*
* @return this object's JSON representation
*/
public String toJson() {
String json = JsonUtil.toJson(this);
return json;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.feedcomm.ws.command;

import org.hawkular.feedcomm.ws.JsonUtil;

import com.fasterxml.jackson.annotation.JsonInclude;

public class BasicResponse {
public enum ResultStatus {
OK, ERROR
}

@JsonInclude
private final ResultStatus resultStatus;

@JsonInclude
private final ErrorDetails errorDetails;

/**
* Constructs a basic "OK" response.
*/
public BasicResponse() {
this.resultStatus = ResultStatus.OK;
this.errorDetails = null;

}

/**
* Constructs an error response.
*
* @param errorDetails error details
*/
public BasicResponse(ErrorDetails errorDetails) {
this.resultStatus = ResultStatus.ERROR;
this.errorDetails = errorDetails;
}

public ResultStatus getResultStatus() {
return this.resultStatus;
}

/**
* Provides details on the error that occurred, if one did occur.
* This will be <code>null</code> if {@link #getResultStatus()} is {@link ResultStatus#OK}.
*
* @return if this result was an error, this describes the error that occurred
*/
public ErrorDetails getErrorDetails() {
return this.errorDetails;
}

/**
* Serialize this object into JSON.
*
* @return this object's JSON representation
*/
public String toJson() {
String json = JsonUtil.toJson(this);
return json;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public String execute(String json) {
String reply = String.format("ECHO [%s]", echoMessage.echoMessage);

// return the JSON response
EchoResponse echoResponse = new EchoResponse(reply);
return JsonUtil.toJson(echoResponse);
return new EchoResponse(reply).toJson();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.fasterxml.jackson.annotation.JsonInclude;

public class EchoRequest {
public class EchoRequest extends BasicRequest {
@JsonInclude
public String echoMessage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@

import com.fasterxml.jackson.annotation.JsonInclude;

public class EchoResponse {
public class EchoResponse extends BasicResponse {

@JsonInclude
public String replyMessage;
public String reply;

public EchoResponse() {
super();
}

public EchoResponse(ErrorDetails errorDetails) {
super(errorDetails);
}

public EchoResponse(String replyMessage) {
this.replyMessage = replyMessage;
public EchoResponse(String reply) {
super();
this.reply = reply;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@
import com.fasterxml.jackson.annotation.JsonInclude;

/**
* A generic error response that can be JSON-encoded.
* Contains some details that describe an error that occurred.
*/
public class ErrorResponse {
public class ErrorDetails {

@JsonInclude
private final String message;
private final String errorMessage;

@JsonInclude
private final String exception;

public ErrorResponse(String message) {
this.message = message;
public ErrorDetails(String errorMessage) {
this.errorMessage = errorMessage;
this.exception = null;
}

public ErrorResponse(Throwable throwable) {
this.message = null;
public ErrorDetails(Throwable throwable) {
this.errorMessage = null;
this.exception = throwable.toString();
}

public ErrorResponse(String message, Throwable throwable) {
this.message = message;
public ErrorDetails(String errorMessage, Throwable throwable) {
this.errorMessage = errorMessage;
this.exception = throwable.toString();
}

Expand Down

0 comments on commit e0353be

Please sign in to comment.