Example usage for java.net HttpURLConnection getHeaderFields

List of usage examples for java.net HttpURLConnection getHeaderFields

Introduction

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

Prototype

public Map<String, List<String>> getHeaderFields() 

Source Link

Document

Returns an unmodifiable Map of the header fields.

Usage

From source file:easyshop.downloadhelper.HttpPageGetter.java

public HttpPage getDHttpPage(PageRef url, String charSet) {
    count++;//w w w  .ja  v a2  s .  c om
    log.debug("getURL(" + count + ")");

    if (url.getUrlStr() == null) {
        ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0);
        return new OriHttpPage(-1, null, null, null, conRes, null);
    }
    URL requestedURL = null;
    try {
        requestedURL = new URL(url.getUrlStr());
    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        log.error("wrong urlstr" + url.getUrlStr());
        ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0);
        return new OriHttpPage(-1, null, null, null, conRes, null);
    }
    ;
    //        System.out.println(""+requestedURL.toExternalForm());
    URL referer = null;
    try {
        log.debug("Creating HTTP connection to " + requestedURL);
        HttpURLConnection conn = (HttpURLConnection) requestedURL.openConnection();
        if (referer != null) {
            log.debug("Setting Referer header to " + referer);
            conn.setRequestProperty("Referer", referer.toExternalForm());
        }

        if (userAgent != null) {
            log.debug("Setting User-Agent to " + userAgent);
            conn.setRequestProperty("User-Agent", userAgent);
        }

        //            DateFormat dateFormat=DateFormat.getDateInstance();
        //            conn.setRequestProperty("If-Modlfied-Since",dateFormat.parse("2005-08-15 20:18:30").toGMTString());
        conn.setUseCaches(false);

        //            conn.setRequestProperty("connection","keep-alive");
        for (Iterator it = conn.getRequestProperties().keySet().iterator(); it.hasNext();) {
            String key = (String) it.next();
            if (key == null) {
                break;
            }
            String value = conn.getHeaderField(key);
            //                System.out.println("Request header " + key + ": " + value);
        }
        log.debug("Opening URL");
        long startTime = System.currentTimeMillis();
        conn.connect();

        String resp = conn.getResponseMessage();
        log.debug("Remote server response: " + resp);

        int code = conn.getResponseCode();

        if (code != 200) {
            log.error("Could not get connection for code=" + code);
            System.err.println("Could not get connection for code=" + code);
            ConnResponse conRes = new ConnResponse(null, null, 0, 0, code);
            return new HttpPage(requestedURL.toExternalForm(), null, conRes, null);
        }

        //            if (conn.getContentLength()<=0||conn.getContentLength()>10000000){
        //               log.error("Content length==0");
        //               System.err.println("Content length==0");
        //               ConnResponse conRes=new ConnResponse(null,null,null,0,0,-100);
        //               return new URLObject(-1,requestedURL, null,null,conRes);
        //            }

        String respStr = conn.getHeaderField(0);
        long serverDate = conn.getDate();
        //            log.info("Server response: " + respStr);

        for (int i = 1; i < conn.getHeaderFields().size(); i++) {
            String key = conn.getHeaderFieldKey(i);
            if (key == null) {
                break;
            }
            String value = conn.getHeaderField(key);
            //                System.out.println("Received header " + key + ": " + value);
            //               log.debug("Received header " + key + ": " + value);
        }

        //            log.debug("Getting buffered input stream from remote connection");
        log.debug("start download(" + count + ")");
        BufferedInputStream remoteBIS = new BufferedInputStream(conn.getInputStream());
        ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);
        byte[] buf = new byte[1024];
        int bytesRead = 0;
        while (bytesRead >= 0) {
            baos.write(buf, 0, bytesRead);
            bytesRead = remoteBIS.read(buf);
        }
        //            baos.write(remoteBIS.read(new byte[conn.getContentLength()]));
        //            remoteBIS.close();
        byte[] content = baos.toByteArray();
        long timeTaken = System.currentTimeMillis() - startTime;
        if (timeTaken < 100)
            timeTaken = 500;

        int bytesPerSec = (int) ((double) content.length / ((double) timeTaken / 1000.0));
        //            log.info("Downloaded " + content.length + " bytes, " + bytesPerSec + " bytes/sec");
        if (content.length < conn.getContentLength()) {
            log.warn("Didn't download full content for URL: " + url);
            //                failureCount++;
            ConnResponse conRes = new ConnResponse(conn.getContentType(), null, content.length, serverDate,
                    code);
            return new HttpPage(requestedURL.toExternalForm(), null, conRes, conn.getContentType());
        }

        log.debug("download(" + count + ")");

        ConnResponse conRes = new ConnResponse(conn.getContentType(), null, conn.getContentLength(), serverDate,
                code);
        String c = charSet;
        if (c == null)
            c = conRes.getCharSet();
        HttpPage obj = new HttpPage(requestedURL.toExternalForm(), content, conRes, c);
        return obj;
    } catch (IOException ioe) {
        log.warn("Caught IO Exception: " + ioe.getMessage(), ioe);
        failureCount++;
        ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0);
        return new HttpPage(requestedURL.toExternalForm(), null, conRes, null);
    } catch (Exception e) {
        log.warn("Caught Exception: " + e.getMessage(), e);
        failureCount++;
        ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0);
        return new HttpPage(requestedURL.toExternalForm(), null, conRes, null);
    }
}

From source file:org.mozilla.mozstumbler.service.core.http.HttpUtil.java

