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
@RunAsClient//from  ww w . j  a  va 2  s. c om
public void testGetPackageVersionsForAtom(@ArquillianResource URL baseURL)
        throws MalformedURLException, IOException {
    URL url = new URL(baseURL, "rest/packages/restPackage1/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 restPackage1", feed.getTitle());

    List<Entry> entries = feed.getEntries();
    assertEquals(3, 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/versions/1",
            entriesMap.get("1").getLinks().get(0).getHref().getPath());
    assertTrue(entriesMap.get("1").getUpdated() != null);
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/versions/2",
            entriesMap.get("2").getLinks().get(0).getHref().getPath());
    assertTrue(entriesMap.get("2").getUpdated() != null);
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/versions/3",
            entriesMap.get("3").getLinks().get(0).getHref().getPath());
    assertTrue(entriesMap.get("3").getUpdated() != null);
}

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

@Test
@RunAsClient//from w  ww  .  ja v a  2  s  .c  o m
public void testGetPackageBinary(@ArquillianResource URL baseURL) throws Exception {
    // restPackageCompilationFailure build fails due to: ClassNotFoundException
    URL url = new URL(baseURL, "rest/packages/restPackageCompilationFailure/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(500, connection.getResponseCode());

    //restPackage2 should build ok. 
    URL url2 = new URL(baseURL, "rest/packages/restPackage2/binary");
    HttpURLConnection connection2 = (HttpURLConnection) url2.openConnection();
    connection2.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    connection2.setRequestMethod("GET");
    connection2.setRequestProperty("Accept", MediaType.APPLICATION_OCTET_STREAM);
    connection2.connect();

    assertEquals(200, connection2.getResponseCode());
    assertEquals(MediaType.APPLICATION_OCTET_STREAM, connection2.getContentType());
    //System.out.println(IOUtils.toString(connection2.getInputStream()));
}

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

@Test
@RunAsClient//w w  w . jav a  2  s .  c  om
public void testBasicAuthentication(@ArquillianResource URL baseURL) throws MalformedURLException, IOException {
    //Test with valid user name and pwd
    URL url = new URL(baseURL, "rest/packages");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML);
    byte[] authEncBytes = Base64.encodeBase64("admin:admin".getBytes());
    connection.setRequestProperty("Authorization", "Basic " + new String(authEncBytes));
    connection.connect();
    assertEquals(200, connection.getResponseCode());
    assertEquals(MediaType.APPLICATION_ATOM_XML, connection.getContentType());
    //System.out.println(IOUtils.toString(connection.getInputStream()));
}

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

@Test
@RunAsClient/*from w w  w.j  a  v a 2 s  .  co  m*/
public void testGetHistoricalPackageForAtom(@ArquillianResource URL baseURL)
        throws MalformedURLException, IOException {
    URL url = new URL(baseURL, "rest/packages/restPackage1/versions/2");
    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<Entry> doc = abdera.getParser().parse(in);
    Entry entry = doc.getRoot();
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/versions/2", entry.getBaseUri().getPath());
    assertEquals("restPackage1", entry.getTitle());
    assertEquals("this is package restPackage1", entry.getSummary());
    //assertEquals(MediaType.APPLICATION_OCTET_STREAM_TYPE.getType(), entry.getContentMimeType().getPrimaryType());
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/versions/2/binary",
            entry.getContentSrc().getPath());

    List<Link> links = entry.getLinks();
    assertEquals(7, links.size());
    Map<String, Link> linksMap = new HashMap<String, Link>();
    for (Link link : links) {
        linksMap.put(link.getTitle(), link);
    }

    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/versions/2/assets/drools",
            linksMap.get("drools").getHref().getPath());
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/versions/2/assets/func",
            linksMap.get("func").getHref().getPath());
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/versions/2/assets/myDSL",
            linksMap.get("myDSL").getHref().getPath());
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/versions/2/assets/rule1",
            linksMap.get("rule1").getHref().getPath());
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/versions/2/assets/rule2",
            linksMap.get("rule2").getHref().getPath());
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/versions/2/assets/model1",
            linksMap.get("model1").getHref().getPath());

    ExtensibleElement metadataExtension = entry.getExtension(Translator.METADATA);
    ExtensibleElement archivedExtension = metadataExtension.getExtension(Translator.ARCHIVED);
    assertEquals("false", archivedExtension.getSimpleExtension(Translator.VALUE));
    ExtensibleElement uuidExtension = metadataExtension.getExtension(Translator.UUID);
    assertNotNull(uuidExtension.getSimpleExtension(Translator.VALUE));
    ExtensibleElement checkinCommentExtension = metadataExtension.getExtension(Translator.CHECKIN_COMMENT);
    assertEquals("version2", checkinCommentExtension.getSimpleExtension(Translator.VALUE));
    ExtensibleElement versionNumberExtension = metadataExtension.getExtension(Translator.VERSION_NUMBER);
    assertEquals("2", versionNumberExtension.getSimpleExtension(Translator.VALUE));
}

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

