Example usage for com.fasterxml.jackson.dataformat.xml XmlMapper XmlMapper

List of usage examples for com.fasterxml.jackson.dataformat.xml XmlMapper XmlMapper

Introduction

In this page you can find the example usage for com.fasterxml.jackson.dataformat.xml XmlMapper XmlMapper.

Prototype

public XmlMapper() 

Source Link

Usage

From source file:org.yql4j.impl.HttpComponentsYqlClient.java

/**
 * Creates the Jackson {@link ObjectMapper} instance to use for XML
 * processing./*from  w  w w  .j  a va 2  s .c  om*/
 * <p>
 * Applies only if <code>format=xml</code>.
 * 
 * @return a Jackson {@link ObjectMapper} instance
 */
protected ObjectMapper createXmlMapper() {
    return new XmlMapper();
}

From source file:alluxio.client.rest.TestCase.java

/**
 * Runs the test case./*w w w.jav  a2s.c o m*/
 */
public void run() throws Exception {
    String expected = "";
    if (mExpectedResult != null) {
        if (mOptions.getContentType().equals(TestCaseOptions.JSON_CONTENT_TYPE)) {
            ObjectMapper mapper = new ObjectMapper();
            if (mOptions.isPrettyPrint()) {
                expected = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mExpectedResult);
            } else {
                expected = mapper.writeValueAsString(mExpectedResult);
            }
        } else if (mOptions.getContentType().equals(TestCaseOptions.XML_CONTENT_TYPE)) {
            XmlMapper mapper = new XmlMapper();
            if (mOptions.isPrettyPrint()) {
                expected = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mExpectedResult);
            } else {
                expected = mapper.writeValueAsString(mExpectedResult);
            }
        } else {
            throw new InvalidArgumentException("Invalid content type in TestCaseOptions!");
        }
    }
    String result = call();
    Assert.assertEquals(mEndpoint, expected, result);
}

From source file:ninja.utils.NinjaTestBrowser.java

