Example usage for java.net HttpURLConnection setInstanceFollowRedirects

List of usage examples for java.net HttpURLConnection setInstanceFollowRedirects

Introduction

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

Prototype

public void setInstanceFollowRedirects(boolean followRedirects) 

Source Link

Document

Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this HttpURLConnection instance.

Usage

From source file:org.openqa.selenium.server.ProxyHandler.java

protected long proxyPlainTextRequest(URL url, String pathInContext, String pathParams, HttpRequest request,
        HttpResponse response) throws IOException {
    CaptureNetworkTrafficCommand.Entry entry = new CaptureNetworkTrafficCommand.Entry(request.getMethod(),
            url.toString());/*  ww w  .  j  av a  2 s  . c om*/
    entry.addRequestHeaders(request);

    if (log.isDebugEnabled())
        log.debug("PROXY URL=" + url);

    URLConnection connection = url.openConnection();
    connection.setAllowUserInteraction(false);

    if (proxyInjectionMode) {
        adjustRequestForProxyInjection(request, connection);
    }

    // Set method
    HttpURLConnection http = null;
    if (connection instanceof HttpURLConnection) {
        http = (HttpURLConnection) connection;
        http.setRequestMethod(request.getMethod());
        http.setInstanceFollowRedirects(false);
        if (trustAllSSLCertificates && connection instanceof HttpsURLConnection) {
            TrustEverythingSSLTrustManager.trustAllSSLCertificates((HttpsURLConnection) connection);
        }
    }

    // check connection header
    String connectionHdr = request.getField(HttpFields.__Connection);
    if (connectionHdr != null && (connectionHdr.equalsIgnoreCase(HttpFields.__KeepAlive)
            || connectionHdr.equalsIgnoreCase(HttpFields.__Close)))
        connectionHdr = null;

    // copy headers
    boolean xForwardedFor = false;
    boolean isGet = "GET".equals(request.getMethod());
    boolean hasContent = false;
    Enumeration enm = request.getFieldNames();
    while (enm.hasMoreElements()) {
        // TODO could be better than this!
        String hdr = (String) enm.nextElement();

        if (_DontProxyHeaders.containsKey(hdr) || !_chained && _ProxyAuthHeaders.containsKey(hdr))
            continue;
        if (connectionHdr != null && connectionHdr.indexOf(hdr) >= 0)
            continue;

        if (!isGet && HttpFields.__ContentType.equals(hdr))
            hasContent = true;

        Enumeration vals = request.getFieldValues(hdr);
        while (vals.hasMoreElements()) {
            String val = (String) vals.nextElement();
            if (val != null) {
                // don't proxy Referer headers if the referer is Selenium!
                if ("Referer".equals(hdr) && (-1 != val.indexOf("/selenium-server/"))) {
                    continue;
                }
                if (!isGet && HttpFields.__ContentLength.equals(hdr) && Integer.parseInt(val) > 0) {
                    hasContent = true;
                }

                connection.addRequestProperty(hdr, val);
                xForwardedFor |= HttpFields.__XForwardedFor.equalsIgnoreCase(hdr);
            }
        }
    }

    // add any custom request headers that the user asked for
    Map<String, String> customRequestHeaders = AddCustomRequestHeaderCommand.getHeaders();
    for (Map.Entry<String, String> e : customRequestHeaders.entrySet()) {
        connection.addRequestProperty(e.getKey(), e.getValue());
        entry.addRequestHeader(e.getKey(), e.getValue());
    }

    // Proxy headers
    if (!_anonymous)
        connection.setRequestProperty("Via", "1.1 (jetty)");
    if (!xForwardedFor)
        connection.addRequestProperty(HttpFields.__XForwardedFor, request.getRemoteAddr());

    // a little bit of cache control
    String cache_control = request.getField(HttpFields.__CacheControl);
    if (cache_control != null
            && (cache_control.indexOf("no-cache") >= 0 || cache_control.indexOf("no-store") >= 0))
        connection.setUseCaches(false);

    // customize Connection
    customizeConnection(pathInContext, pathParams, request, connection);

    try {
        connection.setDoInput(true);

        // do input thang!
        InputStream in = request.getInputStream();
        if (hasContent) {
            connection.setDoOutput(true);
            IO.copy(in, connection.getOutputStream());
        }

        // Connect
        connection.connect();
    } catch (Exception e) {
        LogSupport.ignore(log, e);
    }

    InputStream proxy_in = null;

    // handler status codes etc.
    int code = -1;
    if (http != null) {
        proxy_in = http.getErrorStream();

        try {
            code = http.getResponseCode();
        } catch (SSLHandshakeException e) {
            throw new RuntimeException("Couldn't establish SSL handshake.  Try using trustAllSSLCertificates.\n"
                    + e.getLocalizedMessage(), e);
        }
        response.setStatus(code);
        response.setReason(http.getResponseMessage());

        String contentType = http.getContentType();
        if (log.isDebugEnabled()) {
            log.debug("Content-Type is: " + contentType);
        }
    }

    if (proxy_in == null) {
        try {
            proxy_in = connection.getInputStream();
        } catch (Exception e) {
            LogSupport.ignore(log, e);
            proxy_in = http.getErrorStream();
        }
    }

    // clear response defaults.
    response.removeField(HttpFields.__Date);
    response.removeField(HttpFields.__Server);

    // set response headers
    int h = 0;
    String hdr = connection.getHeaderFieldKey(h);
    String val = connection.getHeaderField(h);
    while (hdr != null || val != null) {
        if (hdr != null && val != null && !_DontProxyHeaders.containsKey(hdr)
                && (_chained || !_ProxyAuthHeaders.containsKey(hdr)))
            response.addField(hdr, val);
        h++;
        hdr = connection.getHeaderFieldKey(h);
        val = connection.getHeaderField(h);
    }
    if (!_anonymous)
        response.setField("Via", "1.1 (jetty)");

    response.removeField(HttpFields.__ETag); // possible cksum?  Stop caching...
    response.removeField(HttpFields.__LastModified); // Stop caching...

    // Handled
    long bytesCopied = -1;
    request.setHandled(true);
    if (proxy_in != null) {
        boolean injectableResponse = http.getResponseCode() == HttpURLConnection.HTTP_OK
                || (http.getResponseCode() >= 400 && http.getResponseCode() < 600);
        if (proxyInjectionMode && injectableResponse) {
            // check if we should proxy this path based on the dontProxyRegex that can be user-specified
            if (shouldInject(request.getPath())) {
                bytesCopied = InjectionHelper.injectJavaScript(request, response, proxy_in,
                        response.getOutputStream(), debugURL);
            } else {
                bytesCopied = ModifiedIO.copy(proxy_in, response.getOutputStream());
            }
        } else {
            bytesCopied = ModifiedIO.copy(proxy_in, response.getOutputStream());
        }
    }

    entry.finish(code, bytesCopied);
    entry.addResponseHeader(response);

    CaptureNetworkTrafficCommand.capture(entry);

    return bytesCopied;
}

