[OLINGO-659] better debug output in error case
Change-Id: I3f0a270807c4a929027894ed42c51523e56bbc14 Signed-off-by: Michael Bolz <michael.bolz@sap.com>
This commit is contained in:
parent
ccf361b8fe
commit
83a098f22b
|
@ -32,26 +32,25 @@ public interface DebugSupport {
|
|||
public static final String ODATA_DEBUG_DOWNLOAD = "download";
|
||||
|
||||
/**
|
||||
* Initializes the debug support implementation. Is called before {@link #isUserAuthorized()} and
|
||||
* {@link #createDebugResponse(String, DebugInformation)}
|
||||
* Initializes the debug support implementation.
|
||||
* Is called before {@link #isUserAuthorized()} and
|
||||
* {@link #createDebugResponse(String, DebugInformation)}.
|
||||
* @param odata
|
||||
*/
|
||||
void init(OData odata);
|
||||
|
||||
/**
|
||||
* This method is called to make sure that the user that requested the debug output is authorized to see this output.
|
||||
* Ensures that the user that requested the debug output is authorized to see this output.
|
||||
* @return true if the current user is authorized
|
||||
*/
|
||||
boolean isUserAuthorized();
|
||||
|
||||
/**
|
||||
* This method should create a debug response and deliver it back to the Olingo library. This method MUST NEVER throw
|
||||
* an exception.
|
||||
* @param debugFormat which is requested via the odata-debug query parameter
|
||||
* @param request object which was send to the server
|
||||
* @param response object which was filled by the application
|
||||
* @param exception which has been thrown. Might be null in case there was no exception
|
||||
* @return a new debug response which will be send to the client
|
||||
* Creates a debug response and delivers it back to the Olingo library.
|
||||
* This method MUST NEVER throw an exception.
|
||||
* @param debugFormat the value of the odata-debug query parameter
|
||||
* @param debugInfo all necessary information to construct debug output
|
||||
* @return a new debug response which will be sent to the client
|
||||
*/
|
||||
ODataResponse createDebugResponse(String debugFormat, DebugInformation debugInfo);
|
||||
|
||||
|
|
|
@ -74,7 +74,7 @@ public class ODataHandler {
|
|||
|
||||
public ODataResponse process(final ODataRequest request) {
|
||||
ODataResponse response = new ODataResponse();
|
||||
int measurementHandel = debugger.startRuntimeMeasurement("ODataHandler", "processInternal");
|
||||
final int responseHandle = debugger.startRuntimeMeasurement("ODataHandler", "process");
|
||||
try {
|
||||
processInternal(request, response);
|
||||
} catch (final UriValidationException e) {
|
||||
|
@ -111,29 +111,51 @@ public class ODataHandler {
|
|||
ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e);
|
||||
handleException(request, response, serverError, e);
|
||||
}
|
||||
debugger.stopRuntimeMeasurement(measurementHandel);
|
||||
debugger.stopRuntimeMeasurement(responseHandle);
|
||||
return response;
|
||||
}
|
||||
|
||||
private void processInternal(final ODataRequest request, final ODataResponse response)
|
||||
throws ODataApplicationException, ODataLibraryException {
|
||||
final int measurementHandle = debugger.startRuntimeMeasurement("ODataHandler", "processInternal");
|
||||
|
||||
response.setHeader(HttpHeader.ODATA_VERSION, ODataServiceVersion.V40.toString());
|
||||
try {
|
||||
validateODataVersion(request);
|
||||
} catch (final ODataHandlerException e) {
|
||||
debugger.stopRuntimeMeasurement(measurementHandle);
|
||||
throw e;
|
||||
}
|
||||
|
||||
validateODataVersion(request);
|
||||
|
||||
int measurementUriParser = debugger.startRuntimeMeasurement("UriParser", "parseUri");
|
||||
uriInfo = new Parser().parseUri(request.getRawODataPath(), request.getRawQueryPath(), null,
|
||||
serviceMetadata.getEdm());
|
||||
final int measurementUriParser = debugger.startRuntimeMeasurement("UriParser", "parseUri");
|
||||
try {
|
||||
uriInfo = new Parser().parseUri(request.getRawODataPath(), request.getRawQueryPath(), null,
|
||||
serviceMetadata.getEdm());
|
||||
} catch (final ODataLibraryException e) {
|
||||
debugger.stopRuntimeMeasurement(measurementUriParser);
|
||||
debugger.stopRuntimeMeasurement(measurementHandle);
|
||||
throw e;
|
||||
}
|
||||
debugger.stopRuntimeMeasurement(measurementUriParser);
|
||||
|
||||
int measurementUriValidator = debugger.startRuntimeMeasurement("UriValidator", "validate");
|
||||
final int measurementUriValidator = debugger.startRuntimeMeasurement("UriValidator", "validate");
|
||||
final HttpMethod method = request.getMethod();
|
||||
new UriValidator().validate(uriInfo, method);
|
||||
try {
|
||||
new UriValidator().validate(uriInfo, method);
|
||||
} catch (final UriValidationException e) {
|
||||
debugger.stopRuntimeMeasurement(measurementUriValidator);
|
||||
debugger.stopRuntimeMeasurement(measurementHandle);
|
||||
throw e;
|
||||
}
|
||||
debugger.stopRuntimeMeasurement(measurementUriValidator);
|
||||
|
||||
int measurementDispatcher = debugger.startRuntimeMeasurement("Dispatcher", "dispatch");
|
||||
new ODataDispatcher(uriInfo, this).dispatch(request, response);
|
||||
debugger.stopRuntimeMeasurement(measurementDispatcher);
|
||||
final int measurementDispatcher = debugger.startRuntimeMeasurement("ODataDispatcher", "dispatch");
|
||||
try {
|
||||
new ODataDispatcher(uriInfo, this).dispatch(request, response);
|
||||
} finally {
|
||||
debugger.stopRuntimeMeasurement(measurementDispatcher);
|
||||
debugger.stopRuntimeMeasurement(measurementHandle);
|
||||
}
|
||||
}
|
||||
|
||||
public void handleException(final ODataRequest request, final ODataResponse response,
|
||||
|
@ -154,13 +176,12 @@ public class ODataHandler {
|
|||
} catch (final ContentNegotiatorException e) {
|
||||
requestedContentType = ContentType.JSON;
|
||||
}
|
||||
int measurementHandle = debugger.startRuntimeMeasurement("ErrorProcessor", "processError");
|
||||
final int measurementHandle = debugger.startRuntimeMeasurement("ErrorProcessor", "processError");
|
||||
exceptionProcessor.processError(request, response, serverError, requestedContentType);
|
||||
debugger.stopRuntimeMeasurement(measurementHandle);
|
||||
}
|
||||
|
||||
private void validateODataVersion(final ODataRequest request)
|
||||
throws ODataHandlerException {
|
||||
private void validateODataVersion(final ODataRequest request) throws ODataHandlerException {
|
||||
final String maxVersion = request.getHeader(HttpHeader.ODATA_MAX_VERSION);
|
||||
if (maxVersion != null && ODataServiceVersion.isBiggerThan(ODataServiceVersion.V40.toString(), maxVersion)) {
|
||||
throw new ODataHandlerException("ODataVersion not supported: " + maxVersion,
|
||||
|
|
|
@ -45,10 +45,10 @@ import org.apache.olingo.server.api.ODataResponse;
|
|||
import org.apache.olingo.server.api.ODataServerError;
|
||||
import org.apache.olingo.server.api.ServiceMetadata;
|
||||
import org.apache.olingo.server.api.debug.DebugSupport;
|
||||
import org.apache.olingo.server.api.deserializer.DeserializerException;
|
||||
import org.apache.olingo.server.api.etag.CustomETagSupport;
|
||||
import org.apache.olingo.server.api.processor.Processor;
|
||||
import org.apache.olingo.server.api.serializer.CustomContentTypeSupport;
|
||||
import org.apache.olingo.server.api.serializer.SerializerException;
|
||||
import org.apache.olingo.server.core.debug.ServerCoreDebugger;
|
||||
|
||||
public class ODataHttpHandlerImpl implements ODataHttpHandler {
|
||||
|
@ -72,21 +72,17 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
|
|||
ODataResponse odResponse;
|
||||
debugger.resolveDebugMode(request);
|
||||
|
||||
int processMethodHandel = debugger.startRuntimeMeasurement("ODataHttpHandlerImpl", "process");
|
||||
final int processMethodHandle = debugger.startRuntimeMeasurement("ODataHttpHandlerImpl", "process");
|
||||
try {
|
||||
int requestHandel = debugger.startRuntimeMeasurement("ODataHttpHandlerImpl", "fillODataRequest");
|
||||
fillODataRequest(odRequest, request, split);
|
||||
debugger.stopRuntimeMeasurement(requestHandel);
|
||||
|
||||
int responseHandel = debugger.startRuntimeMeasurement("ODataHandler", "process");
|
||||
odResponse = handler.process(odRequest);
|
||||
debugger.stopRuntimeMeasurement(responseHandel);
|
||||
// ALL future methods after process must not throw exceptions!
|
||||
} catch (Exception e) {
|
||||
exception = e;
|
||||
odResponse = handleException(odRequest, e);
|
||||
}
|
||||
debugger.stopRuntimeMeasurement(processMethodHandel);
|
||||
debugger.stopRuntimeMeasurement(processMethodHandle);
|
||||
|
||||
if (debugger.isDebugMode()) {
|
||||
Map<String, String> serverEnvironmentVariables = createEnvironmentVariablesMap(request);
|
||||
|
@ -95,7 +91,7 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
|
|||
exception = handler.getLastThrownException();
|
||||
}
|
||||
odResponse =
|
||||
debugger.createDebugResponse(request, exception, odRequest, odResponse, handler.getUriInfo(),
|
||||
debugger.createDebugResponse(odRequest, odResponse, exception, handler.getUriInfo(),
|
||||
serverEnvironmentVariables);
|
||||
}
|
||||
|
||||
|
@ -103,7 +99,7 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
|
|||
}
|
||||
|
||||
private Map<String, String> createEnvironmentVariablesMap(HttpServletRequest request) {
|
||||
LinkedHashMap<String, String> environment = new LinkedHashMap<String, String>();
|
||||
Map<String, String> environment = new LinkedHashMap<String, String>();
|
||||
environment.put("authType", request.getAuthType());
|
||||
environment.put("localAddr", request.getLocalAddr());
|
||||
environment.put("localName", request.getLocalName());
|
||||
|
@ -186,9 +182,9 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
|
|||
}
|
||||
}
|
||||
|
||||
private ODataRequest fillODataRequest(final ODataRequest odRequest, final HttpServletRequest httpRequest,
|
||||
final int split)
|
||||
throws ODataLibraryException {
|
||||
private ODataRequest fillODataRequest(ODataRequest odRequest, final HttpServletRequest httpRequest,
|
||||
final int split) throws ODataLibraryException {
|
||||
final int requestHandle = debugger.startRuntimeMeasurement("ODataHttpHandlerImpl", "fillODataRequest");
|
||||
try {
|
||||
odRequest.setBody(httpRequest.getInputStream());
|
||||
odRequest.setProtocol(httpRequest.getProtocol());
|
||||
|
@ -198,13 +194,14 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
|
|||
|
||||
return odRequest;
|
||||
} catch (final IOException e) {
|
||||
throw new SerializerException("An I/O exception occurred.", e,
|
||||
SerializerException.MessageKeys.IO_EXCEPTION);
|
||||
throw new DeserializerException("An I/O exception occurred.", e,
|
||||
DeserializerException.MessageKeys.IO_EXCEPTION);
|
||||
} finally {
|
||||
debugger.stopRuntimeMeasurement(requestHandle);
|
||||
}
|
||||
}
|
||||
|
||||
static HttpMethod extractMethod(final HttpServletRequest httpRequest)
|
||||
throws ODataLibraryException {
|
||||
static HttpMethod extractMethod(final HttpServletRequest httpRequest) throws ODataLibraryException {
|
||||
try {
|
||||
HttpMethod httpRequestMethod = HttpMethod.valueOf(httpRequest.getMethod());
|
||||
|
||||
|
|
|
@ -65,7 +65,7 @@ public class DebugResponseHelperImpl implements DebugResponseHelper {
|
|||
}
|
||||
|
||||
@Override
|
||||
public ODataResponse createDebugResponse(DebugInformation debugInfo) {
|
||||
public ODataResponse createDebugResponse(final DebugInformation debugInfo) {
|
||||
try {
|
||||
final List<DebugTab> parts = createParts(debugInfo);
|
||||
|
||||
|
@ -100,7 +100,7 @@ public class DebugResponseHelperImpl implements DebugResponseHelper {
|
|||
}
|
||||
}
|
||||
|
||||
private List<DebugTab> createParts(DebugInformation debugInfo) {
|
||||
private List<DebugTab> createParts(final DebugInformation debugInfo) {
|
||||
List<DebugTab> parts = new ArrayList<DebugTab>();
|
||||
|
||||
// request
|
||||
|
@ -182,20 +182,18 @@ public class DebugResponseHelperImpl implements DebugResponseHelper {
|
|||
}
|
||||
|
||||
/**
|
||||
* Get version field information for response.
|
||||
* Result is never null.
|
||||
*
|
||||
* Gets version field information for response. Result is never null.
|
||||
* @return version field information
|
||||
*/
|
||||
private String getVersion() {
|
||||
final String version = DebugResponseHelperImpl.class.getPackage().getImplementationVersion();
|
||||
if (version == null) {
|
||||
return "Olingo";
|
||||
}
|
||||
return "Olingo " + version;
|
||||
protected static String getVersion() {
|
||||
final Package pack = DebugResponseHelperImpl.class.getPackage();
|
||||
final String name = pack.getImplementationTitle();
|
||||
final String version = pack.getImplementationVersion();
|
||||
return (name == null ? "Olingo" : name)
|
||||
+ (version == null ? "" : " Version " + version);
|
||||
}
|
||||
|
||||
private InputStream wrapInHtml(final List<DebugTab> parts, String title) throws IOException {
|
||||
private InputStream wrapInHtml(final List<DebugTab> parts, final String title) throws IOException {
|
||||
StringWriter writer = new StringWriter();
|
||||
|
||||
writer.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n")
|
||||
|
@ -265,33 +263,39 @@ public class DebugResponseHelperImpl implements DebugResponseHelper {
|
|||
return value == null ? null : value.replace("&", "&").replace("<", "<").replace(">", ">");
|
||||
}
|
||||
|
||||
protected static void appendJsonTable(final JsonGenerator gen, final Map<String, String> entries)
|
||||
protected static void appendJsonTable(JsonGenerator gen, final Map<String, String> entries)
|
||||
throws IOException {
|
||||
gen.writeStartObject();
|
||||
|
||||
for (Map.Entry<String, String> entry : entries.entrySet()) {
|
||||
if (entry.getValue() != null) {
|
||||
gen.writeStringField(entry.getKey(), entry.getValue());
|
||||
} else {
|
||||
gen.writeNullField(entry.getKey());
|
||||
if (entries == null || entries.isEmpty()) {
|
||||
gen.writeNull();
|
||||
} else {
|
||||
gen.writeStartObject();
|
||||
for (Map.Entry<String, String> entry : entries.entrySet()) {
|
||||
gen.writeFieldName(entry.getKey());
|
||||
if (entry.getValue() == null) {
|
||||
gen.writeNull();
|
||||
} else {
|
||||
gen.writeString(entry.getValue());
|
||||
}
|
||||
}
|
||||
gen.writeEndObject();
|
||||
}
|
||||
gen.writeEndObject();
|
||||
}
|
||||
|
||||
protected static void appendHtmlTable(final Writer writer, final Map<String, String> entries) throws IOException {
|
||||
protected static void appendHtmlTable(Writer writer, final Map<String, String> entries) throws IOException {
|
||||
writer.append("<table>\n<thead>\n")
|
||||
.append("<tr><th class=\"name\">Name</th><th class=\"value\">Value</th></tr>\n")
|
||||
.append("</thead>\n<tbody>\n");
|
||||
for (final Entry<String, String> entry : entries.entrySet()) {
|
||||
writer.append("<tr><td class=\"name\">").append(entry.getKey()).append("</td>")
|
||||
.append("<td class=\"value\">");
|
||||
if (entry.getValue() != null) {
|
||||
writer.append(escapeHtml(entry.getValue()));
|
||||
} else {
|
||||
writer.append("null");
|
||||
if (entries != null && !entries.isEmpty()) {
|
||||
for (final Entry<String, String> entry : entries.entrySet()) {
|
||||
writer.append("<tr><td class=\"name\">").append(entry.getKey()).append("</td>")
|
||||
.append("<td class=\"value\">");
|
||||
if (entry.getValue() != null) {
|
||||
writer.append(escapeHtml(entry.getValue()));
|
||||
} else {
|
||||
writer.append("null");
|
||||
}
|
||||
writer.append("</td></tr>\n");
|
||||
}
|
||||
writer.append("</td></tr>\n");
|
||||
}
|
||||
writer.append("</tbody>\n</table>\n");
|
||||
}
|
||||
|
|
|
@ -106,12 +106,12 @@ public class DebugTabBody implements DebugTab {
|
|||
case XML:
|
||||
writer.append("<pre class=\"code").append(" xml").append("\">\n");
|
||||
writer.append(DebugResponseHelperImpl.escapeHtml(body));
|
||||
writer.append("</pre>\n");
|
||||
writer.append("\n</pre>\n");
|
||||
break;
|
||||
case JSON:
|
||||
writer.append("<pre class=\"code").append(" json").append("\">\n");
|
||||
writer.append(DebugResponseHelperImpl.escapeHtml(body));
|
||||
writer.append("</pre>\n");
|
||||
writer.append("\n</pre>\n");
|
||||
break;
|
||||
case IMAGE:
|
||||
writer.append("<img src=\"data:").append(response.getHeader(HttpHeader.CONTENT_TYPE)).append(";base64,")
|
||||
|
@ -122,7 +122,7 @@ public class DebugTabBody implements DebugTab {
|
|||
default:
|
||||
writer.append("<pre class=\"code").append("\">\n");
|
||||
writer.append(DebugResponseHelperImpl.escapeHtml(body));
|
||||
writer.append("</pre>\n");
|
||||
writer.append("\n</pre>\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,7 +85,7 @@ public class DebugTabResponse implements DebugTab {
|
|||
private Map<String, String> map(Map<String, List<String>> headers) {
|
||||
Map<String, String> result = new HashMap<String, String>();
|
||||
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
|
||||
if(entry.getValue().size() == 1) {
|
||||
if (entry.getValue().size() == 1) {
|
||||
result.put(entry.getKey(), entry.getValue().get(0));
|
||||
} else {
|
||||
result.put(entry.getKey(), entry.getValue().toString());
|
||||
|
@ -105,7 +105,7 @@ public class DebugTabResponse implements DebugTab {
|
|||
if (response != null && response.getContent() != null) {
|
||||
new DebugTabBody(response).appendHtml(writer);
|
||||
} else {
|
||||
writer.append("<p>ODataLibrary: no response body</p>");
|
||||
writer.append("<p>ODataLibrary: no response body</p>\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -89,7 +89,7 @@ public class DebugTabRuntime implements DebugTab {
|
|||
private void appendRuntimeNode(final RuntimeNode node, final String draw, final boolean isLast, final Writer writer)
|
||||
throws IOException {
|
||||
if (node.className != null) {
|
||||
writer.append("<li>")
|
||||
writer.append("<li>\n")
|
||||
.append("<span class=\"code\">")
|
||||
.append("<span class=\"draw\">").append(draw)
|
||||
.append(isLast ? "└" : "├").append("─ </span>")
|
||||
|
|
|
@ -42,22 +42,14 @@ public class DebugTabServer implements DebugTab {
|
|||
|
||||
@Override
|
||||
public void appendJson(JsonGenerator gen) throws IOException {
|
||||
if (serverEnvironmentVariables != null && !serverEnvironmentVariables.isEmpty()) {
|
||||
DebugResponseHelperImpl.appendJsonTable(gen, serverEnvironmentVariables);
|
||||
} else {
|
||||
gen.writeNull();
|
||||
}
|
||||
DebugResponseHelperImpl.appendJsonTable(gen, serverEnvironmentVariables);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendHtml(Writer writer) throws IOException {
|
||||
final Package pack = DebugResponseHelperImpl.class.getPackage();
|
||||
writer.append("<h2>Library Version</h2>\n")
|
||||
.append("<p>").append(pack.getImplementationTitle())
|
||||
.append(" Version ").append(pack.getImplementationVersion()).append("</p>\n")
|
||||
.append("<p>").append(DebugResponseHelperImpl.getVersion()).append("</p>\n")
|
||||
.append("<h2>Server Environment</h2>\n");
|
||||
if (serverEnvironmentVariables != null && !serverEnvironmentVariables.isEmpty()) {
|
||||
DebugResponseHelperImpl.appendHtmlTable(writer, serverEnvironmentVariables);
|
||||
}
|
||||
DebugResponseHelperImpl.appendHtmlTable(writer, serverEnvironmentVariables);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,12 +19,9 @@
|
|||
package org.apache.olingo.server.core.debug;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Writer;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.olingo.server.api.uri.UriInfo;
|
||||
import org.apache.olingo.server.api.uri.UriResource;
|
||||
import org.apache.olingo.server.api.uri.queryoption.CountOption;
|
||||
|
@ -38,9 +35,7 @@ import org.apache.olingo.server.api.uri.queryoption.SelectOption;
|
|||
import org.apache.olingo.server.api.uri.queryoption.SkipOption;
|
||||
import org.apache.olingo.server.api.uri.queryoption.TopOption;
|
||||
import org.apache.olingo.server.api.uri.queryoption.expression.Expression;
|
||||
import org.apache.olingo.server.core.serializer.utils.CircleStreamBuffer;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonEncoding;
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
|
||||
|
@ -63,11 +58,6 @@ public class DebugTabUri implements DebugTab {
|
|||
|
||||
@Override
|
||||
public void appendJson(JsonGenerator gen) throws IOException {
|
||||
if (uriInfo == null) {
|
||||
gen.writeNull();
|
||||
return;
|
||||
}
|
||||
|
||||
gen.writeStartObject();
|
||||
|
||||
if (uriInfo.getFormatOption() != null) {
|
||||
|
@ -233,7 +223,7 @@ public class DebugTabUri implements DebugTab {
|
|||
boolean first = true;
|
||||
for (UriResource resourcePart : selectItem.getResourcePath().getUriResourceParts()) {
|
||||
if (!first) {
|
||||
selectedProperty = selectedProperty + "/";
|
||||
selectedProperty += "/";
|
||||
}
|
||||
selectedProperty = resourcePart.toString();
|
||||
first = false;
|
||||
|
@ -245,62 +235,12 @@ public class DebugTabUri implements DebugTab {
|
|||
|
||||
@Override
|
||||
public void appendHtml(final Writer writer) throws IOException {
|
||||
if (uriInfo == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
writer.append("<h2>Uri Information</h2>\n")
|
||||
.append("<ul class=\"json\"><li>");
|
||||
writer.append(getJsonString());
|
||||
writer.append("</li></ul>\n");
|
||||
}
|
||||
|
||||
private String getJsonString() throws IOException {
|
||||
CircleStreamBuffer csb = new CircleStreamBuffer();
|
||||
IOException cachedException = null;
|
||||
OutputStream outputStream = csb.getOutputStream();
|
||||
try {
|
||||
JsonGenerator json =
|
||||
new JsonFactory().createGenerator(outputStream, JsonEncoding.UTF8)
|
||||
.setPrettyPrinter(new DefaultPrettyPrinter());
|
||||
appendJson(json);
|
||||
json.close();
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
throw e;
|
||||
} finally {
|
||||
if (outputStream != null) {
|
||||
try {
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
if (cachedException != null) {
|
||||
throw cachedException;
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
InputStream inputStream = csb.getInputStream();
|
||||
try {
|
||||
String jsonString = IOUtils.toString(inputStream);
|
||||
inputStream.close();
|
||||
return jsonString;
|
||||
} catch (IOException e) {
|
||||
throw e;
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
if (cachedException != null) {
|
||||
throw cachedException;
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.append("<ul class=\"json\">\n<li>\n");
|
||||
JsonGenerator json = new JsonFactory().createGenerator(writer)
|
||||
.setPrettyPrinter(new DefaultPrettyPrinter());
|
||||
appendJson(json);
|
||||
json.close();
|
||||
writer.append("\n</li>\n</ul>\n");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,17 +61,16 @@ public class ServerCoreDebugger {
|
|||
}
|
||||
}
|
||||
|
||||
public ODataResponse createDebugResponse(final HttpServletRequest request, final Exception exception,
|
||||
final ODataRequest odRequest, final ODataResponse odResponse, UriInfo uriInfo,
|
||||
Map<String, String> serverEnvironmentVariables) {
|
||||
public ODataResponse createDebugResponse(final ODataRequest request, final ODataResponse response,
|
||||
final Exception exception, final UriInfo uriInfo, final Map<String, String> serverEnvironmentVariables) {
|
||||
//Failsafe so we do not generate unauthorized debug messages
|
||||
if(!isDebugMode){
|
||||
return odResponse;
|
||||
if (!isDebugMode) {
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
DebugInformation debugInfo =
|
||||
createDebugInformation(request, exception, odRequest, odResponse, uriInfo, serverEnvironmentVariables);
|
||||
createDebugInformation(request, response, exception, uriInfo, serverEnvironmentVariables);
|
||||
|
||||
return debugSupport.createDebugResponse(debugFormat, debugInfo);
|
||||
} catch (Exception e) {
|
||||
|
@ -88,16 +87,15 @@ public class ServerCoreDebugger {
|
|||
return odResponse;
|
||||
}
|
||||
|
||||
private DebugInformation createDebugInformation(final HttpServletRequest request, final Exception exception,
|
||||
final ODataRequest odRequest, final ODataResponse odResponse, UriInfo uriInfo,
|
||||
Map<String, String> serverEnvironmentVaribles) {
|
||||
private DebugInformation createDebugInformation(final ODataRequest request, final ODataResponse response,
|
||||
final Exception exception, final UriInfo uriInfo, final Map<String, String> serverEnvironmentVariables) {
|
||||
DebugInformation debugInfo = new DebugInformation();
|
||||
debugInfo.setRequest(odRequest);
|
||||
debugInfo.setApplicationResponse(odResponse);
|
||||
debugInfo.setRequest(request);
|
||||
debugInfo.setApplicationResponse(response);
|
||||
|
||||
debugInfo.setException(exception);
|
||||
|
||||
debugInfo.setServerEnvironmentVariables(serverEnvironmentVaribles);
|
||||
debugInfo.setServerEnvironmentVariables(serverEnvironmentVariables);
|
||||
|
||||
debugInfo.setUriInfo(uriInfo);
|
||||
|
||||
|
|
|
@ -18,41 +18,28 @@
|
|||
*/
|
||||
package org.apache.olingo.server.core.debug;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.olingo.server.core.serializer.utils.CircleStreamBuffer;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonEncoding;
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
|
||||
public abstract class AbstractDebugTabTest {
|
||||
|
||||
protected String createHtml(DebugTab tab) throws Exception {
|
||||
protected String createHtml(DebugTab tab) throws IOException {
|
||||
StringWriter writer = new StringWriter();
|
||||
tab.appendHtml(writer);
|
||||
writer.flush();
|
||||
byte[] bytes = writer.toString().getBytes("UTF-8");
|
||||
return IOUtils.toString(new ByteArrayInputStream(bytes));
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
protected String createJson(DebugTab requestTab) throws IOException {
|
||||
CircleStreamBuffer csb = new CircleStreamBuffer();
|
||||
JsonGenerator gen = new JsonFactory().createGenerator(csb.getOutputStream(), JsonEncoding.UTF8);
|
||||
requestTab.appendJson(gen);
|
||||
protected String createJson(DebugTab tab) throws IOException {
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonGenerator gen = new JsonFactory().createGenerator(writer);
|
||||
tab.appendJson(gen);
|
||||
gen.flush();
|
||||
gen.close();
|
||||
csb.closeWrite();
|
||||
return IOUtils.toString(csb.getInputStream());
|
||||
writer.flush();
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
protected void print(DebugTab tab) throws Exception{
|
||||
System.out.println(createJson(tab));
|
||||
System.out.println("---------------------------------------------------------");
|
||||
System.out.println(createHtml(tab));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,10 +28,7 @@ public class DebugTabBodyTest extends AbstractDebugTabTest {
|
|||
public void nullResponseMustNotLeadToException() throws Exception {
|
||||
DebugTabBody tab = new DebugTabBody(null);
|
||||
|
||||
String expectedHtml = "<pre class=\"code\">\n"
|
||||
+ "ODataLibrary: No body.</pre>\n";
|
||||
|
||||
assertEquals("null", createJson(tab));
|
||||
assertEquals(expectedHtml, createHtml(tab));
|
||||
assertEquals("<pre class=\"code\">\nODataLibrary: No body.\n</pre>\n", createHtml(tab));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ public class DebugTabResponseTest extends AbstractDebugTabTest {
|
|||
+ "</tbody>\n"
|
||||
+ "</table>\n"
|
||||
+ "<h2>Response Body</h2>\n"
|
||||
+ "<p>ODataLibrary: no response body</p>";
|
||||
+ "<p>ODataLibrary: no response body</p>\n";
|
||||
|
||||
assertEquals(expectedJson, createJson(tab));
|
||||
assertEquals(expectedHtml, createHtml(tab));
|
||||
|
@ -71,7 +71,7 @@ public class DebugTabResponseTest extends AbstractDebugTabTest {
|
|||
+ "</tbody>\n"
|
||||
+ "</table>\n"
|
||||
+ "<h2>Response Body</h2>\n"
|
||||
+ "<p>ODataLibrary: no response body</p>";
|
||||
+ "<p>ODataLibrary: no response body</p>\n";
|
||||
assertEquals(expectedJson, createJson(tab));
|
||||
assertEquals(expectedHtml, createHtml(tab));
|
||||
}
|
||||
|
|
|
@ -18,7 +18,8 @@
|
|||
*/
|
||||
package org.apache.olingo.server.core.debug;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
|
@ -35,18 +36,17 @@ public class DebugTabServerTest extends AbstractDebugTabTest {
|
|||
assertEquals("null", createJson(serverTab));
|
||||
String html = createHtml(serverTab);
|
||||
assertTrue(html.startsWith("<h2>Library Version</h2>"));
|
||||
assertTrue(html.endsWith("<h2>Server Environment</h2>\n"));
|
||||
assertTrue(html.contains("<h2>Server Environment</h2>\n"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initialServerInformationMustNotleadToException() throws Exception {
|
||||
Map<String, String> env = Collections.emptyMap();
|
||||
DebugTabServer serverTab = new DebugTabServer(env);
|
||||
DebugTabServer serverTab = new DebugTabServer(Collections.<String, String> emptyMap());
|
||||
|
||||
assertEquals("null", createJson(serverTab));
|
||||
String html = createHtml(serverTab);
|
||||
assertTrue(html.startsWith("<h2>Library Version</h2>"));
|
||||
assertTrue(html.endsWith("<h2>Server Environment</h2>\n"));
|
||||
assertTrue(html.contains("<h2>Server Environment</h2>\n"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -18,9 +18,10 @@
|
|||
*/
|
||||
package org.apache.olingo.server.core.debug;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
@ -30,6 +31,7 @@ import javax.servlet.http.HttpServletRequest;
|
|||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.olingo.commons.api.ex.ODataRuntimeException;
|
||||
import org.apache.olingo.commons.api.http.HttpStatusCode;
|
||||
import org.apache.olingo.server.api.OData;
|
||||
import org.apache.olingo.server.api.ODataResponse;
|
||||
import org.apache.olingo.server.api.debug.DebugInformation;
|
||||
|
@ -44,11 +46,15 @@ public class ServerCoreDebuggerTest {
|
|||
@Before
|
||||
public void setupDebugger() {
|
||||
debugger = new ServerCoreDebugger(OData.newInstance());
|
||||
debugger.setDebugSupportProcessor(new LocalDebugProcessor());
|
||||
DebugSupport processor = mock(DebugSupport.class);
|
||||
when(processor.isUserAuthorized()).thenReturn(true);
|
||||
when(processor.createDebugResponse(anyString(), any(DebugInformation.class)))
|
||||
.thenThrow(new ODataRuntimeException("Test"));
|
||||
debugger.setDebugSupportProcessor(processor);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void standardIsDebugModeIsFlase() {
|
||||
public void standardIsDebugModeIsFalse() {
|
||||
assertFalse(debugger.isDebugMode());
|
||||
}
|
||||
|
||||
|
@ -86,36 +92,20 @@ public class ServerCoreDebuggerTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFailResponse() throws IOException {
|
||||
public void failResponse() throws IOException {
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
when(request.getParameter(DebugSupport.ODATA_DEBUG_QUERY_PARAMETER)).thenReturn(DebugSupport.ODATA_DEBUG_JSON);
|
||||
debugger.resolveDebugMode(request);
|
||||
ODataResponse debugResponse = debugger.createDebugResponse(null, null, null, null, null, null);
|
||||
assertEquals(500, debugResponse.getStatusCode());
|
||||
ODataResponse debugResponse = debugger.createDebugResponse(null, null, null, null, null);
|
||||
assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), debugResponse.getStatusCode());
|
||||
assertEquals("ODataLibrary: Could not assemble debug response.", IOUtils.toString(debugResponse.getContent()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noDebugModeCreateDebugResponseCallMustDoNothing() {
|
||||
ODataResponse odResponse = new ODataResponse();
|
||||
ODataResponse debugResponse = debugger.createDebugResponse(null, null, null, odResponse, null, null);
|
||||
ODataResponse debugResponse = debugger.createDebugResponse(null, odResponse, null, null, null);
|
||||
|
||||
assertTrue(odResponse == debugResponse);
|
||||
}
|
||||
|
||||
public class LocalDebugProcessor implements DebugSupport {
|
||||
|
||||
@Override
|
||||
public void init(OData odata) {}
|
||||
|
||||
@Override
|
||||
public boolean isUserAuthorized() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ODataResponse createDebugResponse(String debugFormat, DebugInformation debugInfo) {
|
||||
throw new ODataRuntimeException("Test");
|
||||
}
|
||||
assertEquals(odResponse, debugResponse);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue