Example usage for java.net URLConnection setDoInput

List of usage examples for java.net URLConnection setDoInput

Introduction

In this page you can find the example usage for java.net URLConnection setDoInput.

Prototype

public void setDoInput(boolean doinput) 

Source Link

Document

Sets the value of the doInput field for this URLConnection to the specified value.

Usage

From source file:com.ikanow.infinit.e.harvest.enrichment.custom.UnstructuredAnalysisHarvester.java

public void getRawTextFromUrlIfNeeded(DocumentPojo doc, SourceRssConfigPojo feedConfig) throws IOException {
    if (null != doc.getFullText()) { // Nothing to do
        return;/*from   www  .ja  v a2  s. co  m*/
    }
    Scanner s = null;
    OutputStreamWriter wr = null;
    try {
        URL url = new URL(doc.getUrl());
        URLConnection urlConnect = null;
        String postContent = null;
        if (null != feedConfig) {
            urlConnect = url.openConnection(ProxyManager.getProxy(url, feedConfig.getProxyOverride()));
            if (null != feedConfig.getUserAgent()) {
                urlConnect.setRequestProperty("User-Agent", feedConfig.getUserAgent());
            } // TESTED (by hand)
            if (null != feedConfig.getHttpFields()) {
                for (Map.Entry<String, String> httpFieldPair : feedConfig.getHttpFields().entrySet()) {
                    if (httpFieldPair.getKey().equalsIgnoreCase("content")) {
                        postContent = httpFieldPair.getValue();
                        urlConnect.setDoInput(true);
                        urlConnect.setDoOutput(true);
                    } else {
                        urlConnect.setRequestProperty(httpFieldPair.getKey(), httpFieldPair.getValue());
                    }
                }
            } //TESTED (by hand)
        } else {
            urlConnect = url.openConnection();
        }
        InputStream urlStream = null;
        try {
            securityManager.setSecureFlag(true); // (disallow file/local URL access)
            if (null != postContent) {
                wr = new OutputStreamWriter(urlConnect.getOutputStream());
                wr.write(postContent.toCharArray());
                wr.flush();
            } //TESTED
            urlStream = urlConnect.getInputStream();
        } catch (SecurityException se) {
            throw se;
        } catch (Exception e) { // Try one more time, this time exception out all the way
            securityManager.setSecureFlag(false); // (some file stuff - so need to re-enable)
            if (null != feedConfig) {
                urlConnect = url.openConnection(ProxyManager.getProxy(url, feedConfig.getProxyOverride()));
                if (null != feedConfig.getUserAgent()) {
                    urlConnect.setRequestProperty("User-Agent", feedConfig.getUserAgent());
                } // TESTED
                if (null != feedConfig.getHttpFields()) {
                    for (Map.Entry<String, String> httpFieldPair : feedConfig.getHttpFields().entrySet()) {
                        if (httpFieldPair.getKey().equalsIgnoreCase("content")) {
                            urlConnect.setDoInput(true); // (need to do this again)
                            urlConnect.setDoOutput(true);
                        } else {
                            urlConnect.setRequestProperty(httpFieldPair.getKey(), httpFieldPair.getValue());
                        }
                    }
                } //TESTED
            } else {
                urlConnect = url.openConnection();
            }
            securityManager.setSecureFlag(true); // (disallow file/local URL access)
            if (null != postContent) {
                wr = new OutputStreamWriter(urlConnect.getOutputStream());
                wr.write(postContent.toCharArray());
                wr.flush();
            } //TESTED
            urlStream = urlConnect.getInputStream();
        } finally {
            securityManager.setSecureFlag(false); // (turn security check for local URL/file access off)
        }
        // Grab any interesting header fields
        Map<String, List<String>> headers = urlConnect.getHeaderFields();
        BasicDBObject metadataHeaderObj = null;
        for (Map.Entry<String, List<String>> it : headers.entrySet()) {
            if (null != it.getKey()) {
                if (it.getKey().startsWith("X-") || it.getKey().startsWith("Set-")
                        || it.getKey().startsWith("Location")) {
                    if (null == metadataHeaderObj) {
                        metadataHeaderObj = new BasicDBObject();
                    }
                    metadataHeaderObj.put(it.getKey(), it.getValue());
                }
            }
        } //TESTED
          // Grab the response code
        try {
            HttpURLConnection httpUrlConnect = (HttpURLConnection) urlConnect;
            int responseCode = httpUrlConnect.getResponseCode();
            if (200 != responseCode) {
                if (null == metadataHeaderObj) {
                    metadataHeaderObj = new BasicDBObject();
                }
                metadataHeaderObj.put("responseCode", String.valueOf(responseCode));
            }
        } //TESTED
        catch (Exception e) {
        } // interesting, not an HTTP connect ... shrug and carry on
        if (null != metadataHeaderObj) {
            doc.addToMetadata("__FEED_METADATA__", metadataHeaderObj);
        } //TESTED
        s = new Scanner(urlStream, "UTF-8");
        doc.setFullText(s.useDelimiter("\\A").next());
    } catch (MalformedURLException me) { // This one is worthy of a more useful error message
        throw new MalformedURLException(me.getMessage()
                + ": Likely because the document has no full text (eg JSON) and you are calling a contentMetadata block without setting flags:'m' or 'd'");
    } finally { //(release resources)
        if (null != s) {
            s.close();
        }
        if (null != wr) {
            wr.close();
        }
    }

}