@Override
public IResponse post(String urlString, byte[] data, Map<String, String> headers, boolean precompressed) {

    URL url = null;// w w  w .j a  v a2s  . c om
    HttpURLConnection httpURLConnection = null;

    try {
        url = new URL(urlString);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("Invalid URL", e);
    }

    if (data == null) {
        throw new IllegalArgumentException("Data must be not null");
    }

    if (headers == null) {
        headers = new HashMap<String, String>();
    }

    try {
        httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setConnectTimeout(5000); // set timeout to 5 seconds
        // HttpURLConnection and Java are braindead.
        // http://stackoverflow.com/questions/8587913/what-exactly-does-urlconnection-setdooutput-affect
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");

    } catch (IOException e) {
        Log.e(LOG_TAG, "Couldn't open a connection: ", e);
        return null;
    }

    httpURLConnection.setDoOutput(true);
    httpURLConnection.setRequestProperty(USER_AGENT_HEADER, userAgent);
    httpURLConnection.setRequestProperty("Content-Type", "application/json");

    // Workaround for a bug in Android mHttpURLConnection. When the library
    // reuses a stale connection, the connection may fail with an EOFException
    // http://stackoverflow.com/questions/15411213/android-httpsurlconnection-eofexception/17791819#17791819
    if (Build.VERSION.SDK_INT > 13 && Build.VERSION.SDK_INT < 19) {
        httpURLConnection.setRequestProperty("Connection", "Close");
    }

    // Set headers
    for (Map.Entry<String, String> entry : headers.entrySet()) {
        httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue());
    }

    byte[] wire_data = data;
    if (!precompressed) {
        wire_data = Zipper.zipData(data);
        if (wire_data != null) {
            httpURLConnection.setRequestProperty("Content-Encoding", "gzip");
        } else {
            Log.w(LOG_TAG, "Couldn't compress data, falling back to raw data.");
            wire_data = data;
        }
    } else {
        httpURLConnection.setRequestProperty("Content-Encoding", "gzip");
    }

    httpURLConnection.setFixedLengthStreamingMode(wire_data.length);
    try {
        OutputStream out = new BufferedOutputStream(httpURLConnection.getOutputStream());
        out.write(wire_data);
        out.flush();
        return new HTTPResponse(httpURLConnection.getResponseCode(), httpURLConnection.getHeaderFields(),
                getContentBody(httpURLConnection), wire_data.length);
    } catch (IOException e) {
        Log.e(LOG_TAG, "post error", e);
    } finally {
        httpURLConnection.disconnect();
    }
    return null;
}

From source file:org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest.java

public RestResponse httpSendByMethod(String url, String method, String body, Map<String, String> headers)
        throws IOException {

    RestResponse restResponse = new RestResponse();
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // add request method
    con.setRequestMethod(method);/* ww w .j  a  v a 2s.  c  om*/

    // add request headers
    if (headers != null) {
        for (Entry<String, String> header : headers.entrySet()) {
            String key = header.getKey();
            String value = header.getValue();
            con.setRequestProperty(key, value);
        }

    }
    if (body != null && !body.isEmpty() && !method.equals("DELETE")) {
        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(body);
        wr.flush();
        wr.close();
    }

    // con.connect();

    int responseCode = con.getResponseCode();
    logger.debug("Send {} http request, url: {}", method, url);
    logger.debug("Response Code: {}", responseCode);

    StringBuffer response = new StringBuffer();

    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
    } catch (Exception e) {
        // response = null;
        logger.debug("response body is null");
    }

    String result;
    try {

        result = IOUtils.toString(con.getErrorStream());
        response.append(result);

    } catch (Exception e2) {
        result = null;
    }
    logger.debug("Response body: {}", response);

    // print result

    restResponse.setErrorCode(responseCode);
    // if (response == null) {
    // restResponse.setResponse(null);
    // } else {
    // restResponse.setResponse(response.toString());
    // }

    if (response != null) {
        restResponse.setResponse(response.toString());
    }
    Map<String, List<String>> headerFields = con.getHeaderFields();
    restResponse.setHeaderFields(headerFields);
    String responseMessage = con.getResponseMessage();
    restResponse.setResponseMessage(responseMessage);

    con.disconnect();
    return restResponse;

}

From source file:org.jboss.tools.ws.ui.utils.JAXRSTester.java

/**
 * Call a JAX-RS service/*www  . j  av a  2s. co m*/
 * @param address
 * @param parameters
 * @param headers
 * @param methodType
 * @param requestBody
 * @param proxy
 * @param port
 * @throws Exception
 */
