Example usage for java.net HttpURLConnection getHeaderField

List of usage examples for java.net HttpURLConnection getHeaderField

Introduction

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

Prototype

public String getHeaderField(int n) 

Source Link

Document

Returns the value for the n th header field.

Usage

From source file:com.google.ytd.picasa.PicasaApiHelper.java

public String getResumableUploadUrl(com.google.ytd.model.PhotoEntry photoEntry, String title,
        String description, String albumId, Double latitude, Double longitude) throws IllegalArgumentException {
    LOG.info(String.format("Resumable upload request.\nTitle: %s\nDescription: %s\nAlbum: %s", title,
            description, albumId));/*from   w w  w  . j a v  a2s  .  c om*/

    // Picasa API resumable uploads are not currently documented publicly, but they're essentially
    // the same as what YouTube API offers:
    // http://code.google.com/apis/youtube/2.0/developers_guide_protocol_resumable_uploads.html
    // The Java client library does offer support for resumable uploads, but its use of threads
    // and some other assumptions makes it unsuitable for our purposes.
    try {
        URL url = new URL(String.format(RESUMABLE_UPLOADS_URL_FORMAT, albumId));
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setConnectTimeout(CONNECT_TIMEOUT);
        connection.setReadTimeout(READ_TIMEOUT);
        connection.setRequestMethod("POST");

        // Set all the GData request headers. These strings should probably be moved to CONSTANTS.
        connection.setRequestProperty("Content-Type", "application/atom+xml;charset=UTF-8");
        connection.setRequestProperty("Authorization",
                String.format("AuthSub token=\"%s\"", adminConfigDao.getAdminConfig().getPicasaAuthSubToken()));
        connection.setRequestProperty("GData-Version", "2.0");
        connection.setRequestProperty("Slug", photoEntry.getOriginalFileName());
        connection.setRequestProperty("X-Upload-Content-Type", photoEntry.getFormat());
        connection.setRequestProperty("X-Upload-Content-Length",
                String.valueOf(photoEntry.getOriginalFileSize()));

        // If we're given lat/long then create the element to geotag the picture; otherwise, pass in
        // and empty string for no geotag.
        String geoRss = "";
        if (latitude != null && longitude != null) {
            geoRss = String.format(GEO_RSS_XML_FORMAT, latitude, longitude);
            LOG.info("Geo RSS XML: " + geoRss);
        }

        String atomXml = String.format(UPLOAD_ENTRY_XML_FORMAT, StringEscapeUtils.escapeXml(title),
                StringEscapeUtils.escapeXml(description), geoRss);

        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(atomXml);
        writer.close();

        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            String uploadUrl = connection.getHeaderField("Location");
            if (util.isNullOrEmpty(uploadUrl)) {
                throw new IllegalArgumentException("No Location header found in HTTP response.");
            } else {
                LOG.info("Resumable upload URL is " + uploadUrl);

                return uploadUrl;
            }
        } else {
            LOG.warning(String.format("HTTP POST to %s returned status %d (%s).", url.toString(),
                    connection.getResponseCode(), connection.getResponseMessage()));
        }
    } catch (MalformedURLException e) {
        LOG.log(Level.WARNING, "", e);

        throw new IllegalArgumentException(e);
    } catch (IOException e) {
        LOG.log(Level.WARNING, "", e);
    }

    return null;
}

From source file:org.oclc.oai.harvester2.verb.HarvesterVerb.java

/**
 * Preforms the OAI request, recovering from typical XML error
 *
 * @author nfreire Nuno Freire / Gilberto Pedrosa
 * @param requestURL/*from w  ww  .  j av a  2s.com*/
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerException
 */