From source file:com.orange.oidc.tim.service.HttpOpenidConnect.java

String refreshTokenWithTIM() {
    // android.os.Debug.waitForDebugger();

    // check initialization
    if (mOcp == null || isEmpty(mOcp.m_server_url))
        return null;

    // get refresh endpoint
    String token_endpoint = getEndpointFromConfigOidc("token_endpoint", mOcp.m_server_url);
    if (isEmpty(token_endpoint)) {
        Logd(TAG, "logout : could not get token_endpoint on server : " + mOcp.m_server_url);
        return null;
    }/*from  w w  w  .j  ava  2  s. c o  m*/

    // set up connection
    HttpURLConnection huc = getHUC(token_endpoint);
    huc.setDoOutput(true);
    huc.setInstanceFollowRedirects(false);
    huc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    // generate new RSA keys by SIM
    TokensKeys tk = secureStorage.genTimAppKey(mOcp);

    if (tk == null) {
        // no token key found
        return null;
    }

    // check value of refresh token
    if (isEmpty(tk.refresh_token)) {
        // nothing to do
        Log.d("refreshTokenWithTIM", "refresh_token null or empty");
        return null;
    }

    // prepare parameters
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(7);

    // add authentication assertion
    if (mUsePrivateKeyJWT) {
        String pkj = secureStorage.getPrivateKeyJwt(token_endpoint);
        if (pkj != null && pkj.length() > 0) {
            nameValuePairs.add(new BasicNameValuePair("client_assertion_type",
                    "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"));
            nameValuePairs.add(new BasicNameValuePair("client_assertion", pkj));
        }
    } else {
        String csb = secureStorage.getClientSecretBasic();
        if (csb != null && csb.length() > 0) {
            huc.setRequestProperty("Authorization", "Basic " + csb);
        }
    }

    String clientId = secureStorage.getClientId();
    nameValuePairs.add(new BasicNameValuePair("client_id", clientId));
    nameValuePairs.add(new BasicNameValuePair("grant_type", "refresh_token"));
    nameValuePairs.add(new BasicNameValuePair("refresh_token", tk.refresh_token));
    if (!isEmpty(tk.serverScope)) {
        nameValuePairs.add(new BasicNameValuePair("scope", tk.serverScope));
    } else if (!isEmpty(mOcp.m_scope)) {
        nameValuePairs.add(new BasicNameValuePair("scope", mOcp.m_scope));
    }

    // add public key to request
    nameValuePairs.add(new BasicNameValuePair("tim_app_key", tk.jwkPub));

    try {
        // write parameters to http connection
        OutputStream os = huc.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        // get URL encoded string from list of key value pairs
        String postParam = getQuery(nameValuePairs);
        Logd("refreshTokenWithTIM", "url: " + token_endpoint);
        Logd("refreshTokenWithTIM", "POST: " + postParam);
        writer.write(postParam);
        writer.flush();
        writer.close();
        os.close();

        // try to connect
        huc.connect();
        // connexion status
        int responseCode = 0;
        try {
            // Will throw IOException if server responds with 401.
            responseCode = huc.getResponseCode();
        } catch (IOException e) {
            e.printStackTrace();
            // Will return 401, because now connection has the correct internal state.
            responseCode = huc.getResponseCode();
        }

        Logd(TAG, "refreshTokenWithTIM response: " + responseCode);

        // if 200 - OK, read the json string
        if (responseCode == 200) {
            sysout("refreshTokenWithTIM - code " + responseCode);
            InputStream is = huc.getInputStream();
            String result = convertStreamToString(is);
            is.close();
            huc.disconnect();

            Logd("refreshTokenWithTIM", "result: " + result);
            sysout("refreshTokenWithTIM - content: " + result);

            // TODO : verify TIM app key is correct

            return result;
        } else if (responseCode == 401) {
            // remove entry
            Logd("refreshTokenWithTIM", "got 401, remove entry");
            secureStorage.delete_tokens(mOcp.m_server_url, mOcp.m_client_id, mOcp.m_scope);
        }
        huc.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;

}

From source file:org.runnerup.export.FunBeatUploader.java

@Override
public Status connect() {
    Exception ex = null;/* ww w. ja v  a  2  s  . c om*/
    HttpURLConnection conn = null;
    cookies.clear();
    formValues.clear();

    Status s = Status.NEED_AUTH;
    s.authMethod = AuthMethod.USER_PASS;
    if (username == null || password == null) {
        return s;
    }

    if (loginID == null || loginSecretHashed == null) {
        if (!validateAndCreateSecrets(username, password))
            return s;
    }

    try {
        /**
         * connect to START_URL to get cookies/formValues
         */
        conn = (HttpURLConnection) new URL(START_URL).openConnection();
        conn.setInstanceFollowRedirects(false);
        {
            int responseCode = conn.getResponseCode();
            String amsg = conn.getResponseMessage();
            getCookies(conn);
            getFormValues(conn);
            System.out.println("FunBeat.START_URL => code: " + responseCode + "(" + amsg + "), cookies: "
                    + cookies.size() + ", values: " + formValues.size());
        }
        conn.disconnect();

        /**
         * Then login using a post
         */
        FormValues kv = new FormValues();
        String viewKey = findName(formValues.keySet(), "VIEWSTATE");
        String eventKey = findName(formValues.keySet(), "EVENTVALIDATION");
        String userKey = findName(formValues.keySet(), "Username");
        String passKey = findName(formValues.keySet(), "Password");
        String loginKey = findName(formValues.keySet(), "LoginButton");
        kv.put(viewKey, formValues.get(viewKey));
        kv.put(eventKey, formValues.get(eventKey));
        kv.put(userKey, username);
        kv.put(passKey, password);
        kv.put(loginKey, "Logga in");

        conn = (HttpURLConnection) new URL(LOGIN_URL).openConnection();
        conn.setInstanceFollowRedirects(false);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        addCookies(conn);

        boolean ok = false;
        {
            OutputStream wr = new BufferedOutputStream(conn.getOutputStream());
            kv.write(wr);
            wr.flush();
            wr.close();
            int responseCode = conn.getResponseCode();
            String amsg = conn.getResponseMessage();
            getCookies(conn);
            if (responseCode == 302) {
                String redirect = conn.getHeaderField("Location");
                conn.disconnect();
                conn = (HttpURLConnection) new URL(BASE_URL + redirect).openConnection();
                conn.setInstanceFollowRedirects(false);
                conn.setRequestMethod("GET");
                addCookies(conn);
                responseCode = conn.getResponseCode();
                amsg = conn.getResponseMessage();
                getCookies(conn);
            } else if (responseCode != 200) {
                System.err.println("FunBeatUploader::connect() - got " + responseCode + ", msg: " + amsg);
            }
            String html = getFormValues(conn);
            ok = html.indexOf("Logga ut") > 0;

            conn.disconnect();
        }

        if (ok) {
            return Uploader.Status.OK;
        } else {
            return s;
        }
    } catch (MalformedURLException e) {
        ex = e;
    } catch (IOException e) {
        ex = e;
    }

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

    s.ex = ex;
    if (ex != null) {
        ex.printStackTrace();
    }
    return s;
}

From source file:org.runnerup.export.FunBeatSynchronizer.java

@Override
public Status connect() {
    Exception ex = null;/* www  . j  a v  a  2  s  .c  o  m*/
    HttpURLConnection conn = null;
    cookies.clear();
    formValues.clear();

    Status s = Status.NEED_AUTH;
    s.authMethod = AuthMethod.USER_PASS;
    if (username == null || password == null) {
        return s;
    }

    if (loginID == null || loginSecretHashed == null) {
        if (!validateAndCreateSecrets(username, password))
            return s;
    }

    try {
        /**
         * connect to START_URL to get cookies/formValues
         */
        conn = (HttpURLConnection) new URL(START_URL).openConnection();
        conn.setInstanceFollowRedirects(false);
        {
            int responseCode = conn.getResponseCode();
            String amsg = conn.getResponseMessage();
            getCookies(conn);
            getFormValues(conn);
            Log.i(getName(), "FunBeat.START_URL => code: " + responseCode + "(" + amsg + "), cookies: "
                    + cookies.size() + ", values: " + formValues.size());
        }
        conn.disconnect();

        /**
         * Then login using a post
         */
        FormValues kv = new FormValues();
        String viewKey = SyncHelper.findName(formValues.keySet(), "VIEWSTATE");
        String eventKey = SyncHelper.findName(formValues.keySet(), "EVENTVALIDATION");
        String userKey = SyncHelper.findName(formValues.keySet(), "Username");
        String passKey = SyncHelper.findName(formValues.keySet(), "Password");
        String loginKey = SyncHelper.findName(formValues.keySet(), "LoginButton");
        kv.put(viewKey, formValues.get(viewKey));
        kv.put(eventKey, formValues.get(eventKey));
        kv.put(userKey, username);
        kv.put(passKey, password);
        kv.put(loginKey, "Logga in");

        conn = (HttpURLConnection) new URL(LOGIN_URL).openConnection();
        conn.setInstanceFollowRedirects(false);
        conn.setDoOutput(true);
        conn.setRequestMethod(RequestMethod.POST.name());
        conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        addCookies(conn);

        boolean ok = false;
        {
            OutputStream wr = new BufferedOutputStream(conn.getOutputStream());
            kv.write(wr);
            wr.flush();
            wr.close();
            int responseCode = conn.getResponseCode();
            String amsg = conn.getResponseMessage();
            getCookies(conn);
            if (responseCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                String redirect = conn.getHeaderField("Location");
                conn.disconnect();
                conn = (HttpURLConnection) new URL(BASE_URL + redirect).openConnection();
                conn.setInstanceFollowRedirects(false);
                conn.setRequestMethod(RequestMethod.GET.name());
                addCookies(conn);
                responseCode = conn.getResponseCode();
                amsg = conn.getResponseMessage();
                getCookies(conn);
            } else if (responseCode != HttpStatus.SC_OK) {
                Log.e(getName(), "FunBeatSynchronizer::connect() - got " + responseCode + ", msg: " + amsg);
            }
            String html = getFormValues(conn);
            ok = html.indexOf("Logga ut") > 0;

            conn.disconnect();
        }

        if (ok) {
            return Synchronizer.Status.OK;
        } else {
            return s;
        }
    } catch (MalformedURLException e) {
        ex = e;
    } catch (IOException e) {
        ex = e;
    }

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

    s.ex = ex;
    if (ex != null) {
        ex.printStackTrace();
    }
    return s;
}

From source file:org.codehaus.wadi.web.impl.StandardHttpProxy.java

protected void doProxy(URI uri, WebInvocation context) throws ProxyingException {
    HttpServletRequest req = context.getHreq();
    HttpServletResponse res = context.getHres();

    String requestURI = getRequestURI(req);
    String qs = req.getQueryString();
    if (qs != null) {
        requestURI = new StringBuffer(requestURI).append("?").append(qs).toString();
    }/*  w  ww .j  av  a 2 s  .  c  o m*/

    URL url = null;
    try {
        url = new URL("http", uri.getHost(), uri.getPort(), requestURI);
        if (_log.isTraceEnabled())
            _log.trace("proxying to: " + url);
    } catch (MalformedURLException e) {
        if (_log.isWarnEnabled())
            _log.warn("bad proxy url: " + url, e);
        throw new IrrecoverableException("bad proxy url", e);
    }

    long startTime = System.currentTimeMillis();

    HttpURLConnection huc = null;
    String m = req.getMethod();
    try {
        huc = (HttpURLConnection) url.openConnection(); // IOException
        huc.setRequestMethod(m); // ProtocolException
    } catch (ProtocolException e) {
        if (_log.isWarnEnabled())
            _log.warn("unsupported http method: " + m, e);
        throw new IrrecoverableException("unsupported HTTP method: " + m, e);
    } catch (IOException e) {
        if (_log.isWarnEnabled())
            _log.warn("proxy IO problem", e);
        throw new RecoverableException("could not open proxy connection", e);
    }

    huc.setAllowUserInteraction(false);
    huc.setInstanceFollowRedirects(false);

    // check connection header
    // TODO - this might need some more time: see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
    String connectionHdr = req.getHeader("Connection"); // TODO - what if there are multiple values ?
    if (connectionHdr != null) {
        connectionHdr = connectionHdr.toLowerCase();
        if (connectionHdr.equals("keep-alive") || connectionHdr.equals("close"))
            connectionHdr = null; // TODO  ??
    }

    // copy headers - inefficient, but we are constrained by servlet API
    {
        for (Enumeration e = req.getHeaderNames(); e.hasMoreElements();) {
            String hdr = (String) e.nextElement();
            String lhdr = hdr.toLowerCase();

            if (_DontProxyHeaders.contains(lhdr))
                continue;
            if (connectionHdr != null && connectionHdr.indexOf(lhdr) >= 0) // what is going on here ?
                continue;
            // HTTP/1.1 proxies MUST parse the Connection header field before a message is forwarded and, for each connection-token in this field, remove any header field(s) from the message with the same name as the connection-token. Connection options are signaled by the presence of a connection-token in the Connection header field, not by any corresponding additional header field(s), since the additional header field may not be sent if there are no parameters associated with that connection option
            if (_WADI_IsSecure.equals(hdr)) // don't worry about case - we should be the only one messing with this header...
                continue; // strip this out - we may be being spoofed

            for (Enumeration f = req.getHeaders(hdr); f.hasMoreElements();) {
                String val = (String) f.nextElement();
                if (val != null) {
                    huc.addRequestProperty(hdr, val);
                }
            }
        }
    }

    // content ?
    boolean hasContent = false;
    {
        int contentLength = 0;
        String tmp = huc.getRequestProperty("Content-Length");
        if (tmp != null) {
            try {
                contentLength = Integer.parseInt(tmp);
            } catch (NumberFormatException ignore) {
                // ignore
            }
        }

        if (contentLength > 0)
            hasContent = true;
        else
            hasContent = (huc.getRequestProperty("Content-Type") != null);
    }

    // proxy
    {
        huc.addRequestProperty("Via", "1.1 " + req.getLocalName() + ":" + req.getLocalPort() + " \"WADI\""); // TODO - should we be giving out personal details ?
        huc.addRequestProperty("X-Forwarded-For", req.getRemoteAddr()); // adds last link in request chain...
        // String tmp=uc.getRequestProperty("Max-Forwards"); // TODO - do we really need to bother with this ?
    }

    // cache-control
    {
        String cacheControl = huc.getRequestProperty("Cache-Control");
        if (cacheControl != null
                && (cacheControl.indexOf("no-cache") >= 0 || cacheControl.indexOf("no-store") >= 0))
            huc.setUseCaches(false);
    }

    // confidentiality
    {
        if (req.isSecure()) {
            huc.addRequestProperty(_WADI_IsSecure, req.getLocalAddr().toString());
        }

        // at the other end, if this header is present we must :

        // wrap the request so that req.isSecure()=true, before processing...
        // mask the header - so it is never seen by the app.

        // the code for the other end should live in this class.

        // this code should also confirm that it not being spoofed by confirming that req.getRemoteAddress() is a cluster member...
    }
    // customize Connection
    huc.setDoInput(true);

    // client->server
    int client2ServerTotal = 0;
    {
        if (hasContent) {
            huc.setDoOutput(true);

            OutputStream toServer = null;
            try {
                InputStream fromClient = req.getInputStream(); // IOException
                toServer = huc.getOutputStream(); // IOException
                client2ServerTotal = copy(fromClient, toServer, 8192);
            } catch (IOException e) {
                new IrrecoverableException("problem proxying client request to server", e);
            } finally {
                if (toServer != null) {
                    try {
                        toServer.close(); // IOException
                    } catch (IOException e) {
                        _log.warn("problem closing server request stream", e);
                    }
                }
            }
        }
    }

    // Connect
    try {
        huc.connect(); // IOException
    } catch (IOException e) {
        if (_log.isWarnEnabled())
            _log.warn("proxy connection problem: " + url, e);
        throw new RecoverableException("could not connect to proxy target", e);
    }

    InputStream fromServer = null;

    // handler status codes etc.
    int code = 0;
    if (huc == null) {
        try {
            fromServer = huc.getInputStream(); // IOException
        } catch (IOException e) {
            if (_log.isWarnEnabled())
                _log.warn("proxying problem", e);
            throw new IrrecoverableException("problem acquiring client output", e);
        }
    } else {
        code = 502;
        //         String message="Bad Gateway: could not read server response code or message";
        try {
            code = huc.getResponseCode(); // IOException
            //            message=huc.getResponseMessage(); // IOException
        } catch (IOException e) {
            if (_log.isWarnEnabled())
                _log.warn("proxying problem", e);
            throw new IrrecoverableException("problem acquiring http server response code/message", e);
        } finally {
            //            res.setStatus(code, message); - deprecated
            res.setStatus(code);
        }

        if (code < 400) {
            // 1XX:continue, 2XX:successful, 3XX:multiple-choices...
            try {
                fromServer = huc.getInputStream(); // IOException
            } catch (IOException e) {
                if (_log.isWarnEnabled())
                    _log.warn("proxying problem", e);
                throw new IrrecoverableException("problem acquiring http client output", e);
            }
        } else {
            // 4XX:client, 5XX:server error...
            fromServer = huc.getErrorStream(); // why does this not throw IOException ?
            // TODO - do we need to use sendError()?
        }
    }

    // clear response defaults.
    res.setHeader("Date", null);
    res.setHeader("Server", null);

    // set response headers
    if (false) {
        int h = 0;
        String hdr = huc.getHeaderFieldKey(h);
        String val = huc.getHeaderField(h);
        while (hdr != null || val != null) {
            String lhdr = (hdr != null) ? hdr.toLowerCase() : null;
            if (hdr != null && val != null && !_DontProxyHeaders.contains(lhdr))
                res.addHeader(hdr, val);

            // if (_log.isDebugEnabled()) _log.debug("res " + hdr + ": " + val);

            h++;
            hdr = huc.getHeaderFieldKey(h);
            val = huc.getHeaderField(h);
        }
    } else {
        // TODO - is it a bug in Jetty that I have to start my loop at 1 ? or that key[0]==null ?
        // Try this inside Tomcat...
        String key;
        for (int i = 1; (key = huc.getHeaderFieldKey(i)) != null; i++) {
            key = key.toLowerCase();
            String val = huc.getHeaderField(i);
            if (val != null && !_DontProxyHeaders.contains(key)) {
                res.addHeader(key, val);
            }
        }
    }

    // do we need another Via header in the response...

    // server->client
    int server2ClientTotal = 0;
    {
        if (fromServer != null) {
            try {
                OutputStream toClient = res.getOutputStream();// IOException
                server2ClientTotal += copy(fromServer, toClient, 8192);// IOException
            } catch (IOException e) {
                if (_log.isWarnEnabled())
                    _log.warn("proxying problem", e);
                throw new IrrecoverableException("problem proxying server response back to client", e);
            } finally {
                try {
                    fromServer.close();
                } catch (IOException e) {
                    // well - we did our best...
                    _log.warn("problem closing server response stream", e);
                }
            }
        }
    }

    huc.disconnect();

    long endTime = System.currentTimeMillis();
    long elapsed = endTime - startTime;
    if (_log.isDebugEnabled())
        _log.debug("in:" + client2ServerTotal + ", out:" + server2ClientTotal + ", status:" + code + ", time:"
                + elapsed + ", url:" + url);
}

From source file:com.emc.ecs.smart.SmartUploader.java

/**
 * Does a standard PUT upload using HttpURLConnection.
 *//*from ww w .ja  v  a2  s. c o m*/
private void doSimpleUpload() {
    try {
        fileSize = Files.size(fileToUpload);
        HttpURLConnection conn = null;
        long start = System.currentTimeMillis();
        try (DigestInputStream is = new DigestInputStream(Files.newInputStream(fileToUpload),
                MessageDigest.getInstance("MD5"))) {
            conn = (HttpURLConnection) uploadUrl.openConnection();
            conn.setFixedLengthStreamingMode(fileSize);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setInstanceFollowRedirects(false);
            conn.setUseCaches(false);
            conn.setRequestMethod(HttpMethod.PUT);
            conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM);
            OutputStream os = conn.getOutputStream();
            byte[] buf = new byte[CHUNK_SIZE];
            int len;

            while ((len = is.read(buf)) != -1) {
                os.write(buf, 0, len);
                bytesUploaded += len;
                printPercent();
            }
            os.close();

            if (conn.getResponseCode() != ClientResponse.Status.OK.getStatusCode()) {
                throw new RuntimeException("Unable to upload object content: status=" + conn.getResponseCode());
            } else {
                List<String> eTags = conn.getHeaderFields().get(HttpHeaders.ETAG);
                if (eTags == null || eTags.size() < 1) {
                    throw new RuntimeException("Unable to get ETag for uploaded data");
                } else {
                    byte[] sentMD5 = is.getMessageDigest().digest();
                    String eTag = eTags.get(0);
                    byte[] gotMD5 = DatatypeConverter.parseHexBinary(eTag.substring(1, eTag.length() - 1));
                    if (!Arrays.equals(gotMD5, sentMD5)) {
                        throw new RuntimeException("ETag doesn't match streamed data's MD5.");
                    }
                }
            }
        } catch (IOException e) {
            throw new Exception("IOException while uploading object content after writing " + bytesUploaded
                    + " of " + fileSize + " bytes: " + e.getMessage());
        } finally {
            if (conn != null)
                conn.disconnect();
        }

        long elapsed = System.currentTimeMillis() - start;
        printRate(fileSize, elapsed);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ca.osmcanada.osvuploadr.JPMain.java

private String sendForm(String target_url, Map<String, String> arguments, String method,
        List<Cookie> cookielist) {
    try {/*from  ww w . j ava2 s.  c om*/
        URL url = new URL(target_url);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        //HttpURLConnection http = (HttpURLConnection)con;
        con.setRequestMethod(method); // PUT is another valid option
        con.setDoOutput(true);
        con.setInstanceFollowRedirects(false);
        String cookiestr = "";
        if (cookielist != null) {
            if (cookielist.size() > 0) {
                for (Cookie cookie : cookielist) {
                    if (!cookiestr.equals("")) {
                        cookiestr += ";" + cookie.getName() + "=" + cookie.getValue();
                    } else {
                        cookiestr += cookie.getName() + "=" + cookie.getValue();
                    }
                }
                con.setRequestProperty("Cookie", cookiestr);
            }
        }

        con.setReadTimeout(5000);

        StringJoiner sj = new StringJoiner("&");
        for (Map.Entry<String, String> entry : arguments.entrySet())
            sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "="
                    + URLEncoder.encode(entry.getValue(), "UTF-8"));
        byte[] out = sj.toString().getBytes(StandardCharsets.UTF_8);
        int length = out.length;

        con.setFixedLengthStreamingMode(length);
        con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        con.setRequestProperty("Accept-Language", "en-us;");
        con.connect();
        try (OutputStream os = con.getOutputStream()) {
            os.write(out);
            os.close();
        }

        boolean redirect = false;
        int status = con.getResponseCode();

        if (status != HttpURLConnection.HTTP_OK) {
            if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM
                    || status == HttpURLConnection.HTTP_SEE_OTHER)
                redirect = true;
        }

        if (redirect) {
            String newURL = con.getHeaderField("Location");
            String cookies = con.getHeaderField("Set-Cookie");
            if (cookies == null) {
                cookies = cookiestr;
            }
            con = (HttpURLConnection) new URL(newURL).openConnection();
            con.setRequestProperty("Cookie", cookies);
        }

        InputStream is = con.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte buf[] = new byte[1024];
        int letti;

        while ((letti = is.read(buf)) > 0)
            baos.write(buf, 0, letti);

        String data = new String(baos.toByteArray(), Charset.forName("UTF-8"));
        con.disconnect();

        return data;

    } catch (Exception ex) {
        Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "";
}

From source file:com.openshift.internal.restclient.http.UrlConnectionHttpClient.java

protected HttpURLConnection createConnection(URL url, String userAgent, String acceptedVersion,
        String acceptedMediaType, ISSLCertificateCallback callback, int timeout) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    if (isHttps(url)) {
        HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
        SSLContext sslContext = setSSLCallback(sslAuthorizationCallback, url, httpsConnection);
        setFilteredCiphers(excludedSSLCipherRegex, sslContext, httpsConnection);
    }/* w ww. j  a  v  a2s  .com*/
    setAuthorization(connection);
    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setAllowUserInteraction(false);
    setConnectTimeout(NO_TIMEOUT, connection);
    setReadTimeout(timeout, connection);
    // wont work when switching http->https
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4620571
    connection.setInstanceFollowRedirects(true);
    setUserAgent(userAgent, connection);
    setAcceptHeader(acceptedVersion, acceptedMediaType, connection);

    return connection;
}