/**
 * Test of getPackageAsEntry method, of class PackageResource.
 * /*from  ww  w .  ja v  a2s . c om*/
 * An example of expected result:
 * 
 * <entry xmlns="http://www.w3.org/2005/Atom" xml:base="http://127.0.0.1:8080/1c71582d-3f64-4027-bc5a-d442ab4816e6/rest/packages/restPackage1"> 
 *   <title type="text">restPackage1</title> 
 *   <summary type="text">this is package restPackage1</summary>
 *   <published>2012-01-30T08:17:29.321Z</published> 
 *   <author>
 *     <name>guest</name> 
 *   </author>   
 *   <id>http://127.0.0.1:8080/1c71582d-3f64-4027-bc5a-d442ab4816e6/rest/packages/restPackage1</id> 
 *   
 *   <link href= "http://127.0.0.1:8080/1c71582d-3f64-4027-bc5a-d442ab4816e6/rest/packages/restPackage1/assets/drools" title="drools" rel="asset" /> 
 *   <link href= "http://127.0.0.1:8080/1c71582d-3f64-4027-bc5a-d442ab4816e6/rest/packages/ restPackage1/assets/func" title="func" rel="asset" /> 
 *   <link href= "http://127.0.0.1:8080/1c71582d-3f64-4027-bc5a-d442ab4816e6/rest/packages/restPackage1/assets/myDSL" title="myDSL" rel="asset" /> 
 *   <link href= "http://127.0.0.1:8080/1c71582d-3f64-4027-bc5a-d442ab4816e6/rest/packages/restPackage1/assets/rule1" title="rule1" rel="asset" /> 
 *   <link href= "http://127.0.0.1:8080/1c71582d-3f64-4027-bc5a-d442ab4816e6/rest/packages/restPackage1/assets/rule2" title="rule2" rel="asset" /> 
 *   <link href= "http://127.0.0.1:8080/1c71582d-3f64-4027-bc5a-d442ab4816e6/rest/packages/restPackage1/assets/model1" title="model1" rel="asset" />
 *   <link href= "http://127.0.0.1:8 080/1c71582d-3f64-4027-bc5a-d442ab4816e6/rest/packages/restPackage1/assets/rule4" title="rule4" rel="asset" /> 
 *   
 *   <metadata xmlns=""> 
 *     <archived>
 *       <value>false</value> 
 *     </archived>
 *     <uuid>
 *       <value>a2cabf64-bf84-4a42-83f0-1ce24702fc63</value> 
 *     </uuid> 
 *     <state>
 *       <value /> 
 *     </state> 
 *     <versionNumber>
 *       <value>3</value>
 *     </versionNumber>
 *     <checkinComment>
 *       <value>version3</value>
 *     </checkinComment>
 *   </metadata> 
 *   
 *   <content src= "http://127.0.0.1:8080/1c71582d-3 f64-4027-bc5a-d442ab4816e6/rest/packages/restPackage1/binary"/> 
 * </entry>
 */
