Example usage for org.apache.http.entity ContentType APPLICATION_XML

List of usage examples for org.apache.http.entity ContentType APPLICATION_XML

Introduction

In this page you can find the example usage for org.apache.http.entity ContentType APPLICATION_XML.

Prototype

ContentType APPLICATION_XML

To view the source code for org.apache.http.entity ContentType APPLICATION_XML.

Click Source Link

Usage

From source file:interoperabilite.webservice.fluent.FluentResponseHandling.java

public static void main(String[] args) throws Exception {
    Document result = Request.Get("http://somehost/content").execute()
            .handleResponse(new ResponseHandler<Document>() {

                @Override/*from   w ww .j av a 2s  . co m*/
                public Document handleResponse(final HttpResponse response) throws IOException {
                    StatusLine statusLine = response.getStatusLine();
                    HttpEntity entity = response.getEntity();
                    if (statusLine.getStatusCode() >= 300) {
                        throw new HttpResponseException(statusLine.getStatusCode(),
                                statusLine.getReasonPhrase());
                    }
                    if (entity == null) {
                        throw new ClientProtocolException("Response contains no content");
                    }
                    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
                    try {
                        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
                        ContentType contentType = ContentType.getOrDefault(entity);
                        if (!contentType.equals(ContentType.APPLICATION_XML)) {
                            throw new ClientProtocolException("Unexpected content type:" + contentType);
                        }
                        Charset charset = contentType.getCharset();
                        if (charset == null) {
                            charset = Consts.ISO_8859_1;
                        }
                        return docBuilder.parse(entity.getContent(), charset.name());
                    } catch (ParserConfigurationException ex) {
                        throw new IllegalStateException(ex);
                    } catch (SAXException ex) {
                        throw new ClientProtocolException("Malformed XML document", ex);
                    }
                }

            });
    // Do something useful with the result
    System.out.println(result);
}

From source file:io.aos.protocol.http.httpcommon.FluentResponseHandling.java

public static void main(String... args) throws Exception {
    Document result = Request.Get("http://somehost/content").execute()
            .handleResponse(new ResponseHandler<Document>() {

                public Document handleResponse(final HttpResponse response) throws IOException {
                    StatusLine statusLine = response.getStatusLine();
                    HttpEntity entity = response.getEntity();
                    if (statusLine.getStatusCode() >= 300) {
                        throw new HttpResponseException(statusLine.getStatusCode(),
                                statusLine.getReasonPhrase());
                    }//  ww w  . ja v  a 2 s  .c o  m
                    if (entity == null) {
                        throw new ClientProtocolException("Response contains no content");
                    }
                    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
                    try {
                        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
                        ContentType contentType = ContentType.getOrDefault(entity);
                        if (!contentType.equals(ContentType.APPLICATION_XML)) {
                            throw new ClientProtocolException("Unexpected content type:" + contentType);
                        }
                        Charset charset = contentType.getCharset();
                        if (charset == null) {
                            charset = HTTP.DEF_CONTENT_CHARSET;
                        }
                        return docBuilder.parse(entity.getContent(), charset.name());
                    } catch (ParserConfigurationException ex) {
                        throw new IllegalStateException(ex);
                    } catch (SAXException ex) {
                        throw new ClientProtocolException("Malformed XML document", ex);
                    }
                }

            });
    // Do something useful with the result
    System.out.println(result);
}

From source file:it.govpay.web.console.utils.HttpClientUtils.java

public static HttpResponse sendRichiestaPagamento(String urlToInvoke, RichiestaPagamento richiestaPagamento,
        Logger log) throws Exception {
    HttpResponse responseGET = null;//from www  .  jav a 2s .co m
    try {
        log.debug("Invio del pagamento in corso...");

        URL urlObj = new URL(urlToInvoke);
        HttpHost target = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol());
        CloseableHttpClient client = HttpClientBuilder.create().disableRedirectHandling().build();
        HttpPost richiestaPost = new HttpPost();

        richiestaPost.setURI(urlObj.toURI());

        log.debug("Serializzazione pagamento in corso...");
        byte[] bufferPagamento = JaxbUtils.toBytes(richiestaPagamento);
        log.debug("Serializzazione pagamento completata.");

        HttpEntity bodyEntity = new InputStreamEntity(new ByteArrayInputStream(bufferPagamento),
                ContentType.APPLICATION_XML);
        richiestaPost.setEntity(bodyEntity);
        richiestaPost.setHeader("Content-Type", ContentType.APPLICATION_XML.getMimeType());

        log.debug("Invio tramite client Http in corso...");
        responseGET = client.execute(target, richiestaPost);

        if (responseGET == null)
            throw new NullPointerException("La Response HTTP e' null");

        log.debug("Invio tramite client Http completato.");
        return responseGET;
    } catch (Exception e) {
        log.error("Errore durante l'invio della richiesta di pagamento: " + e.getMessage(), e);
        throw e;
    }
}