From source file:com.dragonflow.StandardMonitor.URLOriginalMonitor.java

private static long getURLStatus_ForBackupToRegularMeansOnly(SocketSession socketsession, long l, String s,
        int i, int j) {
    if (i >= j) {
        System.out.println("URLOriginalMonitor KEEPTRYING!!! - URL: " + s + "FAILED THIS MANY TIMES: " + i);
        return l;
    }/*from w ww .ja  va2  s  .com*/
    if (l != 200L && l != 302L && l != 301L) {
        System.out.println(
                "URLOriginalMonitor KEEPTRYING!!! - URL: " + s + " status: " + l + " times attempted: " + i);
        int k = 0;
        if (socketsession.context.getSetting("_timeout").length() > 0) {
            k = TextUtils.toInt(socketsession.context.getSetting("_timeout")) * 1000;
        } else {
            k = 60000;
        }
        Platform.sleep(k);
        StringBuffer stringbuffer = new StringBuffer();
        try {
            URL url = new URL(s);
            URLConnection urlconnection = url.openConnection();
            System.out.println(
                    "URLOriginalMonitor KEEPTRYING Received a : " + urlconnection.getClass().getName());
            System.out.println(
                    "URLOriginalMonitor KEEPTRYING Received connection string : " + urlconnection.toString());
            urlconnection.setDoInput(true);
            urlconnection.setUseCaches(false);
            System.out.println("URLOriginalMonitor KEEPTRYING Getting an input stream...");
            java.io.InputStream inputstream = urlconnection.getInputStream();
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
            for (String s5 = null; (s5 = bufferedreader.readLine()) != null;) {
                stringbuffer.append(s5);
            }

            System.out.println("URLOriginalMonitor KEEPTRYING CONTENTS: " + stringbuffer.toString());
        } catch (MalformedURLException malformedurlexception) {
            String s1 = "URLOriginalMonitor KEEPTRYING error attempted backup URL retrieval: " + s
                    + " exception: " + malformedurlexception.toString();
            LogManager.log("Error", s1);
            LogManager.log("Error", FileUtils.stackTraceText(malformedurlexception));
            System.out.println(s1);
            malformedurlexception.printStackTrace();
            return getURLStatus_ForBackupToRegularMeansOnly(socketsession, l, s, ++i, j);
        } catch (IOException ioexception) {
            String s2 = "URLOriginalMonitor KEEPTRYING error attempted backup URL retrieval: " + s
                    + " exception: " + ioexception.toString();
            LogManager.log("Error", s2);
            LogManager.log("Error", FileUtils.stackTraceText(ioexception));
            System.out.println(s2);
            ioexception.printStackTrace();
            return getURLStatus_ForBackupToRegularMeansOnly(socketsession, l, s, ++i, j);
        } catch (Exception exception) {
            String s3 = "URLOriginalMonitor KEEPTRYING error attempted backup URL retrieval: " + s
                    + " exception: " + exception.toString();
            LogManager.log("Error", s3);
            LogManager.log("Error", FileUtils.stackTraceText(exception));
            System.out.println(s3);
            exception.printStackTrace();
            return getURLStatus_ForBackupToRegularMeansOnly(socketsession, l, s, ++i, j);
        } catch (Throwable throwable) {
            String s4 = "URLOriginalMonitor KEEPTRYING error attempted backup URL retrieval: " + s
                    + " exception: " + throwable.toString();
            LogManager.log("Error", s4);
            LogManager.log("Error", FileUtils.stackTraceText(throwable));
            System.out.println(s4);
            throwable.printStackTrace();
            return getURLStatus_ForBackupToRegularMeansOnly(socketsession, l, s, ++i, j);
        }
        return 200L;
    } else {
        return l;
    }
}

