Example usage for java.net URLConnection setRequestProperty

List of usage examples for java.net URLConnection setRequestProperty

Introduction

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

Prototype

public void setRequestProperty(String key, String value) 

Source Link

Document

Sets the general request property.

Usage

From source file:org.wso2.es.integration.common.utils.AssetsRESTClient.java

/**
 * This methods make a call to ES-Publisher REST API and obtain a valid sessionID
 *
 * @return SessionId for the authenticated user
 *///  w  ww  .j  a  va2s. c o m
private String login() throws IOException {
    String sessionID = null;
    Reader input = null;
    BufferedWriter writer = null;
    String authenticationEndpoint = getBaseUrl() + PUBLISHER_APIS_AUTHENTICATE_ENDPOINT;
    //construct full authenticate endpoint
    try {
        //authenticate endpoint URL
        URL endpointUrl = new URL(authenticationEndpoint);
        URLConnection urlConn = endpointUrl.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        // Specify the content type.
        urlConn.setRequestProperty(CONTENT_TYPE_HEADER, CONTENT_TYPE);
        // Send POST output.
        writer = new BufferedWriter(new OutputStreamWriter(urlConn.getOutputStream()));
        String content = USERNAME + "=" + URLEncoder.encode(USERNAME_VAL, UTF_8) + "&" + PASSWORD + "="
                + URLEncoder.encode(PASSWORD_VAl, UTF_8);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Send Login Information : " + content);
        }
        writer.write(content);
        writer.flush();

        // Get response data.
        input = new InputStreamReader(urlConn.getInputStream());
        JsonElement elem = parser.parse(input);
        sessionID = elem.getAsJsonObject().getAsJsonObject(AUTH_RESPONSE_WRAP_KEY).get(SESSIONID).toString();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Received SessionID : " + sessionID);
        }

    } catch (MalformedURLException e) {
        LOG.error(getLoginErrorMassage(authenticationEndpoint), e);
        throw e;
    } catch (IOException e) {
        LOG.error(getLoginErrorMassage(authenticationEndpoint), e);
        throw e;
    } finally {
        if (input != null) {
            try {
                input.close();// will close the URL connection as well
            } catch (IOException e) {
                LOG.error("Failed to close input stream ", e);
            }
        }
        if (writer != null) {
            try {
                writer.close();// will close the URL connection as well
            } catch (IOException e) {
                LOG.error("Failed to close output stream ", e);
            }
        }
    }
    return sessionID;
}

From source file:com.adobe.phonegap.contentsync.Sync.java

private static void addHeadersToRequest(URLConnection connection, JSONObject headers) {
    try {/*from w  w w . j ava  2s  .  c  o m*/
        for (Iterator<?> iter = headers.keys(); iter.hasNext();) {
            String headerKey = iter.next().toString();
            JSONArray headerValues = headers.optJSONArray(headerKey);
            if (headerValues == null) {
                headerValues = new JSONArray();
                headerValues.put(headers.getString(headerKey));
            }
            connection.setRequestProperty(headerKey, headerValues.getString(0));
            for (int i = 1; i < headerValues.length(); ++i) {
                connection.addRequestProperty(headerKey, headerValues.getString(i));
            }
        }
    } catch (JSONException e1) {
        // No headers to be manipulated!
    }
}

From source file:org.jab.docsearch.utils.NetUtils.java

/**
 * Downloads URL to file/*w w  w  .j  av a  2 s  .  c o m*/
 *
 * Content type, content length, last modified and md5 will be set in SpiderUrl
 *
 * @param spiderUrl  URL to download
 * @param file       URL content downloads to this file
 * @return           true if URL successfully downloads to a file
 */
public boolean downloadURLToFile(final SpiderUrl spiderUrl, final String file) {

    boolean downloadOk = false;
    BufferedInputStream inputStream = null;
    BufferedOutputStream outputStream = null;
    try {
        // if the file downloads - save it and return true
        URL url = new URL(spiderUrl.getUrl());
        URLConnection conn = url.openConnection();

        // set connection parameter
        conn.setDoInput(true);
        conn.setDoOutput(false);
        conn.setUseCaches(false);
        conn.setRequestProperty("User-Agent", USER_AGENT);

        // connect
        conn.connect();

        // content type
        spiderUrl.setContentType(getContentType(conn));

        // open streams
        inputStream = new BufferedInputStream(conn.getInputStream());
        outputStream = new BufferedOutputStream(new FileOutputStream(file));

        // copy data from URL to file
        long size = 0;
        int readed = 0;
        while ((readed = inputStream.read()) != -1) {
            size++;
            outputStream.write(readed);
        }

        // set values
        spiderUrl.setContentType(getContentType(conn));
        spiderUrl.setSize(size);

        downloadOk = true;
    } catch (IOException ioe) {
        logger.fatal("downloadURLToFile() failed for URL='" + spiderUrl.getUrl() + "'", ioe);
        downloadOk = false;
    } finally {
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(inputStream);
    }

    // set values
    if (downloadOk) {
        spiderUrl.setMd5(FileUtils.getMD5Sum(file));
    }

    return downloadOk;
}