public void doTest(String address, Map<String, String> parameters, Map<String, String> headers,
        String methodType, String requestBody, String proxy, int port, String uid, String pwd)
        throws Exception {

    // handle the proxy
    Proxy proxyObject = null;
    if (proxy != null && proxy.length() > 0 && port > 0) {
        InetSocketAddress proxyAddress = new InetSocketAddress(proxy, port);
        proxyObject = new Proxy(Proxy.Type.HTTP, proxyAddress);
    }

    // clear the returned results
    resultBody = EMPTY_STRING;

    // get the parms string
    String query = buildWebQuery(parameters);

    // Clear the address of any leading/trailing spaces
    address = address.trim();

    // build the complete URL
    URL url = null;
    if (query != null && query.trim().length() > 0) {
        // add the ? if there are parameters
        if (!address.endsWith("?") && !address.contains("?")) {//$NON-NLS-1$ //$NON-NLS-2$

            // if we're a "GET" - add the ? by default
            if (methodType.equalsIgnoreCase("GET")) { //$NON-NLS-1$
                address = address + "?"; //$NON-NLS-1$

                // if we're a PUT or POST, check if we have parms
                // and add the ? if we do
            } else if (methodType.equalsIgnoreCase("POST")//$NON-NLS-1$ 
                    || methodType.equalsIgnoreCase("PUT") //$NON-NLS-1$
                    || methodType.equalsIgnoreCase("DELETE")) { //$NON-NLS-1$
                if (query.trim().length() > 0) {
                    address = address + "?"; //$NON-NLS-1$
                }
            }
        } else if (address.contains("?")) { //$NON-NLS-1$
            address = address + "&"; //$NON-NLS-1$
        }
        // add parms to the url if we have some
        url = new URL(address + query);
    } else {
        url = new URL(address);
    }

    // make connection
    HttpURLConnection httpurlc = null;
    if (proxyObject == null) {
        httpurlc = (HttpURLConnection) url.openConnection();
    } else {
        // if have proxy, pass it along
        httpurlc = (HttpURLConnection) url.openConnection(proxyObject);
    }

    // since we are expecting output back, set to true
    httpurlc.setDoOutput(true);

    // not sure what this does - may be used for authentication?
    httpurlc.setAllowUserInteraction(false);

    // set whether this is a GET or POST
    httpurlc.setRequestMethod(methodType);

    // if we have headers to add
    if (headers != null && !headers.isEmpty()) {
        Iterator<?> iter = headers.entrySet().iterator();
        while (iter.hasNext()) {
            Entry<?, ?> entry = (Entry<?, ?>) iter.next();
            if (entry.getKey() != null && entry.getKey() instanceof String)
                httpurlc.addRequestProperty((String) entry.getKey(), (String) entry.getValue());
        }
    }

    // if we have basic authentication to add, add it!
    if (uid != null && pwd != null) {
        String authStr = uid + ':' + pwd;
        byte[] authEncByte = Base64.encodeBase64(authStr.getBytes());
        String authStringEnc = new String(authEncByte);
        httpurlc.addRequestProperty("Authorization", "Basic " + authStringEnc); //$NON-NLS-1$//$NON-NLS-2$
    }

    requestHeaders = httpurlc.getRequestProperties();

    // Check if task has been interrupted
    if (Thread.interrupted()) {
        throw new InterruptedException();
    }

    // CONNECT!
    httpurlc.connect();

    // Check if task has been interrupted
    if (Thread.interrupted()) {
        throw new InterruptedException();
    }

    // If we are doing a POST and we have some request body to pass along, do it
    if (requestBody != null && (methodType.equalsIgnoreCase("POST") //$NON-NLS-1$
            || methodType.equalsIgnoreCase("PUT"))) { //$NON-NLS-1$
        requestBody = WSTestUtils.stripNLsFromXML(requestBody);
        OutputStreamWriter out = new OutputStreamWriter(httpurlc.getOutputStream());
        String stripped = stripCRLF(requestBody);
        out.write(stripped);
        out.close();
    }

    // Check if task has been interrupted
    if (Thread.interrupted()) {
        throw new InterruptedException();
    }

    // if we have headers to pass to user, copy them
    if (httpurlc.getHeaderFields() != null) {
        resultHeaders = httpurlc.getHeaderFields();
    }

    // retrieve result and put string results into the response
    InputStream is = null;
    try {
        is = httpurlc.getInputStream();
        // Check if task has been interrupted
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));//$NON-NLS-1$
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append("\n");//$NON-NLS-1$
        }
        br.close();
        resultBody = sb.toString();
        // Check if task has been interrupted
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
    } catch (IOException ie) {
        try {
            is = httpurlc.getErrorStream();

            // is possible that we're getting nothing back in the error stream
            if (is != null) {
                BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));//$NON-NLS-1$
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                    sb.append("\n");//$NON-NLS-1$
                }
                br.close();
                resultBody = sb.toString();
            }
        } catch (IOException ie2) {
            resultBody = ie2.getLocalizedMessage();
        }
    }

    // as a last resort, if we still have nothing to report,
    // show an error message to the user
    if (resultBody == null || resultBody.trim().isEmpty()) {
        resultBody = JBossWSUIMessages.JAXRSRSTestView_Message_Unsuccessful_Test;
    }

    // disconnect explicitly (may not be necessary)
    httpurlc.disconnect();
}

From source file:com.jms.notify.utils.httpclient.SimpleHttpUtils.java

/**
 *
 * @param httpParam//from w  w  w  .  j  a v a2s .  com
 * @return
 */
