Skip to content

Commit

Permalink
Fixed issue #4524
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca committed Jul 8, 2015
1 parent beac510 commit b53a252
Showing 1 changed file with 34 additions and 34 deletions.
Expand Up @@ -193,7 +193,7 @@ public void service() throws ONetworkProtocolException, IOException {
"->" + channel.socket.getInetAddress().getHostAddress() + ": Command not found: " + request.httpMethod + "." "->" + channel.socket.getInetAddress().getHostAddress() + ": Command not found: " + request.httpMethod + "."
+ URLDecoder.decode(command, "UTF-8")); + URLDecoder.decode(command, "UTF-8"));


sendTextContent(OHttpUtils.STATUS_INVALIDMETHOD_CODE, OHttpUtils.STATUS_INVALIDMETHOD_DESCRIPTION, null, sendError(OHttpUtils.STATUS_INVALIDMETHOD_CODE, OHttpUtils.STATUS_INVALIDMETHOD_DESCRIPTION, null,
OHttpUtils.CONTENT_TEXT_PLAIN, "Command not found: " + command, request.keepAlive); OHttpUtils.CONTENT_TEXT_PLAIN, "Command not found: " + command, request.keepAlive);
} catch (IOException e1) { } catch (IOException e1) {
sendShutdown(); sendShutdown();
Expand Down Expand Up @@ -337,20 +337,45 @@ protected void handleError(Throwable e) {
} }


try { try {
sendTextContent(errorCode, errorReason, responseHeaders, OHttpUtils.CONTENT_TEXT_PLAIN, errorMessage, request.keepAlive); sendError(errorCode, errorReason, responseHeaders, OHttpUtils.CONTENT_TEXT_PLAIN, errorMessage, request.keepAlive);
} catch (IOException e1) { } catch (IOException e1) {
sendShutdown(); sendShutdown();
} }
} }


protected void sendJsonContent(final int iCode, final String iReason, String iHeaders, final String iContentType, protected void sendTextContent(final int iCode, final String iReason, String iHeaders, final String iContentType,
final String iContent, final boolean iKeepAlive) throws IOException { final String iContent, final boolean iKeepAlive) throws IOException {

final boolean empty = iContent == null || iContent.length() == 0; final boolean empty = iContent == null || iContent.length() == 0;


sendStatus(empty && iCode == 200 ? 204 : iCode, iReason); sendStatus(empty && iCode == 200 ? 204 : iCode, iReason);
sendResponseHeaders(iContentType, iKeepAlive); sendResponseHeaders(iContentType, iKeepAlive);


if (iHeaders != null)
writeLine(iHeaders);

final byte[] binaryContent = empty ? null : iContent.getBytes(utf8);

writeLine(OHttpUtils.HEADER_CONTENT_LENGTH + (empty ? 0 : binaryContent.length));

writeLine(null);

if (binaryContent != null)
channel.writeBytes(binaryContent);
channel.flush();
}

protected void sendError(final int iCode, final String iReason, String iHeaders, final String iContentType,
final String iContent, final boolean iKeepAlive) throws IOException {
final byte[] binaryContent;

if (!jsonResponseError) {
sendTextContent(iCode, iReason, iHeaders, iContentType, iContent, iKeepAlive);
return;
}

sendStatus(iCode, iReason);
sendResponseHeaders(OHttpUtils.CONTENT_JSON, iKeepAlive);

if (iHeaders != null) if (iHeaders != null)
writeLine(iHeaders); writeLine(iHeaders);


Expand All @@ -366,41 +391,16 @@ protected void sendJsonContent(final int iCode, final String iReason, String iHe


response.field("errors", errors); response.field("errors", errors);


final byte[] binaryContent = response.toJSON("prettyPrint").getBytes(utf8); binaryContent = response.toJSON("prettyPrint").getBytes(utf8);
writeLine(OHttpUtils.HEADER_CONTENT_LENGTH + (empty ? 0 : binaryContent.length));


writeLine(OHttpUtils.HEADER_CONTENT_LENGTH + (binaryContent != null ? binaryContent.length : 0));
writeLine(null); writeLine(null);


if (binaryContent != null) if (binaryContent != null)
channel.writeBytes(binaryContent); channel.writeBytes(binaryContent);
channel.flush(); channel.flush();
} }


protected void sendTextContent(final int iCode, final String iReason, String iHeaders, final String iContentType,
final String iContent, final boolean iKeepAlive) throws IOException {
final boolean empty = iContent == null || iContent.length() == 0;

if (jsonResponseError) {
sendJsonContent(iCode, iReason, iHeaders, OHttpUtils.CONTENT_JSON, iContent, iKeepAlive);
} else {
sendStatus(empty && iCode == 200 ? 204 : iCode, iReason);
sendResponseHeaders(iContentType, iKeepAlive);

if (iHeaders != null)
writeLine(iHeaders);

final byte[] binaryContent = empty ? null : iContent.getBytes(utf8);

writeLine(OHttpUtils.HEADER_CONTENT_LENGTH + (empty ? 0 : binaryContent.length));

writeLine(null);

if (binaryContent != null)
channel.writeBytes(binaryContent);
channel.flush();
}
}

protected void writeLine(final String iContent) throws IOException { protected void writeLine(final String iContent) throws IOException {
if (iContent != null) if (iContent != null)
channel.outStream.write(iContent.getBytes()); channel.outStream.write(iContent.getBytes());
Expand Down Expand Up @@ -634,12 +634,12 @@ protected void execute() throws Exception {
} catch (Throwable t) { } catch (Throwable t) {
if (request.httpMethod != null && request.url != null) { if (request.httpMethod != null && request.url != null) {
try { try {
sendTextContent(505, "Error on executing of " + request.httpMethod + " for the resource: " + request.url, null, sendError(505, "Error on executing of " + request.httpMethod + " for the resource: " + request.url, null, "text/plain",
"text/plain", t.toString(), request.keepAlive); t.toString(), request.keepAlive);
} catch (IOException e) { } catch (IOException e) {
} }
} else } else
sendTextContent(505, "Error on executing request", null, "text/plain", t.toString(), request.keepAlive); sendError(505, "Error on executing request", null, "text/plain", t.toString(), request.keepAlive);


readAllContent(request); readAllContent(request);
} finally { } finally {
Expand Down

0 comments on commit b53a252

Please sign in to comment.