Search in sources :

Example 1 with JacksonHelper

use of org.alfresco.rest.framework.jacksonextensions.JacksonHelper in project alfresco-remote-api by Alfresco.

the class ResponseWriter method renderErrorResponse.

/**
 * Renders a JSON error response
 *
 * @param errorResponse The error
 * @param res           web script response
 * @throws IOException
 */
default void renderErrorResponse(final ErrorResponse errorResponse, final WebScriptResponse res, final JacksonHelper jsonHelper) throws IOException {
    String logId = "";
    if (Status.STATUS_INTERNAL_SERVER_ERROR == errorResponse.getStatusCode() || resWriterLogger().isDebugEnabled()) {
        logId = org.alfresco.util.GUID.generate();
        resWriterLogger().error(logId + " : " + errorResponse.getStackTrace());
    }
    String stackMessage = I18NUtil.getMessage(DefaultExceptionResolver.STACK_MESSAGE_ID);
    final ErrorResponse errorToWrite = new ErrorResponse(errorResponse.getErrorKey(), errorResponse.getStatusCode(), errorResponse.getBriefSummary(), stackMessage, logId, errorResponse.getAdditionalState(), DefaultExceptionResolver.ERROR_URL);
    setContentInfoOnResponse(res, DEFAULT_JSON_CONTENT);
    // Status must be set before the response is written by Jackson (which will by default close and commit the response).
    // In a r/w txn, web script buffered responses ensure that it doesn't really matter but for r/o txns this is important.
    res.setStatus(errorToWrite.getStatusCode());
    jsonHelper.withWriter(res.getOutputStream(), new JacksonHelper.Writer() {

        @SuppressWarnings("unchecked")
        @Override
        public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException {
            JSONObject obj = new JSONObject();
            obj.put("error", errorToWrite);
            objectMapper.writeValue(generator, obj);
        }
    });
}
Also used : JSONObject(org.json.simple.JSONObject) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JacksonHelper(org.alfresco.rest.framework.jacksonextensions.JacksonHelper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ErrorResponse(org.alfresco.rest.framework.core.exceptions.ErrorResponse)

Example 2 with JacksonHelper

use of org.alfresco.rest.framework.jacksonextensions.JacksonHelper in project alfresco-remote-api by Alfresco.

the class JsonJacksonTests method setupTests.

@BeforeClass
public static void setupTests() throws Exception {
    jsonHelper = new JacksonHelper();
    RestJsonModule module = new RestJsonModule();
    jsonHelper.setModule(module);
    jsonHelper.afterPropertiesSet();
}
Also used : RestJsonModule(org.alfresco.rest.framework.jacksonextensions.RestJsonModule) JacksonHelper(org.alfresco.rest.framework.jacksonextensions.JacksonHelper) BeforeClass(org.junit.BeforeClass)

Example 3 with JacksonHelper

use of org.alfresco.rest.framework.jacksonextensions.JacksonHelper in project alfresco-remote-api by Alfresco.

the class ParamsExtractorTests method setupTests.

@BeforeClass
public static void setupTests() throws Exception {
    jsonHelper = new JacksonHelper();
    RestJsonModule module = new RestJsonModule();
    jsonHelper.setModule(module);
    jsonHelper.afterPropertiesSet();
    assistant = new ApiAssistant();
    assistant.setJsonHelper(jsonHelper);
}
Also used : ApiAssistant(org.alfresco.rest.framework.tools.ApiAssistant) RestJsonModule(org.alfresco.rest.framework.jacksonextensions.RestJsonModule) JacksonHelper(org.alfresco.rest.framework.jacksonextensions.JacksonHelper) BeforeClass(org.junit.BeforeClass)

Aggregations

JacksonHelper (org.alfresco.rest.framework.jacksonextensions.JacksonHelper)3 RestJsonModule (org.alfresco.rest.framework.jacksonextensions.RestJsonModule)2 BeforeClass (org.junit.BeforeClass)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 ErrorResponse (org.alfresco.rest.framework.core.exceptions.ErrorResponse)1 ApiAssistant (org.alfresco.rest.framework.tools.ApiAssistant)1 JSONObject (org.json.simple.JSONObject)1