public static SimpleHttpResult httpRequest(SimpleHttpParam httpParam) {
    String url = httpParam.getUrl();
    Map<String, Object> parameters = httpParam.getParameters();
    String sMethod = httpParam.getMethod();
    String charSet = httpParam.getCharSet();
    boolean sslVerify = httpParam.isSslVerify();
    int maxResultSize = httpParam.getMaxResultSize();
    Map<String, Object> headers = httpParam.getHeaders();
    int readTimeout = httpParam.getReadTimeout();
    int connectTimeout = httpParam.getConnectTimeout();
    boolean ignoreContentIfUnsuccess = httpParam.isIgnoreContentIfUnsuccess();
    boolean hostnameVerify = httpParam.isHostnameVerify();
    TrustKeyStore trustKeyStore = httpParam.getTrustKeyStore();
    ClientKeyStore clientKeyStore = httpParam.getClientKeyStore();

    if (url == null || url.trim().length() == 0) {
        throw new IllegalArgumentException("invalid url : " + url);
    }
    if (maxResultSize <= 0) {
        throw new IllegalArgumentException("maxResultSize must be positive : " + maxResultSize);
    }
    Charset.forName(charSet);
    HttpURLConnection urlConn = null;
    URL destURL = null;

    String baseUrl = url.trim();
    if (!baseUrl.toLowerCase().startsWith(HTTPS_PREFIX) && !baseUrl.toLowerCase().startsWith(HTTP_PREFIX)) {
        baseUrl = HTTP_PREFIX + baseUrl;
    }

    String method = null;
    if (sMethod != null) {
        method = sMethod.toUpperCase();
    }
    if (method == null || !(method.equals(HTTP_METHOD_POST) || method.equals(HTTP_METHOD_GET))) {
        throw new IllegalArgumentException("invalid http method : " + method);
    }

    int index = baseUrl.indexOf("?");
    if (index > 0) {
        baseUrl = urlEncode(baseUrl, charSet);
    } else if (index == 0) {
        throw new IllegalArgumentException("invalid url : " + url);
    }

    String queryString = mapToQueryString(parameters, charSet);
    String targetUrl = "";
    if (method.equals(HTTP_METHOD_POST)) {
        targetUrl = baseUrl;
    } else {
        if (index > 0) {
            targetUrl = baseUrl + "&" + queryString;
        } else {
            targetUrl = baseUrl + "?" + queryString;
        }
    }
    try {
        destURL = new URL(targetUrl);
        urlConn = (HttpURLConnection) destURL.openConnection();

        setSSLSocketFactory(urlConn, sslVerify, hostnameVerify, trustKeyStore, clientKeyStore);

        boolean hasContentType = false;
        boolean hasUserAgent = false;
        for (String key : headers.keySet()) {
            if ("Content-Type".equalsIgnoreCase(key)) {
                hasContentType = true;
            }
            if ("user-agent".equalsIgnoreCase(key)) {
                hasUserAgent = true;
            }
        }
        if (!hasContentType) {
            headers.put("Content-Type", "application/x-www-form-urlencoded; charset=" + charSet);
        }
        if (!hasUserAgent) {
            headers.put("user-agent", "PlatSystem");
        }

        if (headers != null && !headers.isEmpty()) {
            for (Entry<String, Object> entry : headers.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                List<String> values = makeStringList(value);
                for (String v : values) {
                    urlConn.addRequestProperty(key, v);
                }
            }
        }
        urlConn.setDoOutput(true);
        urlConn.setDoInput(true);
        urlConn.setAllowUserInteraction(false);
        urlConn.setUseCaches(false);
        urlConn.setRequestMethod(method);
        urlConn.setConnectTimeout(connectTimeout);
        urlConn.setReadTimeout(readTimeout);

        if (method.equals(HTTP_METHOD_POST)) {
            String postData = queryString.length() == 0 ? httpParam.getPostData() : queryString;
            if (postData != null && postData.trim().length() > 0) {
                OutputStream os = urlConn.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os, charSet);
                osw.write(postData);
                osw.flush();
                osw.close();
            }
        }

        int responseCode = urlConn.getResponseCode();
        Map<String, List<String>> responseHeaders = urlConn.getHeaderFields();
        String contentType = urlConn.getContentType();

        SimpleHttpResult result = new SimpleHttpResult(responseCode);
        result.setHeaders(responseHeaders);
        result.setContentType(contentType);

        if (responseCode != 200 && ignoreContentIfUnsuccess) {
            return result;
        }

        InputStream is = urlConn.getInputStream();
        byte[] temp = new byte[1024];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int readBytes = is.read(temp);
        while (readBytes > 0) {
            baos.write(temp, 0, readBytes);
            readBytes = is.read(temp);
        }
        String resultString = new String(baos.toByteArray(), charSet); //new String(buffer.array(), charSet);
        baos.close();
        result.setContent(resultString);
        return result;
    } catch (Exception e) {
        logger.warn("connection error : " + e.getMessage());
        return new SimpleHttpResult(e);
    } finally {
        if (urlConn != null) {
            urlConn.disconnect();
        }
    }
}

From source file:com.wadpam.guja.oauth2.social.NetworkTemplate.java