From source file:com.amalto.workbench.utils.Util.java

public static String getResponseFromURL(String url, TreeObject treeObj) throws Exception {
    InputStreamReader doc = null;
    try {/*from  www.  jav a 2  s.  com*/
        Encoder encoder = Base64.getEncoder();
        StringBuffer buffer = new StringBuffer();
        String credentials = encoder.encodeToString((new String(treeObj.getServerRoot().getUsername() + ":"//$NON-NLS-1$
                + treeObj.getServerRoot().getPassword()).getBytes()));

        URL urlCn = new URL(url);
        URLConnection conn = urlCn.openConnection();
        conn.setAllowUserInteraction(true);
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestProperty("Authorization", "Basic " + credentials);//$NON-NLS-1$//$NON-NLS-2$
        conn.setRequestProperty("Expect", "100-continue");//$NON-NLS-1$//$NON-NLS-2$

        doc = new InputStreamReader(conn.getInputStream());
        BufferedReader reader = new BufferedReader(doc);
        String line = reader.readLine();
        while (line != null) {
            buffer.append(line);
            line = reader.readLine();
        }

        return buffer.toString();
    } finally {
        if (doc != null) {
            doc.close();
        }
    }
}

From source file:com.adobe.aem.demomachine.communities.Loader.java

private static void postAnalytics(String analytics, String body) {

    if (analytics != null && body != null) {

        URLConnection urlConn = null;
        DataOutputStream printout = null;
        BufferedReader input = null;
        String tmp = null;//ww w . ja  v a 2s.  co m
        try {

            logger.debug("New Analytics Event: " + body);

            URL sitecaturl = new URL("http://" + analytics);

            urlConn = sitecaturl.openConnection();
            urlConn.setDoInput(true);
            urlConn.setDoOutput(true);
            urlConn.setUseCaches(false);
            urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            printout = new DataOutputStream(urlConn.getOutputStream());

            printout.writeBytes(body);
            printout.flush();
            printout.close();

            input = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

            while (null != ((tmp = input.readLine()))) {
                logger.debug(tmp);
            }
            printout.close();
            input.close();

        } catch (Exception ex) {

            logger.warn("Connectivity error: " + ex.getMessage());

        } finally {

            try {
                input.close();
                printout.close();
            } catch (Exception e) {
                // Omitted
            }

        }

    }

}

From source file:ch.unifr.pai.twice.widgets.mpproxy.server.JettyProxy.java