From source file:nl.welteninstituut.tel.la.importers.fitbit.FitbitTask.java

public String postUrl(String url, String data, String authorization) {
    StringBuilder result = new StringBuilder();

    try {//from  ww w. ja v a2 s .c  o m
        URLConnection conn = new URL(url).openConnection();
        // conn.setConnectTimeout(30);
        conn.setDoOutput(true);

        if (authorization != null)
            conn.setRequestProperty("Authorization",
                    "Basic " + new String(new Base64().encode(authorization.getBytes())));
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        wr.close();
        rd.close();

    } catch (Exception e) {
    }

    return result.toString();
}

From source file:org.chililog.server.workbench.WorkbenchServiceTest.java

/**
 * Check if our expected file types are compressed
 * //from w w w.ja  va  2  s.  c  o m
 * @throws IOException
 * @throws ParseException
 * @throws DecoderException
 */
@Test()
public void testStaticFileCompression() throws IOException, ParseException, DecoderException {
    String[] fileExtensions = new String[] { ".html", ".js", ".css", ".json", ".txt", ".xml",
            ".nocompression" };

    // Get 10K string
    String TEXT = new RandomString(1024 * 10).nextString();
    byte[] TEXT_ARRAY = TEXT.getBytes("UTF-8");

    for (String fileExtension : fileExtensions) {
        String fileName = UUID.randomUUID().toString() + fileExtension;
        File file = new File(_workbenchStaticFilesDirectory, fileName);

        FileOutputStream fos = new FileOutputStream(file);
        OutputStreamWriter out = new OutputStreamWriter(fos, "UTF-8");
        out.write(TEXT);
        out.close();

        // Refresh
        file = new File(file.getPath());

        // Create a URL for the desired page
        URL url = new URL("http://localhost:8989/static/testdata/" + fileName + "?testquerystring=abc");
        URLConnection conn = url.openConnection();
        conn.setRequestProperty("Accept-Encoding", "gzip,deflate");

        // Read all the compressed data
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        InputStream is = conn.getInputStream();
        int b;
        while ((b = is.read()) != -1) {
            os.write(b);
        }

        // Get headers
        String responseCode = "";
        HashMap<String, String> headers = new HashMap<String, String>();
        for (int i = 0;; i++) {
            String name = conn.getHeaderFieldKey(i);
            String value = conn.getHeaderField(i);
            if (name == null && value == null) {
                break;
            }
            if (name == null) {
                responseCode = value;
                _logger.debug("*** Intial Call, Response code: %s", value);
            } else {
                headers.put(name, value);
                _logger.debug("%s = %s", name, value);
            }
        }

        // Should get back a 200
        assertEquals("HTTP/1.1 200 OK", responseCode);
        assertTrue(!StringUtils.isBlank(headers.get("Date")));

        if (fileExtension != ".nocompression") {
            // Uncompress and check it out
            assertEquals("gzip", headers.get("Content-Encoding"));
            byte[] uncompressedContent = uncompress(os.toByteArray());
            for (int j = 0; j < TEXT_ARRAY.length; j++) {
                assertEquals(TEXT_ARRAY[j], uncompressedContent[j]);
            }
        }

        // Clean up
        file.delete();
    }

    return;
}

From source file:com.wavemaker.runtime.ws.SyndFeedService.java

/**
 * Reads from the InputStream of the specified URL and builds the feed object from the returned XML.
 * /*from ww w  .  j  a va 2 s  .  co  m*/
 * @param feedURL The URL to read feed from.
 * @param httpBasicAuthUsername The username for HTTP Basic Authentication.
 * @param httpBasicAuthPassword The password for HTTP Basic Authentication.
 * @param connectionTimeout HTTP connection timeout.
 * @return A feed object.
 */
