Search in sources :

Example 21 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project alfresco-remote-api by Alfresco.

the class JsonJacksonTests method testSerializeMultipleObjects.

@Test
public void testSerializeMultipleObjects() throws IOException {
    final Collection<Comment> allComments = new ArrayList<Comment>();
    Comment aComment = new Comment();
    aComment.setContent("<b>There it is</b>");
    allComments.add(aComment);
    aComment = new Comment();
    aComment.setContent("<p>I agree with the author</p>");
    allComments.add(aComment);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    jsonHelper.withWriter(out, new Writer() {

        @Override
        public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException {
            FilterProvider fp = new SimpleFilterProvider().addFilter(JacksonHelper.DEFAULT_FILTER_NAME, new ReturnAllBeanProperties());
            objectMapper.writer(fp).writeValue(generator, allComments);
        }
    });
    assertTrue(out.toString().contains("content\":\"<b>There it is</b>"));
    assertTrue(out.toString().contains("content\":\"<p>I agree with the author</p>"));
}
Also used : Comment(org.alfresco.rest.api.model.Comment) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) ReturnAllBeanProperties(org.alfresco.rest.framework.jacksonextensions.JacksonHelper.ReturnAllBeanProperties) Writer(org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 22 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project alfresco-remote-api by Alfresco.

the class SerializeTests method writeResponse.

private String writeResponse(final Object respons) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    jsonHelper.withWriter(out, new Writer() {

        @Override
        public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException {
            objectMapper.writeValue(generator, respons);
        }
    });
    System.out.println(out.toString());
    return out.toString();
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) PrintWriter(java.io.PrintWriter) Writer(org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 23 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project alfresco-remote-api by Alfresco.

the class WebScriptOptionsMetaData method writeMetaData.

@Override
public void writeMetaData(OutputStream out, ResourceWithMetadata resource, Map<String, ResourceWithMetadata> allApiResources) throws IOException {
    final Object result = processResult(resource, allApiResources);
    assistant.getJsonHelper().withWriter(out, new Writer() {

        @Override
        public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException {
            objectMapper.writeValue(generator, result);
        }
    });
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) ResourceMetaDataWriter(org.alfresco.rest.framework.metadata.ResourceMetaDataWriter) Writer(org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 24 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project qpid-broker-j by apache.

the class CompressedResponsesTest method doCompressionTest.

private void doCompressionTest(final boolean allowCompression, final boolean acceptCompressed) throws Exception {
    final boolean expectCompression = allowCompression && acceptCompressed;
    getHelper().submitRequest("plugin/httpManagement", "POST", Collections.singletonMap("compressResponses", expectCompression), SC_OK);
    HttpURLConnection conn = getHelper().openManagementConnection("/service/metadata", "GET");
    try {
        if (acceptCompressed) {
            conn.setRequestProperty("Accept-Encoding", "gzip");
        }
        conn.connect();
        String contentEncoding = conn.getHeaderField("Content-Encoding");
        if (expectCompression) {
            assertEquals("gzip", contentEncoding);
        } else {
            if (contentEncoding != null) {
                assertEquals("identity", contentEncoding);
            }
        }
        byte[] bytes;
        try (ByteArrayOutputStream contentBuffer = new ByteArrayOutputStream()) {
            ByteStreams.copy(conn.getInputStream(), contentBuffer);
            bytes = contentBuffer.toByteArray();
        }
        try (InputStream jsonStream = expectCompression ? new GZIPInputStream(new ByteArrayInputStream(bytes)) : new ByteArrayInputStream(bytes)) {
            ObjectMapper mapper = new ObjectMapper();
            try {
                mapper.readValue(jsonStream, LinkedHashMap.class);
            } catch (JsonParseException | JsonMappingException e) {
                fail("Message was not in correct format");
            }
        }
    } finally {
        conn.disconnect();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) HttpURLConnection(java.net.HttpURLConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 25 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project bamboobsc by billchen198318.

the class CxfServerBean method shutdownOrReloadCallOneSystem.

@SuppressWarnings("unchecked")
public static Map<String, Object> shutdownOrReloadCallOneSystem(HttpServletRequest request, String system, String type) throws ServiceException, Exception {
    if (StringUtils.isBlank(system) || StringUtils.isBlank(type)) {
        throw new ServiceException(SysMessageUtil.get(GreenStepSysMsgConstants.PARAMS_BLANK));
    }
    String urlStr = ApplicationSiteUtils.getBasePath(system, request) + "config-services?type=" + type + "&value=" + createParamValue();
    logger.info("shutdownOrReloadCallSystem , url=" + urlStr);
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(urlStr);
    client.executeMethod(method);
    byte[] responseBody = method.getResponseBody();
    if (null == responseBody) {
        throw new Exception("no response!");
    }
    String content = new String(responseBody, Constants.BASE_ENCODING);
    logger.info("shutdownOrReloadCallSystem , system=" + system + " , type=" + type + " , response=" + content);
    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> dataMap = null;
    try {
        dataMap = (Map<String, Object>) mapper.readValue(content, HashMap.class);
    } catch (JsonParseException e) {
        logger.error(e.getMessage().toString());
    } catch (JsonMappingException e) {
        logger.error(e.getMessage().toString());
    }
    if (null == dataMap) {
        throw new Exception("response content error!");
    }
    return dataMap;
}
Also used : ServiceException(com.netsteadfast.greenstep.base.exception.ServiceException) HttpClient(org.apache.commons.httpclient.HttpClient) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JsonParseException(com.fasterxml.jackson.core.JsonParseException) HttpMethod(org.apache.commons.httpclient.HttpMethod) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ServiceException(com.netsteadfast.greenstep.base.exception.ServiceException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)147 IOException (java.io.IOException)75 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)62 JsonParseException (com.fasterxml.jackson.core.JsonParseException)53 Test (org.junit.Test)28 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)23 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)16 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 File (java.io.File)14 Map (java.util.Map)14 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 InputStream (java.io.InputStream)9 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 List (java.util.List)7 Writer (org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer)7 Test (org.junit.jupiter.api.Test)6 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)5