Example usage for java.net HttpURLConnection getDoOutput

List of usage examples for java.net HttpURLConnection getDoOutput

Introduction

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

Prototype

public boolean getDoOutput() 

Source Link

Document

Returns the value of this URLConnection 's doOutput flag.

Usage

From source file:org.ejbca.core.protocol.scep.ProtocolScepHttpTest.java

@Test
public void test02AccessTest() throws Exception {

    ScepConfiguration scepConfig = (ScepConfiguration) globalConfigSession
            .getCachedConfiguration(ScepConfiguration.SCEP_CONFIGURATION_ID);
    boolean remove = false;
    if (!scepConfig.aliasExists("scep")) {
        scepConfig.addAlias("scep");
        globalConfigSession.saveConfiguration(admin, scepConfig);
        remove = true;/*from w  w w .  ja  va 2 s. c  o m*/
    }

    String resourceName = "/ejbca/publicweb/apply/scep/pkiclient.exe?operation=GetCACert&message="
            + x509ca.getName();
    String httpHost = SystemTestsConfiguration.getRemoteHost("127.0.0.1");
    String httpPort = SystemTestsConfiguration.getRemotePortHttp(
            configurationSessionRemote.getProperty(WebConfiguration.CONFIG_HTTPSERVERPUBHTTP));
    String httpBaseUrl = "http://" + httpHost + ":" + httpPort;

    String url = httpBaseUrl + resourceName;
    final HttpURLConnection con;
    URL u = new URL(url);
    con = (HttpURLConnection) u.openConnection();
    con.setRequestMethod("GET");
    con.getDoOutput();
    con.connect();

    int ret = con.getResponseCode();
    log.debug("HTTP response code: " + ret);
    if (ret == 200) {
        log.debug(Streams.asString(con.getInputStream()));
    }
    con.disconnect();

    if (remove) {
        scepConfig.removeAlias("scep");
        globalConfigSession.saveConfiguration(admin, scepConfig);
    }
    assertEquals("HTTP GET is not supported. (This test expects " + httpBaseUrl + resourceName + " to exist)",
            200, ret);

}

From source file:org.ejbca.core.protocol.scep.ProtocolScepHttpTest.java

@Test
public void test08ScepGetCACert() throws Exception {
    {/*w  w  w.j  ava2  s . co  m*/
        String reqUrl = httpReqPath + '/' + resourceScep + "?operation=GetCACert&message="
                + URLEncoder.encode(x509ca.getName(), "UTF-8");
        URL url = new URL(reqUrl);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.getDoOutput();
        con.connect();
        assertEquals("Response code is not 200 (OK)", 200, con.getResponseCode());
        // Some appserver (Weblogic) responds with
        // "application/x-x509-ca-cert; charset=UTF-8"
        assertTrue(con.getContentType().startsWith("application/x-x509-ca-cert"));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // This works for small requests, and SCEP 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("Response can not be null.", respBytes);
        assertTrue(respBytes.length > 0);
        X509Certificate cert = (X509Certificate) CertTools.getCertfromByteArray(respBytes);
        // Check that we got the right cert back
        assertEquals(cacert.getSubjectDN().getName(), cert.getSubjectDN().getName());
    }

    // 
    // Test the same message but without message component, it should use a default CA
    {
        // Try with a non extisting CA first, should respond with a 404
        updatePropertyOnServer("scep.defaultca", "NonExistingCAForSCEPTest");
        String reqUrl = httpReqPath + '/' + resourceScep + "?operation=GetCACert";
        URL url = new URL(reqUrl);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.getDoOutput();
        con.connect();
        assertEquals("Response code is not 404 (not found)", 404, con.getResponseCode());
        // Try with the good CA            
        updatePropertyOnServer("scep.defaultca", x509ca.getName());
        con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.getDoOutput();
        con.connect();
        assertEquals("Response code is not 200 (OK)", 200, con.getResponseCode());
        // Some appserver (Weblogic) responds with
        // "application/x-x509-ca-cert; charset=UTF-8"
        assertTrue(con.getContentType().startsWith("application/x-x509-ca-cert"));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // This works for small requests, and SCEP 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("Response can not be null.", respBytes);
        assertTrue(respBytes.length > 0);
        X509Certificate cert = (X509Certificate) CertTools.getCertfromByteArray(respBytes);
        // Check that we got the right cert back
        assertEquals(cacert.getSubjectDN().getName(), cert.getSubjectDN().getName());
    }
}