@ExposeToClient
public Feed getFeedWithHttpConfig(String feedURL, String httpBasicAuthUsername, String httpBasicAuthPassword,
        int connectionTimeout) {
    URL url = null;
    try {
        url = new URL(feedURL);
    } catch (MalformedURLException e) {
        throw new WebServiceInvocationException(e);
    }

    SyndFeedInput input = new SyndFeedInput();
    try {
        URLConnection urlConn = url.openConnection();
        if (urlConn instanceof HttpURLConnection) {
            urlConn.setAllowUserInteraction(false);
            urlConn.setDoInput(true);
            urlConn.setDoOutput(false);
            ((HttpURLConnection) urlConn).setInstanceFollowRedirects(true);
            urlConn.setUseCaches(false);
            urlConn.setRequestProperty(USER_AGENT_KEY, USER_AGENT_VALUE);

            urlConn.setConnectTimeout(connectionTimeout);

            if (httpBasicAuthUsername != null && httpBasicAuthUsername.length() > 0) {
                String auth = httpBasicAuthPassword == null ? httpBasicAuthUsername
                        : httpBasicAuthUsername + ":" + httpBasicAuthPassword;
                urlConn.setRequestProperty(BASIC_AUTH_KEY,
                        BASIC_AUTH_VALUE_PREFIX + Base64.encodeBase64URLSafeString(auth.getBytes()));
            }
        }
        SyndFeed feed = input.build(new XmlReader(urlConn));
        return FeedBuilder.getFeed(feed);
    } catch (IllegalArgumentException e) {
        throw new WebServiceInvocationException(e);
    } catch (FeedException e) {
        throw new WebServiceInvocationException(e);
    } catch (IOException e) {
        throw new WebServiceInvocationException(e);
    }
}

From source file:com.apache.ivy.BasicURLHandler.java

public InputStream openStream(URL url) throws IOException {
    // Install the IvyAuthenticator
    if ("http".equals(url.getProtocol()) || "https".equals(url.getProtocol())) {
        IvyAuthenticator.install();//from   ww w  . j a  v  a2s  .  c o m
    }

    URLConnection conn = null;
    try {
        url = normalizeToURL(url);
        conn = url.openConnection();
        conn.setRequestProperty("User-Agent", "Apache Ivy/1.0");// + Ivy.getIvyVersion());
        conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
        if (conn instanceof HttpURLConnection) {
            HttpURLConnection httpCon = (HttpURLConnection) conn;
            if (!checkStatusCode(url, httpCon)) {
                throw new IOException("The HTTP response code for " + url + " did not indicate a success."
                        + " See log for more detail.");
            }
        }
        InputStream inStream = getDecodingInputStream(conn.getContentEncoding(), conn.getInputStream());
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        byte[] buffer = new byte[BUFFER_SIZE];
        int len;
        while ((len = inStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, len);
        }
        return new ByteArrayInputStream(outStream.toByteArray());
    } finally {
        disconnect(conn);
    }
}

From source file:org.phaidra.apihooks.APIRESTHooksImpl.java

/**
 * Runs the hook if enabled in fedora.fcfg.
 *
 * @param method The name of the method that calls the hook
 * @param pid The PID that is being accessed
 * @param params Method parameters, depend on the method called
 * @return String Hook verdict. Begins with "OK" if it's ok to proceed.
 * @throws APIHooksException If the remote call went wrong
 *///w  w w.  j  ava2  s  .c o m