public String postXml(String url, Object object) {

    try {//  w  w w . ja  v  a  2 s.  c  om
        httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

        HttpPost post = new HttpPost(url);
        StringEntity entity = new StringEntity(new XmlMapper().writeValueAsString(object), "utf-8");
        entity.setContentType("application/xml; charset=utf-8");
        post.setEntity(entity);
        post.releaseConnection();

        // Here we go!
        return EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8");

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.r10r.doctester.testbrowser.TestBrowserImpl.java

private Response makePatchPostOrPutRequest(Request httpRequest) {

    org.apache.http.HttpResponse apacheHttpClientResponse;
    Response response = null;//from ww w. j a  va  2  s.c o  m

    try {

        httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

        HttpEntityEnclosingRequestBase apacheHttpRequest;

        if (PATCH.equalsIgnoreCase(httpRequest.httpRequestType)) {

            apacheHttpRequest = new HttpPatch(httpRequest.uri);

        } else if (POST.equalsIgnoreCase(httpRequest.httpRequestType)) {

            apacheHttpRequest = new HttpPost(httpRequest.uri);

        } else {

            apacheHttpRequest = new HttpPut(httpRequest.uri);
        }

        if (httpRequest.headers != null) {
            // add all headers
            for (Entry<String, String> header : httpRequest.headers.entrySet()) {
                apacheHttpRequest.addHeader(header.getKey(), header.getValue());
            }
        }

        ///////////////////////////////////////////////////////////////////
        // Either add form parameters...
        ///////////////////////////////////////////////////////////////////
        if (httpRequest.formParameters != null) {

            List<BasicNameValuePair> formparams = Lists.newArrayList();
            for (Entry<String, String> parameter : httpRequest.formParameters.entrySet()) {

                formparams.add(new BasicNameValuePair(parameter.getKey(), parameter.getValue()));
            }

            // encode form parameters and add
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams);
            apacheHttpRequest.setEntity(entity);

        }

        ///////////////////////////////////////////////////////////////////
        // Or add multipart file upload
        ///////////////////////////////////////////////////////////////////
        if (httpRequest.filesToUpload != null) {

            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

            for (Map.Entry<String, File> entry : httpRequest.filesToUpload.entrySet()) {

                // For File parameters
                entity.addPart(entry.getKey(), new FileBody((File) entry.getValue()));

            }

            apacheHttpRequest.setEntity(entity);

        }

        ///////////////////////////////////////////////////////////////////
        // Or add payload and convert if Json or Xml
        ///////////////////////////////////////////////////////////////////
        if (httpRequest.payload != null) {

            if (httpRequest.headers.containsKey(HEADER_CONTENT_TYPE)
                    && httpRequest.headers.containsValue(APPLICATION_JSON_WITH_CHARSET_UTF8)) {

                String string = new ObjectMapper().writeValueAsString(httpRequest.payload);

                StringEntity entity = new StringEntity(string, "utf-8");
                entity.setContentType("application/json; charset=utf-8");

                apacheHttpRequest.setEntity(entity);

            } else if (httpRequest.headers.containsKey(HEADER_CONTENT_TYPE)
                    && httpRequest.headers.containsValue(APPLICATION_XML_WITH_CHARSET_UTF_8)) {

                String string = new XmlMapper().writeValueAsString(httpRequest.payload);

                StringEntity entity = new StringEntity(string, "utf-8");
                entity.setContentType(APPLICATION_XML_WITH_CHARSET_UTF_8);

                apacheHttpRequest.setEntity(new StringEntity(string, "utf-8"));

            } else if (httpRequest.payload instanceof String) {

                StringEntity entity = new StringEntity((String) httpRequest.payload, "utf-8");
                apacheHttpRequest.setEntity(entity);

            } else {

                StringEntity entity = new StringEntity(httpRequest.payload.toString(), "utf-8");
                apacheHttpRequest.setEntity(entity);

            }

        }

        setHandleRedirect(apacheHttpRequest, httpRequest.followRedirects);

        // Here we go!
        apacheHttpClientResponse = httpClient.execute(apacheHttpRequest);
        response = convertFromApacheHttpResponseToDocTesterHttpResponse(apacheHttpClientResponse);

        apacheHttpRequest.releaseConnection();

    } catch (IOException e) {
        logger.error("Fatal problem creating PATCH, POST or PUT request in TestBrowser", e);
        throw new RuntimeException(e);
    }

    return response;

}

From source file:org.wisdom.content.jackson.JacksonSingleton.java

/**
 * Starts the JSON and XML support./*from ww  w  .  ja va 2  s .  com*/
 * An empty mapper is created.
 */
@Validate
public void validate() {
    LOGGER.info("Starting JSON and XML support services");
    setMappers(new ObjectMapper(), new XmlMapper());
}

From source file:org.doctester.testbrowser.TestBrowserImpl.java

private Response makePostOrPutRequest(Request httpRequest) {

    org.apache.http.HttpResponse apacheHttpClientResponse;
    Response response = null;/*from ww  w . j a  va  2  s.co  m*/

    try {

        httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

        HttpEntityEnclosingRequestBase apacheHttpRequest;

        if (POST.equalsIgnoreCase(httpRequest.httpRequestType)) {

            apacheHttpRequest = new HttpPost(httpRequest.uri);

        } else {

            apacheHttpRequest = new HttpPut(httpRequest.uri);
        }

        if (httpRequest.headers != null) {
            // add all headers
            for (Entry<String, String> header : httpRequest.headers.entrySet()) {
                apacheHttpRequest.addHeader(header.getKey(), header.getValue());
            }
        }

        ///////////////////////////////////////////////////////////////////
        // Either add form parameters...
        ///////////////////////////////////////////////////////////////////
        if (httpRequest.formParameters != null) {

            List<BasicNameValuePair> formparams = Lists.newArrayList();
            for (Entry<String, String> parameter : httpRequest.formParameters.entrySet()) {

                formparams.add(new BasicNameValuePair(parameter.getKey(), parameter.getValue()));
            }

            // encode form parameters and add
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams);
            apacheHttpRequest.setEntity(entity);

        }

        ///////////////////////////////////////////////////////////////////
        // Or add multipart file upload
        ///////////////////////////////////////////////////////////////////
        if (httpRequest.filesToUpload != null) {

            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

            for (Map.Entry<String, File> entry : httpRequest.filesToUpload.entrySet()) {

                // For File parameters
                entity.addPart(entry.getKey(), new FileBody((File) entry.getValue()));

            }

            apacheHttpRequest.setEntity(entity);

        }

        ///////////////////////////////////////////////////////////////////
        // Or add payload and convert if Json or Xml
        ///////////////////////////////////////////////////////////////////
        if (httpRequest.payload != null) {

            if (httpRequest.headers.containsKey(HEADER_CONTENT_TYPE)
                    && httpRequest.headers.containsValue(APPLICATION_JSON_WITH_CHARSET_UTF8)) {

                String string = new ObjectMapper().writeValueAsString(httpRequest.payload);

                StringEntity entity = new StringEntity(string, "utf-8");
                entity.setContentType("application/json; charset=utf-8");

                apacheHttpRequest.setEntity(entity);

            } else if (httpRequest.headers.containsKey(HEADER_CONTENT_TYPE)
                    && httpRequest.headers.containsValue(APPLICATION_XML_WITH_CHARSET_UTF_8)) {

                String string = new XmlMapper().writeValueAsString(httpRequest.payload);

                StringEntity entity = new StringEntity(string, "utf-8");
                entity.setContentType(APPLICATION_XML_WITH_CHARSET_UTF_8);

                apacheHttpRequest.setEntity(new StringEntity(string, "utf-8"));

            } else if (httpRequest.payload instanceof String) {

                StringEntity entity = new StringEntity((String) httpRequest.payload, "utf-8");
                apacheHttpRequest.setEntity(entity);

            } else {

                StringEntity entity = new StringEntity(httpRequest.payload.toString(), "utf-8");
                apacheHttpRequest.setEntity(entity);

            }

        }

        setHandleRedirect(apacheHttpRequest, httpRequest.followRedirects);

        // Here we go!
        apacheHttpClientResponse = httpClient.execute(apacheHttpRequest);
        response = convertFromApacheHttpResponseToDocTesterHttpResponse(apacheHttpClientResponse);

        apacheHttpRequest.releaseConnection();

    } catch (IOException e) {
        logger.error("Fatal problem creating POST or PUT request in TestBrowser", e);
        throw new RuntimeException(e);
    }

    return response;

}

