Example usage for java.net HttpURLConnection getContentType

List of usage examples for java.net HttpURLConnection getContentType

Introduction

In this page you can find the example usage for java.net HttpURLConnection getContentType.

Prototype

public String getContentType() 

Source Link

Document

Returns the value of the content-type header field.

Usage

From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceIntegrationTest.java

/**
 * Test of getPackagesAsFeed method, of class PackageService.
 *//*  w w  w.ja v  a2  s .  c  o m*/
@Test
@RunAsClient
public void testGetPackagesForXML(@ArquillianResource URL baseURL) throws MalformedURLException, IOException {
    URL url = new URL(baseURL, "rest/packages");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept", MediaType.APPLICATION_XML);
    connection.connect();
    assertEquals(200, connection.getResponseCode());
    assertEquals(MediaType.APPLICATION_XML, connection.getContentType());
    //System.out.println(IOUtils.toString(connection.getInputStream()));
    //TODO: verify
}

From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceIntegrationTest.java

/**
 * Test of getPackagesAsFeed method, of class PackageService.
 *///from w w  w  .  java2  s .  c o  m
@Test
@RunAsClient
public void testGetPackageForJSON(@ArquillianResource URL baseURL) throws MalformedURLException, IOException {
    URL url = new URL(baseURL, "rest/packages/restPackage1");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept", MediaType.APPLICATION_JSON);
    connection.connect();
    assertEquals(200, connection.getResponseCode());
    assertEquals(MediaType.APPLICATION_JSON, connection.getContentType());
    //logger.log (LogLevel, IOUtils.toString(connection.getInputStream()));
}

From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceIntegrationTest.java

/**
 * Test of getPackagesAsFeed method, of class PackageService.
 *///from  www. j  ava  2s . com
@Test
@RunAsClient
public void testGetPackagesForJSON(@ArquillianResource URL baseURL) throws MalformedURLException, IOException {
    URL url = new URL(baseURL, "rest/packages");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept", MediaType.APPLICATION_JSON);
    connection.connect();
    assertEquals(200, connection.getResponseCode());
    assertEquals(MediaType.APPLICATION_JSON, connection.getContentType());
    //System.out.println(IOUtils.toString(connection.getInputStream()));
    //TODO: verify
}

From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceIntegrationTest.java

@Test
@RunAsClient/*  w w  w .j av a  2s  . c o  m*/
public void testGetHistoricalAssetSource(@ArquillianResource URL baseURL) throws Exception {
    URL url = new URL(baseURL, "rest/packages/restPackage1/assets/model1/versions/1/source");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept", MediaType.WILDCARD);
    connection.connect();

    assertEquals(200, connection.getResponseCode());
    assertEquals(MediaType.TEXT_PLAIN, connection.getContentType());
    String result = IOUtils.toString(connection.getInputStream());

    assertTrue(result.contains("declare Album1"));
    assertTrue(result.contains("genre1: String"));

    url = new URL(baseURL, "rest/packages/restPackage1/assets/model1/versions/2/source");
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept", MediaType.WILDCARD);
    connection.connect();

    assertEquals(200, connection.getResponseCode());
    assertEquals(MediaType.TEXT_PLAIN, connection.getContentType());
    result = IOUtils.toString(connection.getInputStream());
    assertTrue(result.contains("declare Album2"));
    assertTrue(result.contains("genre2: String"));
}

From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceIntegrationTest.java

@Test
@RunAsClient//w w w . j a va  2s.c  o m
public void testGetHistoricalPackageBinary(@ArquillianResource URL baseURL) throws Exception {
    URL url = new URL(baseURL, "rest/packages/restPackage2/versions/2/binary");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept", MediaType.APPLICATION_OCTET_STREAM);
    connection.connect();

    assertEquals(200, connection.getResponseCode());
    assertEquals(MediaType.APPLICATION_OCTET_STREAM, connection.getContentType());
}

From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceIntegrationTest.java

@Test
@RunAsClient//ww  w.  j a v a2 s .  c  o m
public void testGetBinaryContentFromNonBinaryAsset(@ArquillianResource URL baseURL) throws Exception {
    //Get the asset binary. If this asset has no binary content, this will return its 
    //source content instead
    URL url = new URL(baseURL, "rest/packages/restPackage1/assets/model1/binary");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept", MediaType.APPLICATION_OCTET_STREAM);
    connection.connect();
    assertEquals(200, connection.getResponseCode());
    assertEquals(MediaType.APPLICATION_OCTET_STREAM, connection.getContentType());
    String result = IOUtils.toString(connection.getInputStream());
    assertTrue(result.indexOf("declare Album2") > -1);
}

From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceIntegrationTest.java