From source file:pt.aptoide.backupapps.data.webservices.ManagerUploads.java

public EnumServerUploadApkStatus uploadApk(ViewApk viewApk, boolean justMd5) {
    EnumServerUploadApkStatus status = EnumServerUploadApkStatus.SUCCESS;
    String apkPath = viewApk.getPath();
    String token = serviceData.getManagerPreferences().getToken();

    String body = formPart("uploadType", "aptbackup");

    if (viewApk.getCategory() != null) {
        body += formPart("category", viewApk.getCategory());
    }/*from w w  w  . ja va2  s .  c  om*/
    if (viewApk.getDescription() != null) {
        body += formPart("description", viewApk.getDescription());
    }
    if (viewApk.getPhone() != null) {
        body += formPart("apk_phone", viewApk.getPhone());
    }
    if (viewApk.getEmail() != null) {
        body += formPart("apk_email", viewApk.getEmail());
    }
    if (viewApk.getWebURL() != null) {
        body += formPart("apk_website", viewApk.getWebURL());
    }

    if (justMd5) {
        ViewCache apk = serviceData.getManagerCache().getNewViewCache(viewApk.getPath());
        serviceData.getManagerCache().calculateMd5Hash(apk);
        Log.d("Aptoide-ManagerUploads",
                "UploadApk " + viewApk.getPath() + " - using just md5: " + apk.getMd5sum());
        body += formPart("apk_md5sum", apk.getMd5sum());
    } else {
        Log.d("Aptoide-ManagerUploads", "UploadApk " + viewApk.getPath());
    }

    body += formPart("token", token) + formPart("repo", viewApk.getRepository())
            + formPart("apkname", viewApk.getName()) + formPart("rating", viewApk.getRating())
            + formPart("mode", "xml");

    if (!justMd5) {
        body += formBinaryPartNoTail("apk", "application/vnd.android.package-archive");
    }

    DataOutputStream outputStream = null;

    try {
        URL url = new URL(Constants.URI_UPLOAD_WS);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // This fixes #515 : Out of memory bug
        connection.setChunkedStreamingMode(CHUNK_SIZE);
        connection.setConnectTimeout(120000);
        connection.setReadTimeout(120000);

        // Allow Inputs & Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        // Enable POST method
        connection.setRequestMethod("POST");

        connection.setInstanceFollowRedirects(true);

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

        connection.setRequestProperty("User-Agent", getUserAgentString());

        outputStream = new DataOutputStream(connection.getOutputStream());

        outputStream.writeBytes(body);

        if (!justMd5) {
            FileInputStream apk = new FileInputStream(apkPath);
            byte data[] = new byte[CHUNK_SIZE];
            long lenTotal = 0;
            int read;
            long uploadSize = viewApk.getSize();
            int progressPercentage = 0;
            while ((read = apk.read(data, 0, CHUNK_SIZE)) != -1) {
                outputStream.write(data, 0, read);
                lenTotal += read;
                int newProgressPercentage = (int) (lenTotal * 100 / uploadSize);
                Log.d("OutputApk", "sent: " + read + "bytes, Total: " + lenTotal + " completion: "
                        + newProgressPercentage + "% app: " + viewApk.getName());
                if (newProgressPercentage > (progressPercentage + 10)) {
                    progressPercentage = newProgressPercentage;
                    serviceData.uploadingProgressUpdate(viewApk.getAppHashid(), progressPercentage);
                }
            }
        }

        outputStream.writeBytes(LINE_END + TWO_HYPHENS + boundary + TWO_HYPHENS + LINE_END);

        outputStream.flush();
        outputStream.close();

        status = serviceData.getManagerXml().dom.parseApkUploadXml(connection);

    } catch (Exception e) {
        status = EnumServerUploadApkStatus.CONNECTION_ERROR;
        e.printStackTrace();
        return status;
    }
    //      catch (MalformedURLException e) {
    //         // TODO Auto-generated catch block
    //         e.printStackTrace();
    //      } catch (ProtocolException e) {
    //         // TODO Auto-generated catch block
    //         e.printStackTrace();
    //      } catch (FileNotFoundException e) {
    //         // TODO Auto-generated catch block
    //         e.printStackTrace();
    //      } catch (IOException e) {
    //         // TODO Auto-generated catch block
    //         e.printStackTrace();
    //      }  catch (JSONException e) {
    //         // TODO Auto-generated catch block
    //         e.printStackTrace();
    //      } catch (UnsuccessfullSubmitException e) {
    //         // TODO Auto-generated catch block
    //         e.printStackTrace();
    //      }

    return status;
}