@Test
@RunAsClient
public void testGetPackageForAtom(@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_ATOM_XML);
    connection.connect();
    assertEquals(200, connection.getResponseCode());
    assertEquals(MediaType.APPLICATION_ATOM_XML, connection.getContentType());
    //System.out.println("------------------------");
    //System.out.println(IOUtils.toString(connection.getInputStream()));

    InputStream in = connection.getInputStream();
    assertNotNull(in);
    Document<Entry> doc = abdera.getParser().parse(in);
    Entry entry = doc.getRoot();
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1", entry.getBaseUri().getPath());
    assertEquals("restPackage1", entry.getTitle());
    assertNotNull(entry.getPublished());
    assertNotNull(entry.getAuthor().getName());
    assertEquals("this is package restPackage1", entry.getSummary());
    //assertEquals(MediaType.APPLICATION_OCTET_STREAM_TYPE.getType(), entry.getContentMimeType().getPrimaryType());
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/binary", entry.getContentSrc().getPath());

    List<Link> links = entry.getLinks();
    assertEquals(7, links.size());
    Map<String, Link> linksMap = new HashMap<String, Link>();
    for (Link link : links) {
        linksMap.put(link.getTitle(), link);
    }

    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/assets/drools",
            linksMap.get("drools").getHref().getPath());
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/assets/func",
            linksMap.get("func").getHref().getPath());
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/assets/myDSL",
            linksMap.get("myDSL").getHref().getPath());
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/assets/rule1",
            linksMap.get("rule1").getHref().getPath());
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/assets/rule2",
            linksMap.get("rule2").getHref().getPath());
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/assets/rule4",
            linksMap.get("rule4").getHref().getPath());
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/assets/model1",
            linksMap.get("model1").getHref().getPath());

    ExtensibleElement metadataExtension = entry.getExtension(Translator.METADATA);
    ExtensibleElement archivedExtension = metadataExtension.getExtension(Translator.ARCHIVED);
    assertEquals("false", archivedExtension.getSimpleExtension(Translator.VALUE));
    ExtensibleElement uuidExtension = metadataExtension.getExtension(Translator.UUID);
    assertNotNull(uuidExtension.getSimpleExtension(Translator.VALUE));
    ExtensibleElement checkinCommentExtension = metadataExtension.getExtension(Translator.CHECKIN_COMMENT);
    assertEquals("version3", checkinCommentExtension.getSimpleExtension(Translator.VALUE));
    ExtensibleElement versionNumberExtension = metadataExtension.getExtension(Translator.VERSION_NUMBER);
    assertEquals("3", versionNumberExtension.getSimpleExtension(Translator.VALUE));
}

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

@Test
@RunAsClient//from   w  w  w.ja  v  a2  s .c  o  m
public void testGetHistoricalAssetForAtom(@ArquillianResource URL baseURL)
        throws MalformedURLException, IOException {
    URL url = new URL(baseURL, "rest/packages/restPackage1/assets/model1/versions/1");
    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<Entry> doc = abdera.getParser().parse(in);
    Entry entry = doc.getRoot();
    assertEquals("model1", entry.getTitle());
    assertTrue(entry.getPublished() != null);
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/assets/model1/versions/1",
            entry.getId().getPath());
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/assets/model1/versions/1/binary",
            entry.getContentSrc().getPath());
    ExtensibleElement metadataExtension = entry.getExtension(Translator.METADATA);
    ExtensibleElement formatExtension = metadataExtension.getExtension(Translator.FORMAT);
    assertEquals("model.drl", formatExtension.getSimpleExtension(Translator.VALUE));
    ExtensibleElement stateExtension = metadataExtension.getExtension(Translator.STATE);
    assertEquals("Draft", stateExtension.getSimpleExtension(Translator.VALUE));
    ExtensibleElement archivedExtension = metadataExtension.getExtension(Translator.ARCHIVED);
    assertEquals("false", archivedExtension.getSimpleExtension(Translator.VALUE));

    url = new URL(baseURL, "rest/packages/restPackage1/assets/model1/versions/2");
    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()));
    in = connection.getInputStream();

    assertNotNull(in);
    doc = abdera.getParser().parse(in);
    entry = doc.getRoot();
    assertEquals("model1", entry.getTitle());
    assertTrue(entry.getPublished() != null);
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/assets/model1/versions/2",
            entry.getId().getPath());
    assertEquals(baseURL.getPath() + "rest/packages/restPackage1/assets/model1/versions/2/binary",
            entry.getContentSrc().getPath());
    metadataExtension = entry.getExtension(Translator.METADATA);
    formatExtension = metadataExtension.getExtension(Translator.FORMAT);
    assertEquals("model.drl", formatExtension.getSimpleExtension(Translator.VALUE));
    stateExtension = metadataExtension.getExtension(Translator.STATE);
    assertEquals("Draft", stateExtension.getSimpleExtension(Translator.VALUE));
    archivedExtension = metadataExtension.getExtension(Translator.ARCHIVED);
    assertEquals("false", archivedExtension.getSimpleExtension(Translator.VALUE));

}

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