public <J> NetworkResponse<J> exchangeForResponse(String method, String url, Map<String, String> requestHeaders,
        Object requestBody, Class<J> responseClass, boolean followRedirects) {

    // so that we can read in case of Exception
    InputStream in = null;/*  ww  w .  jav a  2  s  .co m*/
    try {

        // expand url?
        if (null != requestBody && !CONTENT_METHODS.contains(method)) {
            Map<String, Object> paramMap = requestBody instanceof Map ? (Map) requestBody
                    : MAPPER.convertValue(requestBody, Map.class);
            url = expandUrl(url, paramMap);
        }

        // create the connection
        URL u = new URL(url);
        HttpURLConnection con = (HttpURLConnection) u.openConnection();
        con.setInstanceFollowRedirects(followRedirects);

        // override default method
        if (null != method) {
            con.setRequestMethod(method);
        }
        LOG.info("{} {}", method, url);

        // Accept
        if (null != accept) {
            con.addRequestProperty(ACCEPT, accept);
            LOG.trace("{}: {}", ACCEPT, accept);
        }

        // Authorization
        if (null != authorization) {
            con.addRequestProperty(AUTHORIZATION, authorization);
            LOG.trace("{}: {}", AUTHORIZATION, authorization);
        }

        // other request headers:
        String contentType = null;
        if (null != requestHeaders) {
            for (Entry<String, String> entry : requestHeaders.entrySet()) {
                con.addRequestProperty(entry.getKey(), entry.getValue());
                LOG.info("{}: {}", entry.getKey(), entry.getValue());
                if (CONTENT_TYPE.equalsIgnoreCase(entry.getKey())) {
                    contentType = entry.getValue();
                }
            }
        }

        if (null != requestBody) {
            if (CONTENT_METHODS.contains(method)) {

                // content-type not specified in request headers?
                if (null == contentType) {
                    contentType = MIME_JSON;
                    con.addRequestProperty(CONTENT_TYPE, MIME_JSON);
                }
                con.setDoOutput(true);
                OutputStream out = con.getOutputStream();
                if (MIME_JSON.equals(contentType)) {
                    MAPPER.writeValue(out, requestBody);
                    final String json = MAPPER.writeValueAsString(requestBody);
                    LOG.debug("json Content: {}", json);
                } else {
                    // application/www-form-urlencoded
                    PrintWriter writer = new PrintWriter(out);
                    if (requestBody instanceof String) {
                        writer.print(requestBody);
                        LOG.debug("Content: {}", requestBody);
                    } else {
                        Map<String, Object> params = MAPPER.convertValue(requestBody, Map.class);
                        String content = expandUrl("", params);
                        writer.print(content.substring(1));
                        LOG.debug("Content: {}", content.substring(1));
                    }
                    writer.flush();
                }
                out.close();
            }
        }

        NetworkResponse<J> response = new NetworkResponse<J>(con.getResponseCode(), con.getHeaderFields(),
                con.getResponseMessage());
        LOG.info("HTTP {} {}", response.getCode(), response.getMessage());

        // response content to read and parse?
        if (null != con.getContentType()) {
            final String responseType = con.getContentType();
            LOG.debug("Content-Type: {}", responseType);
            in = con.getInputStream();

            if (con.getContentType().startsWith(MIME_JSON)) {
                response.setBody(MAPPER.readValue(in, responseClass));
                LOG.debug("Response JSON: {}", response.getBody());
            } else if (String.class.equals(responseClass)) {
                String s = readResponseAsString(in, responseType);
                LOG.info("Read {} bytes from {}", s.length(), con.getContentType());
                response.setBody((J) s);
            } else if (400 <= response.getCode()) {
                String s = readResponseAsString(in, responseType);
                LOG.warn(s);
            }

            in.close();
        }

        return response;
    } catch (IOException ioe) {
        if (null != in) {
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String s;
                while (null != (s = br.readLine())) {
                    LOG.warn(s);
                }
            } catch (IOException ignore) {
            }
        }
        throw new RuntimeException(String.format("NetworkTemplate.exchange: %s", ioe.getMessage()), ioe);
    }
}

From source file:org.opentestsystem.shared.test.interactioncontext.FastInteractionContext.java

public FastInteractionResponse buildResponse(String method, URL url, Map<String, List<String>> headers,
        Reader body, long timeout) throws IOException {

    URI uri = null;//from w  w w . ja v a  2  s .  c om
    try {
        uri = url.toURI();
    } catch (URISyntaxException e) {
        synchronized (this) {
            getActiveTimingRecord().fail(String.format("Invalid URL: %s", url.toString()), e);
        }
    }

    int iTimeout = (int) Math.min(Integer.MAX_VALUE, timeout);

    FastInteractionResponse response = null;
    HttpURLConnection connection = null;

    // This value is only used if an error causes things to fail before the connection is created.
    long t_start = System.currentTimeMillis();
    try {
        connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(iTimeout);
        connection.setReadTimeout(iTimeout);

        // Expect to send ouptut if a body has been supplied
        connection.setDoOutput(body != null);

        // Set headers (except cookies)
        if (headers != null) {
            for (Entry<String, List<String>> header_i : headers.entrySet()) {
                connection.setRequestProperty(header_i.getKey(), StringUtils.join(header_i.getValue(), ','));
            }
        } else {
            headers = new HashMap<String, List<String>>();
        }

        // Set cookies
        for (Entry<String, List<String>> header_i : _cookies.get(uri, headers).entrySet()) {
            for (String cookie_j : header_i.getValue()) {
                connection.addRequestProperty(header_i.getKey(), cookie_j);
            }
        }

        // Log the request, if desired
        if (_logger.isDebugEnabled()) {
            StringBuilder msg = new StringBuilder("Posted HTTP request:\r\n\r\n");
            msg.append(method).append("\r\n");
            msg.append(uri.toString()).append("\r\n");
            for (Entry<String, List<String>> header_i : connection.getRequestProperties().entrySet()) {
                for (String value_j : header_i.getValue())
                    msg.append(header_i.getKey()).append(": ").append(value_j).append("\r\n");
            }
            msg.append("\r\n");

            if (body != null) {
                String bodyString = IOUtils.toString(body);
                IOUtils.closeQuietly(body);
                body = new StringReader(bodyString);
                msg.append(bodyString);
            }

            _logger.debug(msg.toString());
        }

        // Start timing
        // We update here because we don't really want to count time spent above.
        t_start = System.currentTimeMillis();

        // Send the request
        connection.connect();

        // Send the body
        if (body != null) {
            try (OutputStream os = connection.getOutputStream();) {
                IOUtils.copy(body, os, "UTF-8");
            } finally {
                IOUtils.closeQuietly(body);
            }
        }

        // Record the cookies
        // SB-547 workaround: multiple set-cookie headers are sent for the same
        // cookie. Process the last.
        Map<String, String> cookieMap = new HashMap<>();
        for (Entry<String, List<String>> header_i : connection.getHeaderFields().entrySet()) {
            if (StringUtils.equals(header_i.getKey(), "Set-Cookie")) {
                for (String value_j : header_i.getValue()) {
                    for (String value_k : StringUtils.split(value_j, ',')) {
                        String[] cookieParts = StringUtils.split(value_k, "=", 2);
                        if (cookieParts.length > 0) {
                            String oldValue = cookieMap.get(cookieParts[0]);
                            if (oldValue == null || oldValue.length() < value_k.length()) {
                                cookieMap.put(cookieParts[0], value_k);
                            }
                        }
                    }
                }
            }
        }
        List<String> cookieValues = new ArrayList<>(cookieMap.values());
        Map<String, List<String>> cookieHeaders = new HashMap<>();
        cookieHeaders.put("Set-Cookie", cookieValues);
        _cookies.put(uri, cookieHeaders);

        // Retrieve the response
        InputStream is = connection.getInputStream();
        response = new FastInteractionResponse(this, is, connection, timeout, t_start + timeout);
        IOUtils.closeQuietly(is);
        connection.disconnect();

        // Stop timing
        long t_end = System.currentTimeMillis();
        synchronized (this) {
            InteractionTimingRecord timingRecord = getActiveTimingRecord();
            if (timingRecord != null) {
                timingRecord.accumulate("REMOTE_TIME", t_end - t_start);
                timingRecord.accumulate("N_REQ", 1);
            }
        }

        // Log the response, if desired
        if (_logger.isDebugEnabled()) {
            StringBuilder msg = new StringBuilder("Response from HTTP request:\r\n\r\n");
            msg.append(uri.toString()).append("\r\n\r\n");
            for (Entry<String, List<String>> header_i : connection.getHeaderFields().entrySet()) {
                for (String value_j : header_i.getValue()) {
                    msg.append(header_i.getKey()).append(": ").append(value_j).append("\r\n");
                }
            }
            msg.append("\r\n");
            String bodyString = response.getResponseBodyAsString();
            if (bodyString != null) {
                if (bodyString.length() <= 1024) {
                    msg.append(bodyString);
                } else {
                    msg.append(String.format("Body length %d. Showing first 1024 characters\r\n\r\n",
                            bodyString.length()));
                    msg.append(bodyString.subSequence(0, 1024));
                }
            }
            _logger.debug(msg.toString());
        }

        return response;
    } catch (IOException e) {
        synchronized (this) {
            failActiveInteraction(String.format("IO Error trying to fill request to %s", url.toString()), e);
            InputStream es = null;
            es = connection.getErrorStream();
            response = new FastInteractionResponse(this, es, connection, timeout, t_start + timeout);
            IOUtils.closeQuietly(es);
        }
    }
    return response;
}