From source file:nl.esciencecenter.xnattool.DataSetConfig.java

@JsonIgnore
public String toXML() throws JsonProcessingException {
    XmlMapper xmlMapper = new XmlMapper();
    String xml = xmlMapper.writeValueAsString(this);
    return xml;//  w  w w.  j ava 2 s .  c om
}

From source file:com.lushapp.common.web.utils.WebUtils.java

/**
 * XML.//from  w  w w  .j a  v a2 s. c o  m
 * @see #render(javax.servlet.http.HttpServletResponse, String, Object, String...)
 * @param response
 * @param data ? ?List Map
 * @param headers  null UTF-8? 
 * @throws IOException
 */
public static void renderXml(HttpServletResponse response, Object data, final String... headers) {
    renderXml(response, data, new XmlMapper(), headers);
}

From source file:nl.esciencecenter.medim.dicom.DicomProcessingProfile.java

public String toXML() throws JsonProcessingException {
    XmlMapper xmlMapper = new XmlMapper();
    String xml = xmlMapper.writeValueAsString(this);
    return xml;/*from   w ww . j  av  a2  s . c om*/
}

From source file:org.wisdom.content.jackson.JacksonSingleton.java

private void rebuildMappers() {
    mapper = new ObjectMapper();
    for (Module module : modules) {
        mapper.registerModule(module);/* w  w  w .  j  a v a  2  s . c  om*/
    }

    xml = new XmlMapper();
    for (Module module : modules) {
        xml.registerModule(module);
    }

    applyMapperConfiguration(mapper, xml);
}