@Test
@RunAsClient//from  w w  w. ja va  2 s  .  c o  m
@Ignore("Verify this test once we get Arquillian working")
public void testGetAssetVersionsAfterUpdatingSource(@ArquillianResource URL baseURL)
        throws MalformedURLException, IOException {
    /*
     * check version feed
     */
    URL url = new URL(baseURL, "rest/packages/restPackage1/assets/rule4/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();
    int versionNumbers = entries.size();
    connection.disconnect();

    /*
     * update the content rule4
     */
    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("Content-Type", MediaType.APPLICATION_XML);
    connection2.setRequestProperty("Accept", MediaType.APPLICATION_XML);
    OutputStreamWriter out = new OutputStreamWriter(connection2.getOutputStream());
    String newContent = "rule 'nheron' when Goo1() then end";
    out.write(newContent);
    out.close();
    connection2.getInputStream();
    assertEquals(204, connection2.getResponseCode());

    /*
     * check version feed
     */
    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()));

    in = connection.getInputStream();
    assertNotNull(in);
    doc = abdera.getParser().parse(in);
    feed = doc.getRoot();
    assertEquals("Version history of model1", feed.getTitle());
    entries = feed.getEntries();
    assertEquals(versionNumbers + 1, entries.size());
}

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

@Test
@RunAsClient//www  .j av a 2 s  .c om
public void testRenamePackageFromAtom(@ArquillianResource URL baseURL) throws Exception {
    //create a package for testing
    Abdera abdera = new Abdera();
    AbderaClient client = new AbderaClient(abdera);
    client.addCredentials(baseURL.toExternalForm(), null, null,
            new org.apache.commons.httpclient.UsernamePasswordCredentials("admin", "admin"));
    Entry entry = abdera.newEntry();
    entry.setTitle("testRenamePackageFromAtom");
    entry.setSummary("desc for testRenamePackageFromAtom");

    ClientResponse resp = client.post(new URL(baseURL, "rest/packages").toExternalForm(), entry);
    //System.out.println(GetContent(resp.getInputStream()));
    assertEquals(ResponseType.SUCCESS, resp.getType());

    Document<Entry> doc = resp.getDocument();
    Entry returnedEntry = doc.getRoot();
    assertEquals(baseURL.getPath() + "rest/packages/testRenamePackageFromAtom",
            returnedEntry.getBaseUri().getPath());
    assertEquals("testRenamePackageFromAtom", returnedEntry.getTitle());
    assertEquals("desc for testRenamePackageFromAtom", returnedEntry.getSummary());

    //Test rename package
    Entry e = abdera.newEntry();
    e.setTitle("testRenamePackageFromAtomNew");
    org.apache.abdera.model.Link l = Abdera.getNewFactory().newLink();
    l.setHref(new URL(baseURL, "rest/packages/testRenamePackageFromAtomNew").toExternalForm());
    l.setRel("self");
    e.addLink(l);
    e.setSummary("renamed package testRenamePackageFromAtom");
    e.addAuthor("Test McTesty");
    resp = client.put(new URL(baseURL, "rest/packages/testRenamePackageFromAtom").toExternalForm(), e);
    assertEquals(ResponseType.SUCCESS, resp.getType());
    assertEquals(204, resp.getStatus());

    //Verify new package is available after renaming
    URL url1 = new URL(baseURL, "rest/packages/testRenamePackageFromAtomNew");
    HttpURLConnection conn1 = (HttpURLConnection) url1.openConnection();
    conn1.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    conn1.setRequestMethod("GET");
    conn1.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML);
    conn1.connect();
    //System.out.println(GetContent(conn));
    assertEquals(200, conn1.getResponseCode());
    assertEquals(MediaType.APPLICATION_ATOM_XML, conn1.getContentType());

    InputStream in = conn1.getInputStream();
    assertNotNull(in);
    doc = abdera.getParser().parse(in);
    entry = doc.getRoot();
    assertEquals(baseURL.getPath() + "rest/packages/testRenamePackageFromAtomNew",
            entry.getBaseUri().getPath());
    assertEquals("testRenamePackageFromAtomNew", entry.getTitle());
    assertTrue(entry.getPublished() != null);
    assertEquals("renamed package testRenamePackageFromAtom", entry.getSummary());

    //Verify the old package does not exist after renaming
    URL url2 = new URL(baseURL, "rest/packages/testRenamePackageFromAtom");
    HttpURLConnection conn2 = (HttpURLConnection) url2.openConnection();
    conn2.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    conn2.setRequestMethod("GET");
    conn2.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML);
    conn2.connect();
    //System.out.println(IOUtils.toString(connection.getInputStream()));
    assertEquals(404, conn2.getResponseCode());

    //Roll back changes. 
    resp = client.delete(new URL(baseURL, "rest/packages/testRenamePackageFromAtomNew").toExternalForm());
    assertEquals(ResponseType.SUCCESS, resp.getType());

    //Verify the package is indeed deleted
    URL url3 = new URL(baseURL, "rest/packages/testRenamePackageFromAtomNew");
    HttpURLConnection conn3 = (HttpURLConnection) url3.openConnection();
    conn3.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    conn3.setRequestMethod("GET");
    conn3.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML);
    conn3.connect();
    //System.out.println(IOUtils.toString(connection.getInputStream()));
    assertEquals(404, conn3.getResponseCode());
}

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