public ProcessResult loadFromProxy(HttpServletRequest request, HttpServletResponse response, String uri,
        String servletPath, String proxyPath) throws ServletException, IOException {
    //System.out.println("LOAD "+uri); 
    //System.out.println("LOAD "+proxyPath);

    if ("CONNECT".equalsIgnoreCase(request.getMethod())) {
        handleConnect(request, response);

    } else {//from  w  ww.j a v a  2 s. c  o m
        URL url = new URL(uri);

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

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

        // check connection header
        String connectionHdr = request.getHeader("Connection");
        if (connectionHdr != null) {
            connectionHdr = connectionHdr.toLowerCase();
            if (connectionHdr.equals("keep-alive") || connectionHdr.equals("close"))
                connectionHdr = null;
        }

        // copy headers
        boolean xForwardedFor = false;
        boolean hasContent = false;
        Enumeration enm = request.getHeaderNames();
        while (enm.hasMoreElements()) {
            // TODO could be better than this!
            String hdr = (String) enm.nextElement();
            String lhdr = hdr.toLowerCase();

            if (_DontProxyHeaders.contains(lhdr))
                continue;
            if (connectionHdr != null && connectionHdr.indexOf(lhdr) >= 0)
                continue;

            if ("content-type".equals(lhdr))
                hasContent = true;

            Enumeration vals = request.getHeaders(hdr);
            while (vals.hasMoreElements()) {
                String val = (String) vals.nextElement();
                if (val != null) {
                    connection.addRequestProperty(hdr, val);
                    xForwardedFor |= "X-Forwarded-For".equalsIgnoreCase(hdr);
                }
            }
        }

        // Proxy headers
        connection.setRequestProperty("Via", "1.1 (jetty)");
        if (!xForwardedFor)
            connection.addRequestProperty("X-Forwarded-For", request.getRemoteAddr());

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

        // customize Connection

        try {
            connection.setDoInput(true);

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

            // Connect
            connection.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }

        InputStream proxy_in = null;

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

            code = http.getResponseCode();
            response.setStatus(code, http.getResponseMessage());
        }

        if (proxy_in == null) {
            try {
                proxy_in = connection.getInputStream();
            } catch (Exception e) {
                e.printStackTrace();
                proxy_in = http.getErrorStream();
            }
        }

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

        // set response headers
        int h = 0;
        String hdr = connection.getHeaderFieldKey(h);
        String val = connection.getHeaderField(h);
        while (hdr != null || val != null) {
            String lhdr = hdr != null ? hdr.toLowerCase() : null;
            if (hdr != null && val != null && !_DontProxyHeaders.contains(lhdr)) {
                if (hdr.equalsIgnoreCase("Location")) {
                    val = Rewriter.translateCleanUrl(val, servletPath, proxyPath);
                }
                response.addHeader(hdr, val);

            }

            h++;
            hdr = connection.getHeaderFieldKey(h);
            val = connection.getHeaderField(h);

        }

        boolean isGzipped = connection.getContentEncoding() != null
                && connection.getContentEncoding().contains("gzip");
        response.addHeader("Via", "1.1 (jetty)");
        // boolean process = connection.getContentType() == null
        // || connection.getContentType().isEmpty()
        // || connection.getContentType().contains("html");
        boolean process = connection.getContentType() != null && connection.getContentType().contains("text");
        if (proxy_in != null) {
            if (!process) {
                IOUtils.copy(proxy_in, response.getOutputStream());
                proxy_in.close();
            } else {
                InputStream in;
                if (isGzipped && proxy_in != null && proxy_in.available() > 0) {
                    in = new GZIPInputStream(proxy_in);
                } else {
                    in = proxy_in;
                }
                ByteArrayOutputStream byteArrOS = new ByteArrayOutputStream();
                IOUtils.copy(in, byteArrOS);
                in.close();
                if (in != proxy_in)
                    proxy_in.close();
                String charset = response.getCharacterEncoding();
                if (charset == null || charset.isEmpty()) {
                    charset = "ISO-8859-1";
                }
                String originalContent = new String(byteArrOS.toByteArray(), charset);
                byteArrOS.close();
                return new ProcessResult(originalContent, connection.getContentType(), charset, isGzipped);
            }
        }

    }
    return null;
}

From source file:com.arm.connector.bridge.transport.HttpTransport.java