public String runHook(String method, DOWriter w, Context context, String pid, Object[] params)
        throws APIHooksException {
    String rval = null;

    // Only do this if the method is enabled in fedora.fcfg
    if (getParameter(method) == null) {
        log.debug("runHook: method |" + method + "| not configured, not calling webservice");
        return "OK";
    }

    Iterator i = context.subjectAttributes();
    String attrs = "";
    while (i.hasNext()) {
        String name = "";
        try {
            name = (String) i.next();
            String[] value = context.getSubjectValues(name);
            for (int j = 0; j < value.length; j++) {
                attrs += "&attr=" + URLEncoder.encode(name + "=" + value[j], "UTF-8");
                log.debug("runHook: will send |" + name + "=" + value[j] + "| as subject attribute");
            }
        } catch (NullPointerException ex) {
            log.debug(
                    "runHook: caught NullPointerException while trying to retrieve subject attribute " + name);
        } catch (UnsupportedEncodingException ex) {
            log.debug("runHook: caught UnsupportedEncodingException while trying to encode subject attribute "
                    + name);
        }
    }
    String paramstr = "";
    try {
        for (int j = 0; j < params.length; j++) {
            paramstr += "&param" + Integer.toString(j) + "=";
            if (params[j] != null) {
                String p = params[j].toString();
                paramstr += URLEncoder.encode(p, "UTF-8");
            }
        }
    } catch (UnsupportedEncodingException ex) {
        log.debug("runHook: caught UnsupportedEncodingException while trying to encode a parameter");
    }

    String loginId = context.getSubjectValue(Constants.SUBJECT.LOGIN_ID.uri);

    log.debug("runHook: called for method=|" + method + "|, pid=|" + pid + "|");
    try {
        // TODO: timeout? retries?
        URL url;
        URLConnection urlConn;
        DataOutputStream printout;
        BufferedReader input;

        url = new URL(getParameter("restmethod"));
        urlConn = url.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        printout = new DataOutputStream(urlConn.getOutputStream());
        String content = "method=" + URLEncoder.encode(method, "UTF-8") + "&username="
                + URLEncoder.encode(loginId, "UTF-8") + "&pid=" + URLEncoder.encode(pid, "UTF-8") + paramstr
                + attrs;
        printout.writeBytes(content);
        printout.flush();
        printout.close();

        // Get response data.
        input = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), "UTF-8"));
        String str;
        rval = "";
        while (null != ((str = input.readLine()))) {
            rval += str + "\n";
        }
        input.close();

        String ct = urlConn.getContentType();
        if (ct.startsWith("text/xml")) {
            log.debug("runHook: successful REST invocation for method |" + method + "|, returning: " + rval);
        } else if (ct.startsWith("text/plain")) {
            log.debug("runHook: successful REST invocation for method |" + method
                    + "|, but hook returned an error: " + rval);
            throw new Exception(rval);
        } else {
            throw new Exception("Invalid content type " + ct);
        }
    } catch (Exception ex) {
        log.error("runHook: error calling REST hook: " + ex.toString());
        throw new APIHooksException("Error calling REST hook: " + ex.toString(), ex);
    }

    return processResults(rval, w, context);
}

From source file:net.solarnetwork.node.price.delimited.DelimitedPriceDatumDataSource.java

private String readDataRow(URL theUrl) {
    BufferedReader resp = null;// ww  w  .  j  a  v a2 s .com
    if (log.isDebugEnabled()) {
        log.debug("Requesting price data from [" + theUrl + ']');
    }
    try {
        URLConnection conn = theUrl.openConnection();
        conn.setConnectTimeout(this.connectionTimeout);
        conn.setReadTimeout(this.connectionTimeout);
        conn.setRequestProperty("Accept", "text/*");

        resp = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        String str;
        int skipCount = this.skipLines;
        while ((str = resp.readLine()) != null) {
            if (skipCount > 0) {
                skipCount--;
                continue;
            }
            break;
        }
        if (log.isTraceEnabled()) {
            log.trace("Found price data: " + str);
        }
        return str;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (resp != null) {
            try {
                resp.close();
            } catch (IOException e) {
                // ignore this
                log.debug("Exception closing URL stream", e);
            }
        }
    }
}

From source file:com.apache.ivy.BasicURLHandler.java

public URLInfo getURLInfo(URL url, int timeout) {
    // Install the IvyAuthenticator
    if ("http".equals(url.getProtocol()) || "https".equals(url.getProtocol())) {
        IvyAuthenticator.install();/*from w ww. ja v a  2 s.  c o m*/
    }

    URLConnection con = null;
    try {
        url = normalizeToURL(url);
        con = url.openConnection();
        con.setRequestProperty("User-Agent", "Apache Ivy/1.0");//+ Ivy.getIvyVersion());
        if (con instanceof HttpURLConnection) {
            HttpURLConnection httpCon = (HttpURLConnection) con;
            if (getRequestMethod() == URLHandler.REQUEST_METHOD_HEAD) {
                httpCon.setRequestMethod("HEAD");
            }
            if (checkStatusCode(url, httpCon)) {
                String bodyCharset = getCharSetFromContentType(con.getContentType());
                return new URLInfo(true, httpCon.getContentLength(), con.getLastModified(), bodyCharset);
            }
        } else {
            int contentLength = con.getContentLength();
            if (contentLength <= 0) {
                return UNAVAILABLE;
            } else {
                // TODO: not HTTP... maybe we *don't* want to default to ISO-8559-1 here?
                String bodyCharset = getCharSetFromContentType(con.getContentType());
                return new URLInfo(true, contentLength, con.getLastModified(), bodyCharset);
            }
        }
    } catch (UnknownHostException e) {
        // Message.warn("Host " + e.getMessage() + " not found. url=" + url);
        // Message.info("You probably access the destination server through "
        //    + "a proxy server that is not well configured.");
    } catch (IOException e) {
        //Message.error("Server access error at url " + url, e);
    } finally {
        disconnect(con);
    }
    return UNAVAILABLE;
}