@Test
@RunAsClient/* ww  w  .  j av a  2 s .  c o m*/
@Ignore
public void testCreatePackageFromDRLAsJaxB(@ArquillianResource URL baseURL) throws Exception {
    URL url = new URL(baseURL, "rest/packages");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", MediaType.APPLICATION_OCTET_STREAM);
    connection.setRequestProperty("Accept", MediaType.APPLICATION_XML);
    connection.setDoOutput(true);

    //Send request
    BufferedReader br = new BufferedReader(
            new InputStreamReader(getClass().getResourceAsStream("simple_rules3.drl")));
    DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
    while (br.ready())
        dos.writeBytes(br.readLine());
    dos.flush();
    dos.close();

    assertEquals(200, connection.getResponseCode());
    assertEquals(MediaType.APPLICATION_XML, connection.getContentType());
    //logger.log(LogLevel, IOUtils.toString(connection.getInputStream()));
}

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

@Test
@RunAsClient//w w  w.j a va  2  s .  c  om
public void testCreateAndUpdateAndDeletePackageFromAtom(@ArquillianResource URL baseURL) throws Exception {
    //Test create
    Abdera abdera = new Abdera();
    AbderaClient client = new AbderaClient(abdera);
    client.addCredentials(baseURL.toExternalForm(), null, null,
            new org.apache.commons.httpclient.UsernamePasswordCredentials("admin", "admin"));
    Entry entry = abdera.newEntry();
    entry.setTitle("testCreatePackageFromAtom");
    entry.setSummary("desc for testCreatePackageFromAtom");
    ExtensibleElement extension = entry.addExtension(Translator.METADATA);
    ExtensibleElement childExtension = extension.addExtension(Translator.CHECKIN_COMMENT);
    childExtension.addSimpleExtension(Translator.VALUE,
            "checkin comment:initial desc for testCreatePackageFromAtom");

    ClientResponse resp = client.post(new URL(baseURL, "rest/packages").toExternalForm(), entry);
    //System.out.println(GetContent(resp.getInputStream()));

    assertEquals(ResponseType.SUCCESS, resp.getType());

    Document<Entry> doc = resp.getDocument();
    Entry returnedEntry = doc.getRoot();
    assertEquals(baseURL.getPath() + "rest/packages/testCreatePackageFromAtom",
            returnedEntry.getBaseUri().getPath());
    assertEquals("testCreatePackageFromAtom", returnedEntry.getTitle());
    assertEquals("desc for testCreatePackageFromAtom", returnedEntry.getSummary());
    ExtensibleElement metadataExtension = entry.getExtension(Translator.METADATA);
    ExtensibleElement checkinCommentExtension = metadataExtension.getExtension(Translator.CHECKIN_COMMENT);
    assertEquals("checkin comment:initial desc for testCreatePackageFromAtom",
            checkinCommentExtension.getSimpleExtension(Translator.VALUE));

    //Test update package
    Entry e = abdera.newEntry();
    e.setTitle("testCreatePackageFromAtom");
    org.apache.abdera.model.Link l = Abdera.getNewFactory().newLink();
    l.setHref(new URL(baseURL, "rest/packages/testCreatePackageFromAtom").toExternalForm());
    l.setRel("self");
    e.addLink(l);
    e.setSummary("updated desc for testCreatePackageFromAtom");
    e.addAuthor("Test McTesty");
    extension = e.addExtension(Translator.METADATA);
    childExtension = extension.addExtension(Translator.CHECKIN_COMMENT);
    childExtension.addSimpleExtension(Translator.VALUE,
            "checkin comment:updated desc for testCreatePackageFromAtom");
    resp = client.put(new URL(baseURL, "rest/packages/testCreatePackageFromAtom").toExternalForm(), e);
    assertEquals(ResponseType.SUCCESS, resp.getType());
    assertEquals(204, resp.getStatus());

    //NOTE: could not figure out why the code below always returns -1 as the ResponseCode.
    /*        URL url = new URL(baseURL, "rest/packages/testCreatePackageFromAtom");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("Authorization",
        "Basic " + new Base64().encodeToString(( "admin:admin".getBytes() )));
            conn.setRequestMethod("PUT");
            conn.setRequestProperty("Content-type", MediaType.APPLICATION_ATOM_XML);
            conn.setRequestProperty("Content-Length", Integer.toString(e.toString().getBytes().length));
            conn.setDoOutput(true);
            e.writeTo(conn.getOutputStream());
            assertEquals(204, conn.getResponseCode());
            conn.disconnect(); */

    URL url1 = new URL(baseURL, "rest/packages/testCreatePackageFromAtom");
    HttpURLConnection conn1 = (HttpURLConnection) url1.openConnection();
    conn1.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    conn1.setRequestMethod("GET");
    conn1.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML);
    conn1.connect();
    //System.out.println(GetContent(conn));
    assertEquals(200, conn1.getResponseCode());
    assertEquals(MediaType.APPLICATION_ATOM_XML, conn1.getContentType());

    InputStream in = conn1.getInputStream();
    assertNotNull(in);
    doc = abdera.getParser().parse(in);
    entry = doc.getRoot();
    assertEquals(baseURL.getPath() + "rest/packages/testCreatePackageFromAtom", entry.getBaseUri().getPath());
    assertEquals("testCreatePackageFromAtom", entry.getTitle());
    assertNotNull(entry.getPublished());
    assertNotNull(entry.getAuthor().getName());
    assertEquals("updated desc for testCreatePackageFromAtom", entry.getSummary());
    metadataExtension = entry.getExtension(Translator.METADATA);
    checkinCommentExtension = metadataExtension.getExtension(Translator.CHECKIN_COMMENT);
    assertEquals("checkin comment:updated desc for testCreatePackageFromAtom",
            checkinCommentExtension.getSimpleExtension(Translator.VALUE));

    //Roll back changes. 
    resp = client.delete(new URL(baseURL, "rest/packages/testCreatePackageFromAtom").toExternalForm());
    assertEquals(ResponseType.SUCCESS, resp.getType());

    //Verify the package is indeed deleted
    URL url2 = new URL(baseURL, "rest/packages/testCreatePackageFromAtom");
    HttpURLConnection conn2 = (HttpURLConnection) url2.openConnection();
    conn2.setRequestProperty("Authorization",
            "Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
    conn2.setRequestMethod("GET");
    conn2.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML);
    conn2.connect();
    //System.out.println(IOUtils.toString(connection.getInputStream()));
    assertEquals(404, conn2.getResponseCode());
}