From source file:eionet.cr.harvest.PullHarvest.java

/**
 *
 * @param connectUrl//  www.  j a va 2  s .co  m
 * @return
 * @throws IOException
 * @throws DAOException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
private HttpURLConnection openUrlConnection(String connectUrl)
        throws IOException, DAOException, SAXException, ParserConfigurationException {

    String sanitizedUrl = StringUtils.substringBefore(connectUrl, "#");
    sanitizedUrl = StringUtils.replace(sanitizedUrl, " ", "%20");

    HttpURLConnection connection = (HttpURLConnection) new URL(sanitizedUrl).openConnection();
    connection.setRequestProperty("Accept", ACCEPT_HEADER);
    connection.setRequestProperty("User-Agent", URLUtil.userAgentHeader());
    connection.setRequestProperty("Connection", "close");
    connection.setInstanceFollowRedirects(false);

    // Set the timeout both for establishing the connection, and reading from it once established.
    int httpTimeout = GeneralConfig.getIntProperty(GeneralConfig.HARVESTER_HTTP_TIMEOUT, getTimeout());
    connection.setConnectTimeout(httpTimeout);
    connection.setReadTimeout(httpTimeout);

    // Use "If-Modified-Since" header, if this is not an on-demand harvest
    if (!isOnDemandHarvest) {

        // "If-Modified-Since" will be compared to this URL's last harvest
        Date lastHarvestDate = getContextSourceDTO().getLastHarvest();
        long lastHarvest = lastHarvestDate == null ? 0L : lastHarvestDate.getTime();
        if (lastHarvest > 0) {

            // Check if this URL has a conversion stylesheet, and if the latter has been modified since last harvest.
            String conversionStylesheetUrl = getConversionStylesheetUrl(sanitizedUrl);
            boolean hasConversion = !StringUtils.isBlank(conversionStylesheetUrl);
            boolean hasModifiedConversion = hasConversion
                    && URLUtil.isModifiedSince(conversionStylesheetUrl, lastHarvest);

            // Check if post-harvest scripts are updated
            boolean scriptsModified = DAOFactory.get().getDao(PostHarvestScriptDAO.class)
                    .isScriptsModified(lastHarvestDate, getContextSourceDTO().getUrl());

            // "If-Modified-Since" should only be set if there is no modified conversion or post-harvest scripts for this URL.
            // Because if there is a conversion stylesheet or post-harvest scripts, and any of them has been modified since last
            // harvest, we surely want to get the content again and run the conversion or script on the content, regardless of
            // when the content itself was last modified.
            if (!hasModifiedConversion && !scriptsModified) {
                LOGGER.debug(loggerMsg(
                        "Using if-modified-since, compared to last harvest " + formatDate(lastHarvestDate)));
                connection.setIfModifiedSince(lastHarvest);
            }
        }
    }

    return connection;
}