From source file:org.ejbca.core.protocol.scep.ProtocolScepHttpTest.java

@Test
public void test10ScepGetCACaps() throws Exception {
    String reqUrl = httpReqPath + '/' + resourceScep + "?operation=GetCACaps&message="
            + URLEncoder.encode(x509ca.getName(), "UTF-8");
    URL url = new URL(reqUrl);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.getDoOutput();
    con.connect();/*from ww w.ja  v a 2 s  . c om*/
    assertEquals("Response code", 200, con.getResponseCode());
    // Some appserver (Weblogic) responds with "text/plain; charset=UTF-8"
    assertTrue(con.getContentType().startsWith("text/plain"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // This works for small requests, and SCEP 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("Response can not be null.", respBytes);
    assertTrue(respBytes.length > 0);
    assertEquals(new String(respBytes), "POSTPKIOperation\nRenewal\nSHA-1");
}

From source file:org.ejbca.core.protocol.scep.ProtocolScepHttpTest.java

/**
 * Sends a SCEP request with the alias requestAlias in the URL and expects a SCEP error message 
 * that can inform us about the alias extracted from the URL.
 *///ww w .  j  a v a  2 s.  c o m
private void sendScepAliasRequest(ScepConfiguration scepConfig, String requestAlias, String extractedAlias,
        String expectedErrMsg) throws Exception {

    if (extractedAlias != null) {
        if (scepConfig.aliasExists(extractedAlias)) {
            scepConfig.renameAlias(extractedAlias,
                    "backUpAlias" + extractedAlias + "ForAliasTesting001122334455");
            globalConfigSession.saveConfiguration(admin, scepConfig);
        }
    }

    try {
        String resource = "publicweb/apply/scep/" + (requestAlias != null ? requestAlias + "/" : "")
                + "pkiclient.exe";
        String urlString = httpReqPath + '/' + resource + "?operation=PKIOperation";
        log.info("http URL: " + urlString);

        String reqUrl = urlString + "&message=" + URLEncoder.encode("Test Scep message", "UTF-8");
        URL url = new URL(reqUrl);
        final HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.getDoOutput();
        con.connect();
        assertEquals("Unexpected HTTP response code.", HttpServletResponse.SC_BAD_REQUEST,
                con.getResponseCode()); // OK response (will use alias "alias123")

        InputStream err = con.getErrorStream();
        byte[] errB = new byte[1024];
        err.read(errB);
        err.close();
        String response = new String(errB);
        assertTrue("Response does not contain the correct error message",
                StringUtils.contains(response, expectedErrMsg));
    } finally {
        if (extractedAlias != null) {
            if (scepConfig.aliasExists("backUpAlias" + extractedAlias + "ForAliasTesting001122334455")) {
                scepConfig.renameAlias("backUpAlias" + extractedAlias + "ForAliasTesting001122334455",
                        extractedAlias);
                globalConfigSession.saveConfiguration(admin, scepConfig);
            }
        }
    }
}

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 2s .  com*/
        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.ejbca.ui.web.pub.cluster.WebEjbcaHealthCheckTest.java

/** Returns a connected HttpURLConnection, that must be closed with con.diconnect() when you are done 
 * //from  w w w  .  ja va2 s .  c  o m
 * @param url The URL to connect to 
 * @return HttpURLConnection that you can use
 * @throws IOException In case the connection can not be opened
 */
private HttpURLConnection getHttpURLConnection(String url) throws IOException {
    final HttpURLConnection con;
    URL u = new URL(url);
    con = (HttpURLConnection) u.openConnection();
    con.setRequestMethod("GET");
    con.getDoOutput();
    con.connect();
    return con;
}