List of usage examples for java.net HttpURLConnection getContentType
public String getContentType()
From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceTest.java
/** * Test of getPackagesAsFeed method, of class PackageService. */// w w w. ja va 2 s. c om @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(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)); }
From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceTest.java
@Test @RunAsClient/*w w w. j a v a 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("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.BasicPackageResourceTest.java
@Test @RunAsClient//w ww . j a va2 s. c o m @Ignore("verify this test indeed works once we get Arquillian working") 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("testCreatePackageFromAtom", returnedEntry.getTitle()); assertEquals("desc for testCreatePackageFromAtom", 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 testCreatePackageFromAtom"); e.addAuthor("Test McTesty"); resp = client.put(new URL(baseURL, "rest/packages/testCreatePackageFromAtom").toExternalForm(), e); assertEquals(ResponseType.SUCCESS, resp.getType()); assertEquals(204, resp.getStatus()); //Verify everything still works 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 testCreatePackageFromAtom", entry.getSummary()); //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 url2 = new URL(baseURL, "rest/packages/testRenamePackageFromAtomNew"); 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(500, conn2.getResponseCode()); }
From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceTest.java
@Test @RunAsClient/*from w w w. j a v a 2s .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"); 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()); //Test update package Entry e = abdera.newEntry(); e.setTitle("testUpdatePackageFromAtom"); 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"); 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()); assertTrue(entry.getPublished() != null); assertEquals("updated desc for testCreatePackageFromAtom", entry.getSummary()); //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(500, conn2.getResponseCode()); }
From source file:org.ejbca.core.protocol.cmp.CmpTestCase.java
protected byte[] sendCmpHttp(byte[] message, int httpRespCode, String cmpAlias) throws IOException { // POST the CMP request // we are going to do a POST final String urlString = getProperty("httpCmpProxyURL", this.httpReqPath + '/' + resourceCmp) + '/' + cmpAlias;//from w ww. j a v a 2 s. c o m log.info("http URL: " + urlString); URL url = new URL(urlString); final HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoOutput(true); con.setRequestMethod("POST"); con.setRequestProperty("Content-type", "application/pkixcmp"); con.connect(); // POST it OutputStream os = con.getOutputStream(); os.write(message); os.close(); assertEquals("Unexpected HTTP response code.", httpRespCode, con.getResponseCode()); // Only try to read the response if we expected a 200 (ok) response if (httpRespCode != 200) { return null; } // Some appserver (Weblogic) responds with // "application/pkixcmp; charset=UTF-8" assertNotNull("No content type in response.", con.getContentType()); assertTrue(con.getContentType().startsWith("application/pkixcmp")); // Check that the CMP respone has the cache-control headers as specified in // http://tools.ietf.org/html/draft-ietf-pkix-cmp-transport-protocols-14 final String cacheControl = con.getHeaderField("Cache-Control"); assertNotNull("'Cache-Control' header is not present.", cacheControl); assertEquals("no-cache", cacheControl); final String pragma = con.getHeaderField("Pragma"); assertNotNull(pragma); assertEquals("no-cache", pragma); // Now read in the bytes ByteArrayOutputStream baos = new ByteArrayOutputStream(); // This works for small requests, and CMP requests are small enough InputStream in = con.getInputStream(); int b = in.read(); while (b != -1) { baos.write(b); b = in.read(); } baos.flush(); in.close(); byte[] respBytes = baos.toByteArray(); assertNotNull(respBytes); assertTrue(respBytes.length > 0); return respBytes; }
From source file:org.ejbca.core.protocol.scep.ProtocolScepHttpTest.java
private byte[] sendScep(boolean post, byte[] scepPackage, int responseCode) throws IOException { // POST the SCEP request // we are going to do a POST String urlString = httpReqPath + '/' + resourceScep + "?operation=PKIOperation"; log.debug("UrlString =" + urlString); final HttpURLConnection con; if (post) {//from w w w .j a v a 2 s . c o m URL url = new URL(urlString); con = (HttpURLConnection) url.openConnection(); con.setDoOutput(true); con.setRequestMethod("POST"); con.connect(); // POST it OutputStream os = con.getOutputStream(); os.write(scepPackage); os.close(); } else { String reqUrl = urlString + "&message=" + URLEncoder.encode(new String(Base64.encode(scepPackage)), "UTF-8"); URL url = new URL(reqUrl); con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.getDoOutput(); con.connect(); } assertEquals("Response code", responseCode, con.getResponseCode()); // Some appserver (Weblogic) responds with // "application/x-pki-message; charset=UTF-8" if (responseCode == HttpServletResponse.SC_OK) { assertTrue(con.getContentType().startsWith("application/x-pki-message")); } else { assertTrue(con.getContentType().startsWith("text/html")); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); // This works for small requests, and SCEP requests are small enough final InputStream in; if (responseCode == HttpServletResponse.SC_OK) { in = con.getInputStream(); } else { in = con.getErrorStream(); } int b = in.read(); while (b != -1) { baos.write(b); b = in.read(); } baos.flush(); in.close(); byte[] respBytes = baos.toByteArray(); assertNotNull("Response can not be null.", respBytes); assertTrue(respBytes.length > 0); return respBytes; }
From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceTest.java
@Test @RunAsClient/* ww w. jav a 2 s. c o m*/ 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.setRequestProperty("Authorization", "Basic " + new Base64().encodeToString(("admin:admin".getBytes()))); 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.ejbca.core.protocol.ocsp.ProtocolOcspHttpTest.java
/** * Just verify that a simple GET works.// w w w. j a va 2s . c o m */ @Test public void test13GetRequests() throws Exception { // See if the OCSP Servlet can read non-encoded requests final String plainReq = httpReqPath + '/' + resourceOcsp + '/' + "MGwwajBFMEMwQTAJBgUrDgMCGgUABBRBRfilzPB+Aevx0i1AoeKTkrHgLgQUFJw5gwk9BaEgsX3pzsRF9iso29ICCCzdx5N0v9XwoiEwHzAdBgkrBgEFBQcwAQIEECrZswo/a7YW+hyi5Sn85fs="; URL url = new URL(plainReq); log.info(url.toString()); // Dump the exact string we use for access HttpURLConnection con = (HttpURLConnection) url.openConnection(); assertEquals("Response code did not match. ", 200, con.getResponseCode()); assertNotNull(con.getContentType()); assertTrue(con.getContentType().startsWith("application/ocsp-response")); OCSPResp response = new OCSPResp(IOUtils.toByteArray(con.getInputStream())); assertNotNull("Response should not be null.", response); assertTrue("Should not be considered malformed.", OCSPRespBuilder.MALFORMED_REQUEST != response.getStatus()); final String dubbleSlashNonEncReq = httpReqPath + '/' + resourceOcsp + '/' + "MGwwajBFMEMwQTAJBgUrDgMCGgUABBRBRfilzPB%2BAevx0i1AoeKTkrHgLgQUFJw5gwk9BaEgsX3pzsRF9iso29ICCAvB//HJyKqpoiEwHzAdBgkrBgEFBQcwAQIEEOTzT2gv3JpVva22Vj8cuKo%3D"; url = new URL(dubbleSlashNonEncReq); log.info(url.toString()); // Dump the exact string we use for access con = (HttpURLConnection) url.openConnection(); assertEquals("Response code did not match. ", 200, con.getResponseCode()); assertNotNull(con.getContentType()); assertTrue(con.getContentType().startsWith("application/ocsp-response")); response = new OCSPResp(IOUtils.toByteArray(con.getInputStream())); assertNotNull("Response should not be null.", response); assertTrue("Should not be concidered malformed.", OCSPRespBuilder.MALFORMED_REQUEST != response.getStatus()); // An OCSP request, ocspTestCert is already created in earlier tests loadUserCert(this.caid); this.helper.reloadKeys(); this.helper.verifyStatusGood(this.caid, this.cacert, this.ocspTestCert.getSerialNumber()); }
From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceIntegrationTest.java
@Test @RunAsClient//from w ww. j a v a2 s . com public void testGetHistoricalPackageSource(@ArquillianResource URL baseURL) throws Exception { URL url = new URL(baseURL, "rest/packages/restPackage1/versions/2/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("package restPackage1")); assertTrue(result.contains("import org.drools.Cheese")); assertTrue(result.contains("global org.drools.Person customer1")); assertTrue(result.contains("function void foo() { System.out.println(\"version 1\"); }")); assertTrue(result.contains("declare Album1")); }
From source file:org.drools.guvnor.server.jaxrs.BasicPackageResourceIntegrationTest.java
@Test @RunAsClient// w w w . j av a 2s. com public void testGetPackageSource(@ArquillianResource URL baseURL) throws Exception { URL url = new URL(baseURL, "rest/packages/restPackage1/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()); assertEquals("attachment; filename=restPackage1", connection.getHeaderField("Content-Disposition")); assertTrue(result.indexOf("package restPackage1") >= 0); assertTrue(result.indexOf("import org.drools.Cheese") >= 0); assertTrue(result.indexOf("global org.drools.Person customer2") >= 0); assertTrue(result.indexOf("function void foo() { System.out.println(\"version 2\"); }") >= 0); assertTrue(result.indexOf("declare Album2") >= 0); }