/**
 * Test of getPackagesAsFeed method, of class PackageService.
 * An example of expected result:/*from   w  w  w.  j ava 2s  . c o  m*/
 * 
 *   <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
 *   <package>
 *     <binaryLink>http://127.0.0.1:8080/da8f3038-194b-4627-ae55-94b1f21d8e24/rest/packages/restPackage1/binary</binaryLink> 
 *     <description>this is package restPackage1</description> 
 *     <sourceLink>http://127.0.0.1:8080/da8f3038-194b-4627-ae55-94b1f21d8e24/rest/packages/restPackage1/source</sourceLink> 
 *     <title>restPackage1</title> 
 *     <author>guest</author>
 *     <published>2012-01-31T19:15:56.933+08:00</published>
 *     <metadata>
 *       <archived>false</archived>
 *       <created>2012-01-30T16:48:37.081+08:00</created> 
 *       <state /> 
 *       <uuid>690e386a-89df-4018-8688-cb322b492f53</uuid> 
 *       <versionNumber>3</versionNumber>
 *       <checkInComment>version3</checkInComment>
 *     </metadata>
 *     
 *     <assets>http://127.0.0.1:8080/da8f3038-194b-4627-ae55-94b1f21d8e24/rest/packages/restPackage1/assets/rule4</assets> 
 *     <assets>http://127.0.0.1:8080/da8f3038-194b-4627-ae55-94b1f21d8e24/rest/packages/restPackage1/assets/drools</assets> 
 *     <assets>http://127.0.0.1:8080/da8f3038-194b-4627-ae55-94b1f21d8e24/rest/packages/restPackage1/assets/rule1</assets> 
 *     <assets>http://127.0.0.1:8080/da8f3038-194b-4627-ae55-94b1f21d8e24/rest/packages/restPackage1/assets/func</assets> 
 *     <assets>http://127.0.0.1:8080/da8f3038-194b-4627-ae55-94b1f21d8e24/rest/packages/restPackage1/assets/myDSL</assets> 
 *     <assets>http://127.0.0.1:8080/da8f3038-194b-4627-ae55-94b1f21d8e24/rest/packages/restPackage1/assets/rule2</assets> 
 *     <assets>http://127.0.0.1:8080/da8f3038-194b-4627-ae55-94b1f21d8e24/rest/packages/restPackage1/assets/model1</assets> 
 *   </package>     
 */
@Test
@RunAsClient
public void testGetPackageForXML(@ArquillianResource URL baseURL) throws Exception {
    URL url = new URL(baseURL, "rest/packages/restPackage1");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept", MediaType.APPLICATION_XML);
    connection.connect();
    assertEquals(200, connection.getResponseCode());
    assertEquals(MediaType.APPLICATION_XML, connection.getContentType());
    //System.out.println("------------------------");
    //System.out.println(IOUtils.toString(connection.getInputStream()));
    Package p = unmarshalPackageXML(connection.getInputStream());
    assertEquals("restPackage1", p.getTitle());
    assertEquals("this is package restPackage1", p.getDescription());
    assertNotNull(p.getAuthor());
    assertNotNull(p.getPublished());
    assertEquals(new URL(baseURL, "rest/packages/restPackage1/source").toExternalForm(),
            p.getSourceLink().toString());
    assertEquals(new URL(baseURL, "rest/packages/restPackage1/binary").toExternalForm(),
            p.getBinaryLink().toString());
    PackageMetadata pm = p.getMetadata();
    assertFalse(pm.isArchived());
    assertNotNull(pm.getCreated());
    assertNotNull(pm.getUuid());
    assertEquals("version3", pm.getCheckinComment());
    assertEquals(3, pm.getVersionNumber());

    Set<URI> assetsURI = p.getAssets();
    assertEquals(7, assetsURI.size());
    assertTrue(assetsURI.contains(new URL(baseURL, "rest/packages/restPackage1/assets/drools").toURI()));
    assertTrue(assetsURI.contains(new URL(baseURL, "rest/packages/restPackage1/assets/func").toURI()));
    assertTrue(assetsURI.contains(new URL(baseURL, "rest/packages/restPackage1/assets/myDSL").toURI()));
    assertTrue(assetsURI.contains(new URL(baseURL, "rest/packages/restPackage1/assets/rule1").toURI()));
    assertTrue(assetsURI.contains(new URL(baseURL, "rest/packages/restPackage1/assets/rule2").toURI()));
    assertTrue(assetsURI.contains(new URL(baseURL, "rest/packages/restPackage1/assets/rule4").toURI()));
    assertTrue(assetsURI.contains(new URL(baseURL, "rest/packages/restPackage1/assets/model1").toURI()));
}

From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceIntegrationTest.java