@SuppressWarnings("empty-statement")
private String doHTTP(String verb, String url_str, String username, String password, String data,
        String content_type, String auth_domain, boolean doInput, boolean doOutput, boolean doSSL,
        boolean use_api_token, String api_token) {
    String result = "";
    String line = "";
    URLConnection connection = null;
    SSLContext sc = null;//from w  w w. j ava  2s  . c o  m

    try {
        URL url = new URL(url_str);

        // Http Connection and verb
        if (doSSL) {
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            } };

            // Install the all-trusting trust manager
            try {
                sc = SSLContext.getInstance("TLS");
                sc.init(null, trustAllCerts, new SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                });
            } catch (NoSuchAlgorithmException | KeyManagementException e) {
                // do nothing
                ;
            }

            // open the SSL connction
            connection = (HttpsURLConnection) (url.openConnection());
            ((HttpsURLConnection) connection).setRequestMethod(verb);
            ((HttpsURLConnection) connection).setSSLSocketFactory(sc.getSocketFactory());
            ((HttpsURLConnection) connection).setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
        } else {
            connection = (HttpURLConnection) (url.openConnection());
            ((HttpURLConnection) connection).setRequestMethod(verb);
        }

        connection.setDoInput(doInput);
        if (doOutput && data != null && data.length() > 0) {
            connection.setDoOutput(doOutput);
        } else {
            connection.setDoOutput(false);
        }

        // enable basic auth if requested
        if (use_api_token == false && username != null && username.length() > 0 && password != null
                && password.length() > 0) {
            String encoding = Base64.encodeBase64String((username + ":" + password).getBytes());
            connection.setRequestProperty("Authorization", this.m_basic_auth_qualifier + " " + encoding);
            //this.errorLogger().info("Basic Authorization: " + username + ":" + password + ": " + encoding);
        }

        // enable ApiTokenAuth auth if requested
        if (use_api_token == true && api_token != null && api_token.length() > 0) {
            // use qualification for the authorization header...
            connection.setRequestProperty("Authorization", this.m_auth_qualifier + " " + api_token);
            //this.errorLogger().info("ApiTokenAuth Authorization: " + api_token);

            // Always reset to the established default
            this.resetAuthorizationQualifier();
        }

        // ETag support if requested
        if (this.m_etag_value != null && this.m_etag_value.length() > 0) {
            // set the ETag header value
            connection.setRequestProperty("ETag", this.m_etag_value);
            //this.errorLogger().info("ETag Value: " + this.m_etag_value);

            // Always reset to the established default
            this.resetETagValue();
        }

        // If-Match support if requested
        if (this.m_if_match_header_value != null && this.m_if_match_header_value.length() > 0) {
            // set the If-Match header value
            connection.setRequestProperty("If-Match", this.m_if_match_header_value);
            //this.errorLogger().info("If-Match Value: " + this.m_if_match_header_value);

            // Always reset to the established default
            this.resetIfMatchValue();
        }

        // specify content type if requested
        if (content_type != null && content_type.length() > 0) {
            connection.setRequestProperty("Content-Type", content_type);
            connection.setRequestProperty("Accept", "*/*");
        }

        // add Connection: keep-alive (does not work...)
        //connection.setRequestProperty("Connection", "keep-alive");

        // special gorp for HTTP DELETE
        if (verb != null && verb.equalsIgnoreCase("delete")) {
            connection.setRequestProperty("Access-Control-Allow-Methods", "OPTIONS, DELETE");
        }

        // specify domain if requested
        if (auth_domain != null && auth_domain.length() > 0) {
            connection.setRequestProperty("Domain", auth_domain);
        }

        // DEBUG dump the headers
        //if (doSSL) 
        //    this.errorLogger().info("HTTP: Headers: " + ((HttpsURLConnection)connection).getRequestProperties()); 
        //else
        //    this.errorLogger().info("HTTP: Headers: " + ((HttpURLConnection)connection).getRequestProperties()); 

        // specify data if requested - assumes it properly escaped if necessary
        if (doOutput && data != null && data.length() > 0) {
            try (OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream())) {
                out.write(data);
            }
        }

        // setup the output if requested
        if (doInput) {
            try {
                try (InputStream content = (InputStream) connection.getInputStream();
                        BufferedReader in = new BufferedReader(new InputStreamReader(content))) {
                    while ((line = in.readLine()) != null) {
                        result += line;
                    }
                }
            } catch (java.io.FileNotFoundException ex) {
                this.errorLogger().info("HTTP(" + verb + ") empty response (OK).");
                result = "";
            }
        } else {
            // no result expected
            result = "";
        }

        // save off the HTTP response code...
        if (doSSL)
            this.saveResponseCode(((HttpsURLConnection) connection).getResponseCode());
        else
            this.saveResponseCode(((HttpURLConnection) connection).getResponseCode());

        // DEBUG
        //if (doSSL)
        //    this.errorLogger().info("HTTP(" + verb +") URL: " + url_str + " Data: " + data + " Response code: " + ((HttpsURLConnection)connection).getResponseCode());
        //else
        //    this.errorLogger().info("HTTP(" + verb +") URL: " + url_str + " Data: " + data + " Response code: " + ((HttpURLConnection)connection).getResponseCode());
    } catch (IOException ex) {
        this.errorLogger().warning("Caught Exception in doHTTP(" + verb + "): " + ex.getMessage());
        result = null;
    }

    // return the result
    return result;
}

From source file:net.lightbody.bmp.proxy.jetty.http.handler.ProxyHandler.java