private void harvest(String requestURL)
        throws IOException, ParserConfigurationException, SAXException, TransformerException {
    this.requestURL = requestURL;
    logger.debug("requestURL=" + requestURL);
    InputStream in;
    URL url = new URL(requestURL);
    HttpURLConnection con;
    int responseCode;
    do {
        con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(30000);
        con.setReadTimeout(600000);

        if (con.getAllowUserInteraction()) {
            con.setRequestProperty("User-Agent", "OAIHarvester/2.0");
            con.setRequestProperty("Accept-Encoding", "compress, gzip, identify");
        }
        try {
            responseCode = con.getResponseCode();
            logger.debug("responseCode=" + responseCode);
        } catch (FileNotFoundException e) {
            // assume it's a 503 response
            logger.error(requestURL, e);
            responseCode = HttpURLConnection.HTTP_UNAVAILABLE;
        }

        if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) {
            long retrySeconds = con.getHeaderFieldInt("Retry-After", -1);
            if (retrySeconds == -1) {
                long now = (new Date()).getTime();
                long retryDate = con.getHeaderFieldDate("Retry-After", now);
                retrySeconds = retryDate - now;
            }
            if (retrySeconds == 0) { // Apparently, it's a bad URL
                throw new FileNotFoundException("Bad URL?");
            }
            logger.warn("Server response: Retry-After=" + retrySeconds);
            if (retrySeconds > 0) {
                try {
                    Thread.sleep(retrySeconds * 1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    } while (responseCode == HttpURLConnection.HTTP_UNAVAILABLE);
    String contentEncoding = con.getHeaderField("Content-Encoding");
    logger.debug("contentEncoding=" + contentEncoding);
    if ("compress".equals(contentEncoding)) {
        ZipInputStream zis = new ZipInputStream(con.getInputStream());
        zis.getNextEntry();
        in = zis;
    } else if ("gzip".equals(contentEncoding)) {
        in = new GZIPInputStream(con.getInputStream());
    } else if ("deflate".equals(contentEncoding)) {
        in = new InflaterInputStream(con.getInputStream());
    } else {
        in = con.getInputStream();
    }

    byte[] inputBytes = IOUtils.toByteArray(in);
    InputSource data = new InputSource(new ByteArrayInputStream(inputBytes));

    Thread t = Thread.currentThread();
    DocumentBuilder builder = (DocumentBuilder) builderMap.get(t);
    if (builder == null) {
        builder = factory.newDocumentBuilder();
        builderMap.put(t, builder);
    }
    try {
        doc = builder.parse(data);
    } catch (SAXException e) {
        try {
            //Here we can try to recover the xml from known typical problems

            //Recover from invalid characters
            //we assume this is UTF-8...
            String xmlString = new String(inputBytes, "UTF-8");
            xmlString = XmlUtil.removeInvalidXMLCharacters(xmlString);

            data = new InputSource(new ByteArrayInputStream(xmlString.getBytes("UTF-8")));
            doc = builder.parse(data);
        } catch (Exception e2) {
            //the recovered version did not work either. Throw the original exception
            throw e;
        }
    } catch (IOException e3) {
        System.out.println("e = " + e3.getMessage());
    } catch (Exception e4) {
        System.out.println("e = " + e4.getMessage());
    }

    StringTokenizer tokenizer = new StringTokenizer(getSingleString("/*/@xsi:schemaLocation"), " ");
    StringBuffer sb = new StringBuffer();
    while (tokenizer.hasMoreTokens()) {
        if (sb.length() > 0)
            sb.append(" ");
        sb.append(tokenizer.nextToken());
    }
    this.schemaLocation = sb.toString();
    this.defaultNamespace = getDocument().getDocumentElement().getNamespaceURI();
}

From source file:org.oclc.oai.harvester.verb.HarvesterVerb.java

/**
 * Performs the OAI request, recovering from typical XML error
 * //from  w  w w .  j a  v a  2  s. com
 * @author nfreire Nuno Freire / Gilberto Pedrosa
 * @param requestURL
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerException
 */
private void harvest(String requestURL)
        throws IOException, ParserConfigurationException, SAXException, TransformerException {
    this.requestURL = requestURL;
    logger.debug("requestURL=" + requestURL);
    InputStream in;
    URL url = new URL(requestURL);
    HttpURLConnection con;
    int responseCode;
    do {
        con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(30000);
        con.setReadTimeout(600000);

        if (con.getAllowUserInteraction()) {
            con.setRequestProperty("User-Agent", "OAIHarvester/2.0");
            con.setRequestProperty("Accept-Encoding", "compress, gzip, identify");
        }
        try {
            responseCode = con.getResponseCode();
            logger.debug("responseCode=" + responseCode);
        } catch (FileNotFoundException e) {
            // assume it's a 503 response
            logger.error(requestURL, e);
            responseCode = HttpURLConnection.HTTP_UNAVAILABLE;
        }

        if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) {
            long retrySeconds = con.getHeaderFieldInt("Retry-After", -1);
            if (retrySeconds == -1) {
                long now = (new Date()).getTime();
                long retryDate = con.getHeaderFieldDate("Retry-After", now);
                retrySeconds = retryDate - now;
            }
            if (retrySeconds == 0) { // Apparently, it's a bad URL
                throw new FileNotFoundException("Bad URL?");
            }
            logger.warn("Server response: Retry-After=" + retrySeconds);
            if (retrySeconds > 0) {
                try {
                    Thread.sleep(retrySeconds * 1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    } while (responseCode == HttpURLConnection.HTTP_UNAVAILABLE);
    String contentEncoding = con.getHeaderField("Content-Encoding");
    logger.debug("contentEncoding=" + contentEncoding);
    if ("compress".equals(contentEncoding)) {
        ZipInputStream zis = new ZipInputStream(con.getInputStream());
        zis.getNextEntry();
        in = zis;
    } else if ("gzip".equals(contentEncoding)) {
        in = new GZIPInputStream(con.getInputStream());
    } else if ("deflate".equals(contentEncoding)) {
        in = new InflaterInputStream(con.getInputStream());
    } else {
        in = con.getInputStream();
    }

    byte[] inputBytes = IOUtils.toByteArray(in);
    InputSource data = new InputSource(new ByteArrayInputStream(inputBytes));

    Thread t = Thread.currentThread();
    DocumentBuilder builder = builderMap.get(t);
    if (builder == null) {
        builder = factory.newDocumentBuilder();
        builderMap.put(t, builder);
    }
    try {
        doc = builder.parse(data);
    } catch (SAXException e) {
        try {
            //Here we can try to recover the xml from known typical problems

            //Recover from invalid characters
            //we assume this is UTF-8...
            String xmlString = new String(inputBytes, "UTF-8");
            xmlString = XmlUtil.removeInvalidXMLCharacters(xmlString);

            data = new InputSource(new ByteArrayInputStream(xmlString.getBytes("UTF-8")));
            doc = builder.parse(data);
        } catch (Exception e2) {
            //the recovered version did not work either. Throw the original exception
            throw e;
        }
    } catch (IOException e3) {
        System.out.println("e = " + e3.getMessage());
    } catch (Exception e4) {
        System.out.println("e = " + e4.getMessage());
    }

    StringTokenizer tokenizer = new StringTokenizer(getSingleString("/*/@xsi:schemaLocation"), " ");
    StringBuffer sb = new StringBuffer();
    while (tokenizer.hasMoreTokens()) {
        if (sb.length() > 0)
            sb.append(" ");
        sb.append(tokenizer.nextToken());
    }
    this.schemaLocation = sb.toString();
    this.defaultNamespace = getDocument().getDocumentElement().getNamespaceURI();
}

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

@Test
@RunAsClient/*from  ww  w .j  a v a2s  .c o m*/
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);
}

From source file:org.callimachusproject.test.WebResource.java

private WebResource findLink(String rel, boolean rev, String... types)
        throws IOException, MalformedURLException, ProtocolException {
    HttpURLConnection con = (HttpURLConnection) new URL(uri).openConnection();
    con.setRequestMethod("OPTIONS");
    Assert.assertEquals(con.getResponseMessage(), 204, con.getResponseCode());
    for (Map.Entry<String, List<String>> e : con.getHeaderFields().entrySet()) {
        if (!"Link".equalsIgnoreCase(e.getKey()))
            continue;
        for (String header : e.getValue()) {
            Assert.assertNotNull(header);
            Matcher m = LINK.matcher(header);
            while (m.find()) {
                if (rev && !header.contains("rev="))
                    continue;
                String href = m.group(1);
                String a = m.group(2);
                String r = m.group(3) != null ? m.group(3) : m.group(4);
                String t = m.group(5) != null ? m.group(5) : m.group(6);
                if (a != null && !TermFactoryImpl.newInstance(uri).resolve(a).equals(uri))
                    continue;
                if (!rel.equals(r))
                    continue;
                if (types.length == 0 || t == null)
                    return ref(href);
                for (String type : types) {
                    for (String t1 : t.split("\\s+")) {
                        if (t1.length() > 0 && t1.startsWith(type)) {
                            return ref(href);
                        }// w w w . jav a2  s. c  o m
                    }
                }
            }
        }
    }
    StringBuilder sb = new StringBuilder();
    sb.append("<").append(uri).append("?").append(rel);
    sb.append(">");
    if (rev) {
        sb.append("; rev=\"");
    } else {
        sb.append("; rel=\"");
    }
    sb.append(rel).append("\"; type=\"");
    for (String type : types) {
        sb.append(type).append(' ');
    }
    sb.setLength(sb.length() - 1);
    sb.append("\"");
    Assert.assertEquals(sb.toString(), con.getHeaderField("Link"));
    return null;
}

From source file:com.diablominer.DiabloMiner.NetworkState.JSONRPCNetworkState.java

JsonNode doJSONRPCCall(boolean longPoll, ObjectNode message) throws IOException {
    HttpURLConnection connection = null;
    try {//from  w w  w. ja  v  a 2  s  . c  o m
        URL url;

        if (longPoll)
            url = longPollUrl;
        else
            url = queryUrl;

        Proxy proxy = diabloMiner.getProxy();

        if (proxy == null)
            connection = (HttpURLConnection) url.openConnection();
        else
            connection = (HttpURLConnection) url.openConnection(proxy);

        if (longPoll) {
            connection.setConnectTimeout(10 * 60 * 1000);
            connection.setReadTimeout(10 * 60 * 1000);
        } else {
            connection.setConnectTimeout(15 * 1000);
            connection.setReadTimeout(15 * 1000);
        }

        connection.setRequestProperty("Authorization", userPass);
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Accept-Encoding", "gzip,deflate");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Cache-Control", "no-cache");
        connection.setRequestProperty("User-Agent", "DiabloMiner");
        connection.setRequestProperty("X-Mining-Extensions", "longpoll rollntime switchto");
        connection.setDoOutput(true);

        OutputStream requestStream = connection.getOutputStream();
        Writer request = new OutputStreamWriter(requestStream);
        request.write(message.toString());
        request.close();
        requestStream.close();

        ObjectNode responseMessage = null;

        InputStream responseStream = null;

        try {
            String xLongPolling = connection.getHeaderField("X-Long-Polling");

            if (xLongPolling != null && !"".equals(xLongPolling) && longPollAsync == null) {
                if (xLongPolling.startsWith("http"))
                    longPollUrl = new URL(xLongPolling);
                else if (xLongPolling.startsWith("/"))
                    longPollUrl = new URL(queryUrl.getProtocol(), queryUrl.getHost(), queryUrl.getPort(),
                            xLongPolling);
                else
                    longPollUrl = new URL(queryUrl.getProtocol(), queryUrl.getHost(), queryUrl.getPort(),
                            (url.getFile() + "/" + xLongPolling).replace("//", "/"));

                longPollAsync = new LongPollAsync();
                Thread thread = new Thread(longPollAsync,
                        "DiabloMiner JSONRPC LongPollAsync for " + url.getHost());
                thread.start();
                diabloMiner.addThread(thread);

                workLifetime = 60000;

                diabloMiner.debug(queryUrl.getHost() + ": Enabling long poll support");
            }

            String xRollNTime = connection.getHeaderField("X-Roll-NTime");

            if (xRollNTime != null && !"".equals(xRollNTime)) {
                if (!"n".equalsIgnoreCase(xRollNTime) && rollNTime == false) {
                    rollNTime = true;

                    if (xRollNTime.startsWith("expire=")) {
                        try {
                            workLifetime = Integer.parseInt(xRollNTime.substring(7)) * 1000;
                        } catch (NumberFormatException ex) {
                        }
                    } else {
                        workLifetime = 60000;
                    }

                    diabloMiner.debug(queryUrl.getHost() + ": Enabling roll ntime support, expire after "
                            + (workLifetime / 1000) + " seconds");
                } else if ("n".equalsIgnoreCase(xRollNTime) && rollNTime == true) {
                    rollNTime = false;

                    if (longPoll)
                        workLifetime = 60000;
                    else
                        workLifetime = diabloMiner.getWorkLifetime();

                    diabloMiner.debug(queryUrl.getHost() + ": Disabling roll ntime support");
                }
            }

            String xSwitchTo = connection.getHeaderField("X-Switch-To");

            if (xSwitchTo != null && !"".equals(xSwitchTo)) {
                String oldHost = queryUrl.getHost();
                JsonNode newHost = mapper.readTree(xSwitchTo);

                queryUrl = new URL(queryUrl.getProtocol(), newHost.get("host").asText(),
                        newHost.get("port").getIntValue(), queryUrl.getPath());

                if (longPollUrl != null)
                    longPollUrl = new URL(longPollUrl.getProtocol(), newHost.get("host").asText(),
                            newHost.get("port").getIntValue(), longPollUrl.getPath());

                diabloMiner.info(oldHost + ": Switched to " + queryUrl.getHost());
            }

            String xRejectReason = connection.getHeaderField("X-Reject-Reason");

            if (xRejectReason != null && !"".equals(xRejectReason)) {
                rejectReason = xRejectReason;
            }

            String xIsP2Pool = connection.getHeaderField("X-Is-P2Pool");

            if (xIsP2Pool != null && !"".equals(xIsP2Pool)) {
                if (!noDelay)
                    diabloMiner.info("P2Pool no delay mode enabled");

                noDelay = true;
            }

            if (connection.getContentEncoding() != null) {
                if (connection.getContentEncoding().equalsIgnoreCase("gzip"))
                    responseStream = new GZIPInputStream(connection.getInputStream());
                else if (connection.getContentEncoding().equalsIgnoreCase("deflate"))
                    responseStream = new InflaterInputStream(connection.getInputStream());
            } else {
                responseStream = connection.getInputStream();
            }

            if (responseStream == null)
                throw new IOException("Drop to error handler");

            Object output = mapper.readTree(responseStream);

            if (NullNode.class.equals(output.getClass())) {
                throw new IOException("Bitcoin returned unparsable JSON");
            } else {
                try {
                    responseMessage = (ObjectNode) output;
                } catch (ClassCastException e) {
                    throw new IOException("Bitcoin returned unparsable JSON");
                }
            }

            responseStream.close();
        } catch (JsonProcessingException e) {
            throw new IOException("Bitcoin returned unparsable JSON");
        } catch (IOException e) {
            InputStream errorStream = null;
            IOException e2 = null;

            if (connection.getErrorStream() == null)
                throw new IOException("Bitcoin disconnected during response: " + connection.getResponseCode()
                        + " " + connection.getResponseMessage());

            if (connection.getContentEncoding() != null) {
                if (connection.getContentEncoding().equalsIgnoreCase("gzip"))
                    errorStream = new GZIPInputStream(connection.getErrorStream());
                else if (connection.getContentEncoding().equalsIgnoreCase("deflate"))
                    errorStream = new InflaterInputStream(connection.getErrorStream());
            } else {
                errorStream = connection.getErrorStream();
            }

            if (errorStream == null)
                throw new IOException("Bitcoin disconnected during response: " + connection.getResponseCode()
                        + " " + connection.getResponseMessage());

            byte[] errorbuf = new byte[8192];

            if (errorStream.read(errorbuf) < 1)
                throw new IOException("Bitcoin returned an error, but with no message");

            String error = new String(errorbuf).trim();

            if (error.startsWith("{")) {
                try {
                    Object output = mapper.readTree(error);

                    if (NullNode.class.equals(output.getClass()))
                        throw new IOException("Bitcoin returned an error message: " + error);
                    else
                        try {
                            responseMessage = (ObjectNode) output;
                        } catch (ClassCastException f) {
                            throw new IOException("Bitcoin returned unparsable JSON");
                        }

                    if (responseMessage.get("error") != null) {
                        if (responseMessage.get("error").get("message") != null
                                && responseMessage.get("error").get("message").asText() != null) {
                            error = responseMessage.get("error").get("message").asText().trim();
                            e2 = new IOException("Bitcoin returned error message: " + error);
                        } else if (responseMessage.get("error").asText() != null) {
                            error = responseMessage.get("error").asText().trim();

                            if (!"null".equals(error) && !"".equals(error))
                                e2 = new IOException("Bitcoin returned an error message: " + error);
                        }
                    }
                } catch (JsonProcessingException f) {
                    e2 = new IOException("Bitcoin returned unparsable JSON");
                }
            } else {
                e2 = new IOException("Bitcoin returned an error message: " + error);
            }

            errorStream.close();

            if (responseStream != null)
                responseStream.close();

            if (e2 == null)
                e2 = new IOException("Bitcoin returned an error, but with no message");

            throw e2;
        }

        if (responseMessage.get("error") != null) {
            if (responseMessage.get("error").get("message") != null
                    && responseMessage.get("error").get("message").asText() != null) {
                String error = responseMessage.get("error").get("message").asText().trim();
                throw new IOException("Bitcoin returned error message: " + error);
            } else if (responseMessage.get("error").asText() != null) {
                String error = responseMessage.get("error").asText().trim();

                if (!"null".equals(error) && !"".equals(error))
                    throw new IOException("Bitcoin returned error message: " + error);
            }
        }

        JsonNode result;

        try {
            result = responseMessage.get("result");
        } catch (Exception e) {
            throw new IOException("Bitcoin returned unparsable JSON");
        }

        if (result == null)
            throw new IOException("Bitcoin did not return a result or an error");

        return result;
    } catch (IOException e) {
        if (connection != null)
            connection.disconnect();

        throw e;
    }
}

From source file:org.miloss.fgsms.bueller.Bueller.java

/**
 * Sends an HTTP GET request to a url/*from  ww  w  .  j av  a 2  s.c  o  m*/
 *
 * @param endpoint - The URL of the server. (Example: "
 * http://www.yahoo.com/search") Note: This method will add the question
 * mark (?wsdl) to the request
 * @return - OK for 200 messages, all others, the actually response code or
 * error message
 */
protected String sendGetRequest(boolean pooled, String endpoint, int depth) {
    if (depth > 10) {
        //abort, possible redirect loop
        return "Aborting due to redirect loop";
    }
    String result = null;
    String policyUrl = new String(endpoint);
    if (endpoint.startsWith("http://")) {
        // Send a GET request to the servlet
        HttpURLConnection conn = null;
        try {
            String originalendpoint = endpoint;
            if (!endpoint.endsWith("?wsdl")) {
                endpoint = endpoint + "?wsdl";
            }
            conn = (HttpURLConnection) new URL(endpoint).openConnection();

            if (conn.getResponseCode() == 401) {
                //basic example WWW-Authenticate: Basic realm="fgsms Services"
                //digest example WWW-Authenticate: Digest realm="fgsms Services", qop="auth", nonce="2569aa2af54f6d47e8472f47f2e3da01", opaque="a39d25cce80574f8255b97052d8f1544"
                //WWW-Authenticate: Negotiate
                //WWW-Authenticate: NTLM
                //        String authtype = conn.getHeaderField("WWW-Authenticate");
                //    if (authtype.toLowerCase().startsWith("digest")) {
                return sendGetRequestAuth(pooled, endpoint, policyUrl, depth + 1);
            } else if (conn.getResponseCode() == 404) {
                //fix for sonatype nexus and non wsdl urls
                conn = (HttpURLConnection) new URL(originalendpoint).openConnection();
                return "Not found";
            } else if (conn.getResponseCode() == HttpStatus.SC_MOVED_PERMANENTLY
                    || conn.getResponseCode() == HttpStatus.SC_MOVED_TEMPORARILY
                    || conn.getResponseCode() == HttpStatus.SC_TEMPORARY_REDIRECT) {
                //follow the redirect
                String newUrl = conn.getHeaderField("Location");
                return sendGetRequest(pooled, newUrl, depth + 1);
                //System.out.println("Moved to " + newUrl); //should be the new destination url
                //return "Moved " + conn.getResponseMessage();
            } else if (conn.getResponseCode() == HttpStatus.SC_NOT_MODIFIED) {
                return "OK";
            }
            InputStream inputStream = null;
            try {
                inputStream = conn.getInputStream();
                byte[] buffer = new byte[1024];
                while (inputStream.read(buffer) >= 0) {
                }
                inputStream.close();
            } catch (Exception f) {
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (Exception ex) {
                    }
                }
            }

            String msg = conn.getResponseMessage();
            conn.disconnect();
            return msg;
        } catch (java.net.UnknownHostException ex) {
            return "Host unknown";
        } catch (Exception ex) {
            return ex.getMessage();
        } finally {
            if (conn != null) {
                try {
                    conn.disconnect();
                } catch (Exception ex) {
                }
            }
        }
    } else if (endpoint.startsWith("https://")) {
        if (!endpoint.endsWith("?wsdl")) {
            endpoint = endpoint + "?wsdl";
        }
        DefaultHttpClient c = new DefaultHttpClient();
        try {
            URL url = new URL(endpoint);
            int port = url.getPort();
            if (port == -1 && endpoint.toLowerCase().startsWith("http:")) {
                port = 80;
            }
            if (port == -1 && endpoint.toLowerCase().startsWith("https:")) {
                port = 443;
            }
            Scheme sch = null;
            if (sfpki == null) {
                sch = new Scheme("https", port, sf);
            } else {
                sch = new Scheme("https", port, sfpki);
            }
            if (endpoint.toLowerCase().startsWith("https:")) {
                c.getConnectionManager().getSchemeRegistry().register(sch);
            }

            HttpGet m = new HttpGet(endpoint);
            HttpResponse res = c.execute(m);
            int status = res.getStatusLine().getStatusCode();
            try {
                InputStream content = res.getEntity().getContent();
                byte[] buffer = new byte[1024];
                while (content.read(buffer) >= 0) {
                }
            } catch (Exception f) {
            }
            c.getConnectionManager().shutdown();
            if (status == 401) {
                return sendGetRequestAuth(pooled, endpoint, policyUrl, depth + 1);
            } else if (status == HttpStatus.SC_MOVED_PERMANENTLY
                    || status == HttpStatus.SC_MOVED_TEMPORARILY
                    || status == HttpStatus.SC_TEMPORARY_REDIRECT) {
                String newUrl = res.getHeaders("Location")[0].getValue();
                return sendGetRequest(pooled, newUrl, depth + 1);
            } else if (status == HttpStatus.SC_NOT_MODIFIED) {
                return "OK";
            } else {
                return (status < 300) ? "OK" : "offline";
            }
        } catch (Exception ex) {
            c.getConnectionManager().shutdown();
            log.log(Level.WARN, "error caught connecting to " + endpoint, ex);
            return ex.getMessage();
        }

    } else if (endpoint.startsWith("jms:")) {
        return doJmsURL(pooled, endpoint);
    }
    return "Unknown protocol";
}

From source file:winterwell.jtwitter.URLConnectionHttpClient.java

/**
 * Throw an exception if the connection failed
 * /* www . j  a v  a  2 s  . co m*/
 * @param connection
 */
final void processError(HttpURLConnection connection) {
    try {
        int code = connection.getResponseCode();
        if (code == 200)
            return;
        URL url = connection.getURL();
        // any explanation?
        String error = processError2_reason(connection);
        // which error?
        if (code == 401) {
            if (error.contains("Basic authentication is not supported"))
                throw new TwitterException.UpdateToOAuth();
            throw new TwitterException.E401(
                    error + "\n" + url + " (" + (name == null ? "anonymous" : name) + ")");
        }
        if (code == 403) {
            // separate out the 403 cases
            processError2_403(url, error);
        }
        if (code == 404) {
            // user deleted?
            if (error != null && error.contains("deleted"))
                // Note: This is a 403 exception
                throw new TwitterException.SuspendedUser(error + "\n" + url);
            throw new TwitterException.E404(error + "\n" + url);
        }
        if (code == 406)
            // Hm: It might be nice to have info on post variables here 
            throw new TwitterException.E406(error + "\n" + url);
        if (code == 413)
            throw new TwitterException.E413(error + "\n" + url);
        if (code == 416)
            throw new TwitterException.E416(error + "\n" + url);
        if (code == 420)
            throw new TwitterException.TooManyLogins(error + "\n" + url);
        if (code >= 500 && code < 600)
            throw new TwitterException.E50X(error + "\n" + url);

        // Over the rate limit?
        processError2_rateLimit(connection, code, error);

        // redirect??
        if (code > 299 && code < 400) {
            String locn = connection.getHeaderField("Location");
            throw new TwitterException(code + " " + error + " " + url + " -> " + locn);
        }

        // just report it as a vanilla exception
        throw new TwitterException(code + " " + error + " " + url);

    } catch (SocketTimeoutException e) {
        URL url = connection.getURL();
        throw new TwitterException.Timeout(timeout + "milli-secs for " + url);
    } catch (ConnectException e) {
        // probably also a time out
        URL url = connection.getURL();
        throw new TwitterException.Timeout(url.toString());
    } catch (SocketException e) {
        // treat as a server error - because it probably is
        // (yes, it could also be an error at your end)
        throw new TwitterException.E50X(e.toString());
    } catch (IOException e) {
        throw new TwitterException(e);
    }
}

From source file:org.ejbca.core.protocol.ocsp.ProtocolOcspHttpTest.java

/**
 * This test tests that the OCSP response for a status unknown contains the header "cache-control" with the value "no-cache, must-revalidate"
 * //from  ww w  . j  av  a  2s  .  co m
 * @throws Exception
 */
@Test
public void testUnknownStatusCacheControlHeader() throws Exception {

    // set ocsp configuration
    Map<String, String> map = new HashMap<String, String>();
    map.put(OcspConfiguration.UNTIL_NEXT_UPDATE, "1");
    this.helper.alterConfig(map);

    OCSPReqBuilder gen = new OCSPReqBuilder();
    gen.addRequest(new JcaCertificateID(SHA1DigestCalculator.buildSha1Instance(), cacert, new BigInteger("1")));
    OCSPReq req = gen.build();

    String sBaseURL = httpReqPath + '/' + resourceOcsp;
    String urlEnding = "";
    String b64 = new String(Base64.encode(req.getEncoded(), false));
    //String urls = URLEncoder.encode(b64, "UTF-8");    // JBoss/Tomcat will not accept escaped '/'-characters by default
    URL url = new URL(sBaseURL + '/' + b64 + urlEnding);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    if (con.getResponseCode() != 200) {
        log.info("URL when request gave unexpected result: " + url.toString() + " Message was: "
                + con.getResponseMessage());
    }
    assertEquals("Response code did not match. ", 200, con.getResponseCode());
    assertNotNull(con.getContentType());
    assertTrue(con.getContentType().startsWith("application/ocsp-response"));

    assertNotNull("No Cache-Control in reply.", con.getHeaderField("Cache-Control"));
    assertEquals("no-cache, must-revalidate", con.getHeaderField("Cache-Control"));

    // Create a GET request using Nonce extension, in this case we should have no cache-control header
    gen = new OCSPReqBuilder();
    gen.addRequest(new JcaCertificateID(SHA1DigestCalculator.buildSha1Instance(), cacert, new BigInteger("1")));
    Extension[] extensions = new Extension[1];
    extensions[0] = new Extension(OCSPObjectIdentifiers.id_pkix_ocsp_nonce, false,
            new DEROctetString("123456789".getBytes()));
    gen.setRequestExtensions(new Extensions(extensions));
    req = gen.build();
    b64 = new String(Base64.encode(req.getEncoded(), false));
    url = new URL(sBaseURL + '/' + b64 + urlEnding);
    con = (HttpURLConnection) url.openConnection();
    if (con.getResponseCode() != 200) {
        log.info("URL when request gave unexpected result: " + url.toString() + " Message was: "
                + con.getResponseMessage());
    }
    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()));
    BasicOCSPResp brep = (BasicOCSPResp) response.getResponseObject();
    byte[] noncerep = brep.getExtension(OCSPObjectIdentifiers.id_pkix_ocsp_nonce).getExtnValue().getEncoded();
    // Make sure we have a nonce in the response, we should have since we sent one in the request
    assertNotNull("Response should have nonce since we sent a nonce in the request", noncerep);
    ASN1InputStream ain = new ASN1InputStream(noncerep);
    ASN1OctetString oct = ASN1OctetString.getInstance(ain.readObject());
    ain.close();
    assertEquals("Response Nonce was not the same as the request Nonce, it must be", "123456789",
            new String(oct.getOctets()));
    assertNull(
            "Cache-Control in reply although we used Nonce in the request. Responses with Nonce should not have a Cache-control header.",
            con.getHeaderField("Cache-Control"));
}

From source file:com.osbitools.ws.shared.web.BasicWebUtils.java

public WebResponse readHttpData(String method, String url, byte[] params, String sheader, String stoken,
        String ctype) {/*from  w  w  w  .j  a  va2 s .  c om*/
    WebResponse res;
    InputStreamReader in = null;
    HttpURLConnection conn = null;
    Boolean fparams = params.length != 0;

    try {
        conn = (HttpURLConnection) (new URL(url)).openConnection();
        conn.setDoOutput(fparams);
        conn.setRequestMethod(method);

        if (ctype != null) {
            conn.setRequestProperty("Content-Type", ctype);
            conn.setRequestProperty("Content-Length", String.valueOf(params.length));
        }

        if (stoken != null)
            conn.setRequestProperty(sheader == null ? "Cookie" : sheader,
                    (sheader == null ? Constants.SECURE_TOKEN_NAME + "=" : "") + stoken);

        // Initiate connection
        conn.connect();

        if (fparams) {
            OutputStream os = null;

            try {
                os = conn.getOutputStream();
                os.write(params);
            } catch (IOException e) {
                return new WebResponse(conn);
            } finally {
                if (os != null)
                    os.close();
            }
        }

        // Response code
        int code;

        try {
            in = new InputStreamReader(conn.getInputStream());
        } catch (IOException e) {
            return new WebResponse(conn);
        }

        // Read response
        try {
            code = conn.getResponseCode();
        } catch (IOException e) {
            return null;
        }

        try {
            StringWriter out = new StringWriter();
            GenericUtils.copy(in, out);

            String msg = out.toString();
            out.close();

            in.close();

            res = new WebResponse(code, msg.replaceFirst("\"request_id\":\\d*", "\"request_id\":"));

            // Read and remember cookie for POST method
            if (method == "POST") {
                res.setCookie(conn.getHeaderField("Set-Cookie"));
            }

        } catch (IOException e) {
            return new WebResponse(code);
        }

    } catch (IOException e) {
        System.out.println("HTTP Request failed. " + e.getMessage());
        return null;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // Do nothing
            }
        }

        if (conn != null)
            conn.disconnect();
    }

    return res;
}