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:org.geowebcache.jetty.RestIT.java

@Test
public void testCreateUpdateDelete() throws Exception {
    final String layerName = "testLayer";
    final String url1 = "http://example.com/wms1?";
    final String url2 = "http://example.com/wms2?";
    final String layers = "remoteLayer";

    // Create/*w w  w .java 2 s. co m*/
    {
        final HttpPut request = new HttpPut(jetty.getUri().resolve("rest/layers/").resolve(layerName + ".xml"));
        request.setEntity(new StringEntity(
                "<wmsLayer><name>" + layerName + "</name><wmsUrl><string>" + url1
                        + "</string></wmsUrl><wmsLayers>" + layers + "</wmsLayers></wmsLayer>",
                ContentType.APPLICATION_XML));
        try (CloseableHttpResponse response = admin.getClient().execute(request)) {
            assertThat(response.getStatusLine(), hasProperty("statusCode", equalTo(200)));
        }

        doGetXML("rest/layers.xml", admin.getClient(), equalTo(200), doc -> {
            assertThat(doc, hasXPath("/layers/layer[name/text()='" + layerName + "']/atom:link/@href",
                    equalTo(jetty.getUri().resolve("/geowebcache/layers/" + layerName + ".xml").toString())));
        });
        doGetXML("rest/layers/" + layerName + ".xml", admin.getClient(), equalTo(200), doc -> {
            assertThat(doc, hasXPath("/wmsLayer/name", equalTo(layerName)));
            assertThat(doc, hasXPath("/wmsLayer/wmsUrl/string", equalTo(url1)));
            assertThat(doc, hasXPath("/wmsLayer/wmsLayers", equalTo(layers)));
        });
    }
    // Update
    {
        final HttpPost request = new HttpPost(
                jetty.getUri().resolve("rest/layers/").resolve(layerName + ".xml"));
        request.setEntity(new StringEntity(
                "<wmsLayer><name>" + layerName + "</name><wmsUrl><string>" + url2
                        + "</string></wmsUrl><wmsLayers>" + layers + "</wmsLayers></wmsLayer>",
                ContentType.APPLICATION_XML));
        try (CloseableHttpResponse response = admin.getClient().execute(request)) {
            assertThat(response.getStatusLine(), hasProperty("statusCode", equalTo(200)));
        }
        doGetXML("rest/layers/" + layerName + ".xml", admin.getClient(), equalTo(200), doc -> {
            assertThat(doc, hasXPath("/wmsLayer/name", equalTo(layerName)));
            assertThat(doc, hasXPath("/wmsLayer/wmsUrl/string", equalTo(url2)));
            assertThat(doc, hasXPath("/wmsLayer/wmsLayers", equalTo(layers)));
        });
    }
    // GetCap
    {
        doGetXML("service/wmts?REQUEST=getcapabilities", anonymous.getClient(), equalTo(200), doc -> {
            assertThat(doc, hasXPath(
                    "/wmts:Capabilities/wmts:Contents/wmts:Layer/ows:Title[text()='" + layerName + "']"));
        });
    }
    // Delete
    {
        final HttpDelete request = new HttpDelete(
                jetty.getUri().resolve("rest/layers/").resolve(layerName + ".xml"));
        try (CloseableHttpResponse response = admin.getClient().execute(request)) {
            assertThat(response.getStatusLine(), hasProperty("statusCode", equalTo(200)));
        }

        doGetXML("rest/layers.xml", admin.getClient(), equalTo(200), doc -> {
            assertThat(doc, not(hasXPath("/layers/layer[name/text()='" + layerName + "']")));
        });

        final HttpGet request2 = new HttpGet(
                jetty.getUri().resolve("rest/layers/").resolve(layerName + ".xml"));
        try (CloseableHttpResponse response = admin.getClient().execute(request2)) {
            assertThat(response.getStatusLine(), hasProperty("statusCode", equalTo(404)));
        }
    }
    // GetCap
    {
        doGetXML("service/wmts?REQUEST=getcapabilities", anonymous.getClient(), equalTo(200), doc -> {
            assertThat(doc, not(hasXPath(
                    "/wmts:Capabilities/wmts:Contents/wmts:Layer/ows:Title[text()='" + layerName + "']")));
        });
    }

}

From source file:org.wso2.carbon.apimgt.impl.AbstractAPIManagerTestCase.java

@Test
public void testUploadWsdl() throws RegistryException, APIManagementException {
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
    Resource resource = new ResourceImpl();
    String resourcePath = "/test/wsdl";
    String wsdlContent = "sample wsdl";
    Resource resourceMock = Mockito.mock(Resource.class);
    resourceMock.setContent(wsdlContent);
    resourceMock.setMediaType(String.valueOf(ContentType.APPLICATION_XML));
    Mockito.when(registry.newResource()).thenReturn(resource);
    Mockito.doThrow(RegistryException.class).doReturn(resourcePath).when(registry).put(resourcePath, resource);
    try {/*from   w w w.j a  v  a2 s. co m*/
        abstractAPIManager.uploadWsdl(resourcePath, wsdlContent);
        Assert.fail("Exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while uploading wsdl to from the registry"));
    }
    abstractAPIManager.uploadWsdl(resourcePath, wsdlContent);
    Mockito.verify(registry, Mockito.atLeastOnce()).put(resourcePath, resource);
}

From source file:org.xwiki.extension.repository.xwiki.internal.XWikiExtensionRepository.java

protected CloseableHttpResponse postRESTResource(UriBuilder builder, String content, Object... values)
        throws IOException {
    String url;/* w  w  w. java 2  s .  c  om*/
    try {
        url = builder.build(values).toString();
    } catch (Exception e) {
        throw new IOException("Failed to build REST URL", e);
    }

    CloseableHttpClient httpClient = this.httpClientFactory.createClient(
            getDescriptor().getProperty("auth.user"), getDescriptor().getProperty("auth.password"));

    HttpPost postMethod = new HttpPost(url);
    postMethod.addHeader("Accept", "application/xml");

    StringEntity entity = new StringEntity(content,
            ContentType.create(ContentType.APPLICATION_XML.getMimeType(), Consts.UTF_8));
    postMethod.setEntity(entity);

    CloseableHttpResponse response;
    try {
        if (this.localContext != null) {
            response = httpClient.execute(postMethod, this.localContext);
        } else {
            response = httpClient.execute(postMethod);
        }
    } catch (Exception e) {
        throw new IOException(String.format("Failed to request [%s]", postMethod.getURI()), e);
    }

    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new IOException(String.format("Invalid answer [%s] from the server when requesting [%s]",
                response.getStatusLine().getStatusCode(), postMethod.getURI()));
    }

    return response;
}