public void handle(String pathInContext, String pathParams, HttpRequest request, HttpResponse response)
        throws HttpException, IOException {
    URI uri = request.getURI();//from ww w.  j  av a2  s . c om

    // Is this a CONNECT request?
    if (HttpRequest.__CONNECT.equalsIgnoreCase(request.getMethod())) {
        response.setField(HttpFields.__Connection, "close"); // TODO Needed for IE????
        handleConnect(pathInContext, pathParams, request, response);
        return;
    }

    try {
        // Do we proxy this?
        URL url = isProxied(uri);
        if (url == null) {
            if (isForbidden(uri))
                sendForbid(request, response, uri);
            return;
        }

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

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

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

        // 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 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 (HttpFields.__ContentType.equals(hdr))
                hasContent = true;

            Enumeration vals = request.getFieldValues(hdr);
            while (vals.hasMoreElements()) {
                String val = (String) vals.nextElement();
                if (val != null) {
                    connection.addRequestProperty(hdr, val);
                    xForwardedFor |= HttpFields.__XForwardedFor.equalsIgnoreCase(hdr);
                }
            }
        }

        // 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 = HttpResponse.__500_Internal_Server_Error;
        if (http != null) {
            proxy_in = http.getErrorStream();

            code = http.getResponseCode();
            response.setStatus(code);
            response.setReason(http.getResponseMessage());
        }

        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)");

        // Handled
        request.setHandled(true);
        if (proxy_in != null)
            IO.copy(proxy_in, response.getOutputStream());

    } catch (Exception e) {
        log.warn(e.toString());
        LogSupport.ignore(log, e);
        if (!response.isCommitted())
            response.sendError(HttpResponse.__400_Bad_Request);
    }
}

From source file:it.infn.ct.wrf.Wrf.java

private void getRobotProxy(String eTokenServer, String eTokenServerPort, String proxyId, String VO, String FQAN,
        String proxyRenewal) {/*w ww  .ja  v  a  2  s .  co  m*/
    File proxyFile;
    Integer UID = getUID();

    proxyFile = new File("/tmp/x509up_u" + UID);

    String proxyContent = "";

    try {

        URL proxyURL = new URL("http://" + eTokenServer + ":" + eTokenServerPort + "/eTokenServer/eToken/"
                + proxyId + "?voms=" + VO + ":/" + VO + "&proxy-renewal=" + proxyRenewal
                + "&disable-voms-proxy=false&rfc-proxy=true&cn-label=Empty");

        URLConnection proxyConnection = proxyURL.openConnection();
        proxyConnection.setDoInput(true);

        InputStream proxyStream = proxyConnection.getInputStream();
        BufferedReader input = new BufferedReader(new InputStreamReader(proxyStream));

        String line = "";
        while ((line = input.readLine()) != null)
            proxyContent += line + "\n";

        FileUtils.writeStringToFile(proxyFile, proxyContent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.lockss.proxy.ProxyHandler.java

/** Proxy a connection using Java's native URLConection */
void doSun(String pathInContext, String pathParams, HttpRequest request, HttpResponse response)
        throws IOException {
    URI uri = request.getURI();//from w  w w . ja v a  2 s .  c o  m
    try {
        // Do we proxy this?
        URL url = isProxied(uri);
        if (url == null) {
            if (isForbidden(uri)) {
                sendForbid(request, response, uri);
                logAccess(request, "forbidden method: " + request.getMethod());
            }
            return;
        }

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

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

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

        // 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 hasContent = false;
        Enumeration en = request.getFieldNames();

        while (en.hasMoreElements()) {
            // XXX could be better than this!
            String hdr = (String) en.nextElement();

            if (_DontProxyHeaders.containsKey(hdr))
                continue;

            if (connectionHdr != null && connectionHdr.indexOf(hdr) >= 0)
                continue;

            if (HttpFields.__ContentType.equalsIgnoreCase(hdr))
                hasContent = true;

            Enumeration vals = request.getFieldValues(hdr);
            while (vals.hasMoreElements()) {
                String val = (String) vals.nextElement();
                if (val != null) {
                    connection.addRequestProperty(hdr, val);
                }
            }
        }

        // Proxy headers
        connection.addRequestProperty(HttpFields.__Via, makeVia(request));
        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(jlog, e);
        }

        InputStream proxy_in = null;

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

            code = http.getResponseCode();
            response.setStatus(code);
            response.setReason(http.getResponseMessage());
        }

        if (proxy_in == null) {
            try {
                proxy_in = connection.getInputStream();
            } catch (Exception e) {
                LogSupport.ignore(jlog, 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))
                response.addField(hdr, val);
            h++;
            hdr = connection.getHeaderFieldKey(h);
            val = connection.getHeaderField(h);
        }
        response.addField(HttpFields.__Via, makeVia(request));

        // Handled
        request.setHandled(true);
        if (proxy_in != null)
            IO.copy(proxy_in, response.getOutputStream());

    } catch (Exception e) {
        log.warning("doSun error", e);
        if (!response.isCommitted())
            response.sendError(HttpResponse.__400_Bad_Request, e.getMessage());
    }
}

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());//from ww w .  j  av a 2  s  .  co m
    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;
}