From source file:org.apache.streams.urls.LinkResolver.java

public void unwindLink(String url) {
    Objects.requireNonNull(linkDetails);
    Objects.requireNonNull(url);/*from ww  w  . j  a v  a 2  s.co m*/

    // Check url validity
    UrlValidator urlValidator = new UrlValidator();
    if (!urlValidator.isValid(url)) {
        linkDetails.setLinkStatus(LinkDetails.LinkStatus.MALFORMED_URL);
        return;
    }

    // Check to see if they wound up in a redirect loop,
    // IE: 'A' redirects to 'B', then 'B' redirects to 'A'
    if ((linkDetails.getRedirectCount() != null && linkDetails.getRedirectCount() > 0
            && (linkDetails.getOriginalURL().equals(url) || linkDetails.getRedirects().contains(url)))
            || (linkDetails.getRedirectCount() != null
                    && linkDetails.getRedirectCount() > MAX_ALLOWED_REDIRECTS)) {
        linkDetails.setLinkStatus(LinkDetails.LinkStatus.LOOP);
        return;
    }

    if (!linkDetails.getOriginalURL().equals(url))
        linkDetails.getRedirects().add(url);

    HttpURLConnection connection = null;

    // Store where the redirected link will go (if there is one)
    String reDirectedLink = null;

    try {
        // Turn the string into a URL
        URL thisURL = new URL(url);

        // Be sensitive to overloading domains STREAMS-77
        try {
            String host = thisURL.getHost().toLowerCase();
            if (!domainsSensitiveTo.contains(host)) {
                domainsSensitiveTo.add(host);
                long domainWait = LinkResolverHelperFunctions.waitTimeForDomain(thisURL.getHost());
                if (domainWait > 0) {
                    LOGGER.debug("Waiting for domain: {}", domainWait);
                    Thread.sleep(domainWait);
                }
            }
        } catch (Exception e) {
            // noOp
        }

        connection = (HttpURLConnection) new URL(url).openConnection();

        // now we are going to pretend that we are a browser...
        // This is the way my mac works.
        if (!BOTS_ARE_OK.contains(thisURL.getHost())) {
            connection.addRequestProperty("Host", thisURL.getHost());

            // Bots are not 'ok', so we need to spoof the headers
            for (String k : SPOOF_HTTP_HEADERS.keySet())
                connection.addRequestProperty(k, SPOOF_HTTP_HEADERS.get(k));

            // the test to seattlemamadoc.com prompted this change.
            // they auto detect bots by checking the referrer chain and the 'user-agent'
            // this broke the t.co test. t.co URLs are EXPLICITLY ok with bots
            // there is a list for URLS that behave this way at the top in BOTS_ARE_OK
            // smashew 2013-13-2013
            if (linkDetails.getRedirectCount() > 0 && BOTS_ARE_OK.contains(thisURL.getHost()))
                connection.addRequestProperty("Referrer", linkDetails.getOriginalURL());
        }

        connection.setReadTimeout(DEFAULT_HTTP_TIMEOUT);
        connection.setConnectTimeout(DEFAULT_HTTP_TIMEOUT);

        // we want to follow this behavior on our own to ensure that we are getting to the
        // proper place. This is especially true with links that are wounded by special
        // link winders,
        // IE:
        connection.setInstanceFollowRedirects(false);

        if (linkDetails.getCookies() != null)
            for (String cookie : linkDetails.getCookies())
                connection.addRequestProperty("Cookie", cookie.split(";", 1)[0]);

        connection.connect();

        linkDetails.setFinalResponseCode((long) connection.getResponseCode());

        Map<String, List<String>> headers = createCaseInsensitiveMap(connection.getHeaderFields());
        /*
         * If they want us to set cookies, well, then we will set cookies
         * Example URL:
         * http://nyti.ms/1bCpesx
         *****************************************************************/
        if (headers.containsKey(SET_COOKIE_IDENTIFIER))
            linkDetails.getCookies().add(headers.get(SET_COOKIE_IDENTIFIER).get(0));

        switch (linkDetails.getFinalResponseCode().intValue()) {
        /*
         * W3C HTTP Response Codes:
         * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
         */
        case 200: // HTTP OK
            linkDetails.setFinalURL(connection.getURL().toString());
            linkDetails.setDomain(new URL(linkDetails.getFinalURL()).getHost());
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.SUCCESS);
            break;
        case 300: // Multiple choices
        case 301: // URI has been moved permanently
        case 302: // Found
        case 303: // Primarily for a HTTP Post
        case 304: // Not Modified
        case 306: // This status code is unused but in the redirect block.
        case 307: // Temporary re-direct
            /*
             * Author:
             * Smashew
             *
             * Date: 2013-11-15
             *
             * Note:
             * It is possible that we have already found our final URL. In
             * the event that we have found our final URL, we are going to
             * save this URL as long as it isn't the original URL.
             * We are still going to ask the browser to re-direct, but in the
             * case of yet another redirect, seen with the redbull test
             * this can be followed by a 304, a browser, by W3C standards would
             * still render the page with it's content, but for us to assert
             * a success, we are really hoping for a 304 message.
             *******************************************************************/
            if (!linkDetails.getOriginalURL().toLowerCase()
                    .equals(connection.getURL().toString().toLowerCase()))
                linkDetails.setFinalURL(connection.getURL().toString());
            if (!headers.containsKey(LOCATION_IDENTIFIER)) {
                LOGGER.info("Headers: {}", headers);
                linkDetails.setLinkStatus(LinkDetails.LinkStatus.REDIRECT_ERROR);
            } else {
                linkDetails.setRedirected(Boolean.TRUE);
                linkDetails.setRedirectCount(linkDetails.getRedirectCount() + 1);
                reDirectedLink = connection.getHeaderField(LOCATION_IDENTIFIER);
            }
            break;
        case 305: // User must use the specified proxy (deprecated by W3C)
            break;
        case 401: // Unauthorized (nothing we can do here)
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.UNAUTHORIZED);
            break;
        case 403: // HTTP Forbidden (Nothing we can do here)
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.FORBIDDEN);
            break;
        case 404: // Not Found (Page is not found, nothing we can do with a 404)
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.NOT_FOUND);
            break;
        case 500: // Internal Server Error
        case 501: // Not Implemented
        case 502: // Bad Gateway
        case 503: // Service Unavailable
        case 504: // Gateway Timeout
        case 505: // Version not supported
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.HTTP_ERROR_STATUS);
            break;
        default:
            LOGGER.info("Unrecognized HTTP Response Code: {}", linkDetails.getFinalResponseCode());
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.NOT_FOUND);
            break;
        }
    } catch (MalformedURLException e) {
        // the URL is trash, so, it can't load it.
        linkDetails.setLinkStatus(LinkDetails.LinkStatus.MALFORMED_URL);
    } catch (IOException ex) {
        // there was an issue we are going to set to error.
        linkDetails.setLinkStatus(LinkDetails.LinkStatus.ERROR);
    } catch (Exception ex) {
        // there was an unknown issue we are going to set to exception.
        linkDetails.setLinkStatus(LinkDetails.LinkStatus.EXCEPTION);
    } finally {
        // if the connection is not null, then we need to disconnect to close any underlying resources
        if (connection != null)
            connection.disconnect();
    }

    // If there was a redirection, then we have to keep going
    // Placing this code here should help to satisfy ensuring that the connection object
    // is closed successfully.
    if (reDirectedLink != null)
        unwindLink(reDirectedLink);

}