From source file:com.spectralogic.ds3client.commands.interfaces.AbstractRequest.java

@Override
public String getContentType() {
    return ContentType.APPLICATION_XML.getMimeType();
}

From source file:com.spectralogic.ds3client.commands.AbstractRequest.java

@Override
public String getContentType() {
    return ContentType.APPLICATION_XML.toString();
}

From source file:org.sonatype.nexus.repositories.metadata.Hc4RawTransport.java

@Override
public byte[] readRawData(final String path) throws IOException {
    final HttpGet get = new HttpGet(createUrlWithPath(path));
    get.setHeader("Accept", ContentType.APPLICATION_XML.getMimeType());
    final HttpResponse response = httpClient.execute(get);
    try {//from   www  . j av  a2 s .  com
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == 200 && response.getEntity() != null) {
            return EntityUtils.toByteArray(response.getEntity());
        } else if (statusCode == 404) {
            return null;
        } else {
            throw new IOException("The response was not successful: " + response.getStatusLine());
        }
    } finally {
        EntityUtils.consume(response.getEntity());
    }
}

From source file:org.apache.olingo.client.core.communication.request.retrieve.ODataMetadataRequestImpl.java

/**
 * Constructor./*  w w  w.j  a  v a2  s. c  om*/
 *
 * @param odataClient client instance getting this request
 * @param uri metadata URI.
 */
ODataMetadataRequestImpl(final CommonODataClient odataClient, final URI uri) {
    super(odataClient, ODataPubFormat.class, uri);
    super.setAccept(ContentType.APPLICATION_XML.getMimeType());
    super.setContentType(ContentType.APPLICATION_XML.getMimeType());
}

From source file:org.llorllale.youtrack.api.mock.http.response.MockNotFoundResponse.java

/**
 * Ctor.//from   w ww.  j a  v  a2  s.c  om
 *
 * @param xml the xml payload that YouTrack returns in 404 responses
 * @since 0.4.0
 */
@SuppressWarnings("checkstyle:MagicNumber")
public MockNotFoundResponse(String xml) {
    this.statusLine = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 404, "Not Found");
    this.payload = new StringEntity(xml, ContentType.APPLICATION_XML);
}

From source file:org.n52.iceland.skeleton.Base.java

public String pox(String filename) throws IOException {
    URL url = Resources.getResource("requests/" + filename);
    InputStream request = Resources.asByteSource(url).openBufferedStream();
    return Request.Post(getEndpointURL()).bodyStream(request, ContentType.APPLICATION_XML).execute()
            .returnContent().asString();
}

From source file:au.csiro.casda.sodalint.Validator.java

/**
 * Retrieve the content from an address using a GET request.
 *  /*w ww  . j a v  a 2s  .c o m*/
 * @param address The address to be queried.
 * @return The content, or null if no content could be read.
 * @throws HttpResponseException If a non 200 response code is returned.
 * @throws UnsupportedEncodingException If the content does not have an XML format. 
 * @throws IOException If the content could not be read.
 */
protected String getXmlContentFromUrl(String address)
        throws HttpResponseException, UnsupportedEncodingException, IOException {
    Response response = Request.Get(address).execute();
    HttpResponse httpResponse = response.returnResponse();
    StatusLine statusLine = httpResponse.getStatusLine();
    final int statusCodeOk = 200;
    if (statusLine.getStatusCode() != statusCodeOk) {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }
    HttpEntity entity = httpResponse.getEntity();
    if (entity == null) {
        return null;
    }
    ContentType contentType = ContentType.getOrDefault(entity);
    if (!ContentType.APPLICATION_XML.getMimeType().equals(contentType.getMimeType())
            && !ContentType.TEXT_XML.getMimeType().equals(contentType.getMimeType())) {
        throw new UnsupportedEncodingException(contentType.toString());
    }
    String content = readTextContent(entity);
    if (StringUtils.isBlank(content)) {
        return null;
    }

    return content;
}