@Test
@RunAsClient/*from  w w  w  .j  a va 2 s .  c om*/
public void testUpdateAndGetAssetSource(@ArquillianResource URL baseURL) throws Exception {
    /*
     *  Get the content of rule4
     */
    URL url = new URL(baseURL, "rest/packages/restPackage1/assets/rule4/source");
    HttpURLConnection connection1 = (HttpURLConnection) url.openConnection();
    connection1.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    connection1.setRequestMethod("GET");
    connection1.setRequestProperty("Accept", MediaType.TEXT_PLAIN);
    connection1.connect();
    assertEquals(200, connection1.getResponseCode());
    assertEquals(MediaType.TEXT_PLAIN, connection1.getContentType());
    String newContent = "rule 'nheron' when Goo1() then end";
    /*
     * update the content
     */
    URL url2 = new URL(baseURL, "rest/packages/restPackage1/assets/rule4/source");
    HttpURLConnection connection2 = (HttpURLConnection) url2.openConnection();
    connection2.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    connection2.setDoOutput(true);
    connection2.setRequestMethod("PUT");
    connection2.setRequestProperty("Accept", MediaType.APPLICATION_XML);
    connection2.setRequestProperty("Content-Type", MediaType.TEXT_PLAIN);
    OutputStreamWriter out = new OutputStreamWriter(connection2.getOutputStream());
    out.write(newContent);
    out.close();
    connection2.getInputStream();
    assertEquals(204, connection2.getResponseCode());
    /*
     * get the content again and verify it was modified
     */
    URL url3 = new URL(baseURL, "rest/packages/restPackage1/assets/rule4/source");
    HttpURLConnection connection3 = (HttpURLConnection) url3.openConnection();
    connection3.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    connection3.setRequestMethod("GET");
    connection3.setRequestProperty("Accept", MediaType.TEXT_PLAIN);
    connection3.connect();

    assertEquals(200, connection3.getResponseCode());
    assertEquals(MediaType.TEXT_PLAIN, connection3.getContentType());
    String result = IOUtils.toString(connection3.getInputStream());
    assertEquals(result, newContent);
}

From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceIntegrationTest.java

@Test
@RunAsClient//from   ww w .ja va 2s. c o  m
public void testGetAssetVersionsForAtom(@ArquillianResource URL baseURL)
        throws MalformedURLException, IOException {
    URL url = new URL(baseURL, "rest/packages/restPackage1/assets/model1/versions");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML);
    connection.connect();
    assertEquals(200, connection.getResponseCode());
    assertEquals(MediaType.APPLICATION_ATOM_XML, connection.getContentType());
    //System.out.println(IOUtils.toString(connection.getInputStream()));

    InputStream in = connection.getInputStream();
    assertNotNull(in);
    Document<Feed> doc = abdera.getParser().parse(in);
    Feed feed = doc.getRoot();
    assertEquals("Version history of model1", feed.getTitle());

    List<Entry> entries = feed.getEntries();
    assertEquals(2, entries.size());

    Map<String, Entry> entriesMap = new HashMap<String, Entry>();
    for (Entry entry : entries) {
        entriesMap.put(entry.getTitle(), entry);
    }

    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/assets/model1/versions/1",
            entriesMap.get("1").getLinks().get(0).getHref().getPath());
    assertTrue(entriesMap.get("1").getUpdated() != null);
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/assets/model1/versions/2",
            entriesMap.get("2").getLinks().get(0).getHref().getPath());
    assertTrue(entriesMap.get("2").getUpdated() != null);
}

From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceIntegrationTest.java

/**
 * Test of getPackagesAsFeed method, of class PackageService.
 *///  w w w.ja  v  a 2  s . c  o  m
@Test
@RunAsClient
public void testGetPackagesForAtom(@ArquillianResource URL baseURL) throws MalformedURLException, IOException {
    URL url = new URL(baseURL, "rest/packages");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML);
    connection.connect();
    assertEquals(200, connection.getResponseCode());
    assertEquals(MediaType.APPLICATION_ATOM_XML, connection.getContentType());
    //System.out.println(IOUtils.toString(connection.getInputStream()));

    InputStream in = connection.getInputStream();
    assertNotNull(in);
    Document<Feed> doc = abdera.getParser().parse(in);
    Feed feed = doc.getRoot();
    assertEquals(baseURL.getPath() + "rest/packages", feed.getBaseUri().getPath());
    assertEquals("Packages", feed.getTitle());

    List<Entry> entries = feed.getEntries();
    assertEquals(4, entries.size());
    Iterator<Entry> it = entries.iterator();
    boolean foundPackageEntry = false;
    while (it.hasNext()) {
        Entry entry = it.next();
        if ("restPackage1".equals(entry.getTitle())) {
            foundPackageEntry = true;
            List<Link> links = entry.getLinks();
            assertEquals(1, links.size());
            assertEquals(baseURL.getPath() + "rest/packages/restPackage1", links.get(0).getHref().getPath());
        }
    }
    assertTrue(foundPackageEntry);
}