From source file:com.athenahealth.api.APIConnection.java

/**
 * Make the API call./*from  ww  w. j a  v  a  2  s  . c o m*/
 *
 * This method abstracts away the connection, streams, and readers necessary to make an HTTP
 * request.  It also adds in the Authorization header and token.
 *
 * @param verb       HTTP method to use
 * @param path       URI to find
 * @param parameters key-value pairs of request parameters
 * @param headers    key-value pairs of request headers
 * @param secondcall true if this is the retried request
 * @return the JSON-decoded response
 *
 * @throws AthenahealthException If there is an error making the call.
 *                               API-level errors are reported in the return-value.
 */
private Object call(String verb, String path, Map<String, String> parameters, Map<String, String> headers,
        boolean secondcall) throws AthenahealthException {
    try {
        // Join up a url and open a connection
        URL url = new URL(path_join(getBaseURL(), version, practiceid, path));
        HttpURLConnection conn = openConnection(url);
        conn.setRequestMethod(verb);

        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

        // Set the Authorization header using the token, then do the rest of the headers
        conn.setRequestProperty("Authorization", "Bearer " + token);
        if (headers != null) {
            for (Map.Entry<String, String> pair : headers.entrySet()) {
                conn.setRequestProperty(pair.getKey(), pair.getValue());
            }
        }

        // Set the request parameters, if there are any
        if (parameters != null) {
            conn.setDoOutput(true);
            Writer wr = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            wr.write(urlencode(parameters));
            wr.flush();
            wr.close();
        }

        // If we get a 401, retry once
        if (conn.getResponseCode() == 401 && !secondcall) {
            authenticate();
            return call(verb, path, parameters, headers, true);
        }

        // The API response is in the input stream on success and the error stream on failure.
        BufferedReader rd;
        try {
            rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        } catch (IOException e) {
            rd = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
        }
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        rd.close();

        String rawResponse = sb.toString();

        if (503 == conn.getResponseCode())
            throw new AthenahealthException("Service Temporarily Unavailable: " + rawResponse);

        if (!"application/json".equals(conn.getContentType()))
            throw new AthenahealthException("Expected application/json response, got " + conn.getContentType()
                    + " instead." + " Content=" + rawResponse);

        // If it won't parse as an object, it'll parse as an array.
        Object response;
        try {
            response = new JSONObject(rawResponse);
        } catch (JSONException e) {
            try {
                response = new JSONArray(rawResponse);
            } catch (JSONException e2) {
                if (Boolean.getBoolean("com.athenahealth.api.dump-response-on-JSON-error")) {
                    System.err.println("Server response code: " + conn.getResponseCode());
                    Map<String, List<String>> responseHeaders = conn.getHeaderFields();
                    for (Map.Entry<String, List<String>> header : responseHeaders.entrySet())
                        for (String value : header.getValue()) {
                            if (null == header.getKey() || "".equals(header.getKey()))
                                System.err.println("Status: " + value);
                            else
                                System.err.println(header.getKey() + "=" + value);
                        }
                }
                throw new AthenahealthException(
                        "Cannot parse response from server as JSONObject or JSONArray: " + rawResponse, e2);
            }
        }

        return response;
    } catch (MalformedURLException mue) {
        throw new AthenahealthException("Invalid URL", mue);
    } catch (IOException ioe) {
        throw new AthenahealthException("I/O error during call", ioe);
    }
}

From source file:org.alfresco.mobile.android.api.network.NetworkHttpInvoker.java

private static Response invoke(UrlBuilder url, String method, String contentType,
        Map<String, List<String>> httpHeaders, Output writer, boolean forceOutput, BigInteger offset,
        BigInteger length, Map<String, String> params) {
    try {/*  w ww. j ava2s.c o  m*/
        // Log.d("URL", url.toString());

        // connect
        HttpURLConnection conn = (HttpURLConnection) (new URL(url.toString())).openConnection();
        conn.setRequestMethod(method);
        conn.setDoInput(true);
        conn.setDoOutput(writer != null || forceOutput);
        conn.setAllowUserInteraction(false);
        conn.setUseCaches(false);
        conn.setRequestProperty("User-Agent", ClientVersion.OPENCMIS_CLIENT);

        // set content type
        if (contentType != null) {
            conn.setRequestProperty("Content-Type", contentType);
        }
        // set other headers
        if (httpHeaders != null) {
            for (Map.Entry<String, List<String>> header : httpHeaders.entrySet()) {
                if (header.getValue() != null) {
                    for (String value : header.getValue()) {
                        conn.addRequestProperty(header.getKey(), value);
                    }
                }
            }
        }

        // range
        BigInteger tmpOffset = offset;
        if ((tmpOffset != null) || (length != null)) {
            StringBuilder sb = new StringBuilder("bytes=");

            if ((tmpOffset == null) || (tmpOffset.signum() == -1)) {
                tmpOffset = BigInteger.ZERO;
            }

            sb.append(tmpOffset.toString());
            sb.append("-");

            if ((length != null) && (length.signum() == 1)) {
                sb.append(tmpOffset.add(length.subtract(BigInteger.ONE)).toString());
            }

            conn.setRequestProperty("Range", sb.toString());
        }

        conn.setRequestProperty("Accept-Encoding", "gzip,deflate");

        // add url form parameters
        if (params != null) {
            DataOutputStream ostream = null;
            OutputStream os = null;
            try {
                os = conn.getOutputStream();
                ostream = new DataOutputStream(os);

                Set<String> parameters = params.keySet();
                StringBuffer buf = new StringBuffer();

                int paramCount = 0;
                for (String it : parameters) {
                    String parameterName = it;
                    String parameterValue = (String) params.get(parameterName);

                    if (parameterValue != null) {
                        parameterValue = URLEncoder.encode(parameterValue, "UTF-8");
                        if (paramCount > 0) {
                            buf.append("&");
                        }
                        buf.append(parameterName);
                        buf.append("=");
                        buf.append(parameterValue);
                        ++paramCount;
                    }
                }
                ostream.writeBytes(buf.toString());
            } finally {
                if (ostream != null) {
                    ostream.flush();
                    ostream.close();
                }
                IOUtils.closeStream(os);
            }
        }

        // send data

        if (writer != null) {
            // conn.setChunkedStreamingMode((64 * 1024) - 1);
            OutputStream connOut = null;
            connOut = conn.getOutputStream();
            OutputStream out = new BufferedOutputStream(connOut, BUFFER_SIZE);
            writer.write(out);
            out.flush();
        }

        // connect
        conn.connect();

        // get stream, if present
        int respCode = conn.getResponseCode();
        InputStream inputStream = null;
        if ((respCode == HttpStatus.SC_OK) || (respCode == HttpStatus.SC_CREATED)
                || (respCode == HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION)
                || (respCode == HttpStatus.SC_PARTIAL_CONTENT)) {
            inputStream = conn.getInputStream();
        }

        // get the response
        return new Response(respCode, conn.getResponseMessage(), conn.getHeaderFields(), inputStream,
                conn.getErrorStream());
    } catch (Exception e) {
        throw new CmisConnectionException("Cannot access " + url + ": " + e.getMessage(), e);
    }
}