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:captureplugin.drivers.defaultdriver.CaptureExecute.java

/**
 * Executes the URL//from  www . java2 s.c  om
 *
 * @param params
 *          Params for the URL
 * @return Result-Page
 * @throws Exception
 *           Problems while loading the URL
 */
private String executeUrl(String params) throws Exception {
    final StringBuilder result = new StringBuilder();

    URL url = new URL(mData.getWebUrl() + '?' + params);

    URLConnection uc = url.openConnection();

    String userpassword = mData.getUsername() + ':' + mData.getPassword();
    String encoded = new String(Base64.encodeBase64(userpassword.getBytes()));

    uc.setRequestProperty("Authorization", "Basic " + encoded);

    if (uc instanceof HttpURLConnection) {
        if (((HttpURLConnection) uc).getResponseCode() != HttpURLConnection.HTTP_OK) {
            InputStream content = ((HttpURLConnection) uc).getErrorStream();
            StreamUtilities.inputStream(content, new InputStreamProcessor() {

                @Override
                public void process(InputStream input) throws IOException {
                    BufferedReader in = new BufferedReader(new InputStreamReader(input));
                    String line;
                    while ((line = in.readLine()) != null) {
                        result.append(line);
                    }
                }
            });
            mError = true;
            mExitValue = 1;
            return result.toString();
        }
    }

    StreamUtilities.inputStream(uc, new InputStreamProcessor() {

        @Override
        public void process(InputStream input) throws IOException {
            BufferedReader in = new BufferedReader(new InputStreamReader(input));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        }
    });

    mError = false;
    mExitValue = 0;
    return result.toString();
}

From source file:io.github.bonigarcia.wdm.BrowserManager.java

protected InputStream openGitHubConnection(URL driverUrl) throws IOException {
    Proxy proxy = createProxy();//from   w w  w . j  ava 2  s. c om
    URLConnection conn = proxy != null ? driverUrl.openConnection(proxy) : driverUrl.openConnection();
    conn.setRequestProperty("User-Agent", "Mozilla/5.0");
    conn.addRequestProperty("Connection", "keep-alive");

    String gitHubTokenName = WdmConfig.getString("wdm.gitHubTokenName");
    String gitHubTokenSecret = WdmConfig.getString("wdm.gitHubTokenSecret");
    if (!isNullOrEmpty(gitHubTokenName) && !isNullOrEmpty(gitHubTokenSecret)) {
        String userpass = gitHubTokenName + ":" + gitHubTokenSecret;
        String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
        conn.setRequestProperty("Authorization", basicAuth);
    }
    conn.connect();

    return conn.getInputStream();
}

From source file:org.xwiki.contrib.oidc.auth.internal.OIDCUserManager.java

private Principal updateUser(IDTokenClaimsSet idToken, UserInfo userInfo)
        throws XWikiException, QueryException {
    XWikiDocument userDocument = this.store.searchDocument(idToken.getIssuer().getValue(),
            userInfo.getSubject().toString());

    XWikiDocument modifiableDocument;//from w  w w  .  j  ava  2  s  .  c om
    boolean newUser;
    if (userDocument == null) {
        userDocument = getNewUserDocument(idToken, userInfo);

        newUser = true;
        modifiableDocument = userDocument;
    } else {
        // Don't change the document author to not change document execution right

        newUser = false;
        modifiableDocument = userDocument.clone();
    }

    XWikiContext xcontext = this.xcontextProvider.get();

    // Set user fields
    BaseObject userObject = modifiableDocument
            .getXObject(xcontext.getWiki().getUserClass(xcontext).getDocumentReference(), true, xcontext);

    // Address
    Address address = userInfo.getAddress();
    if (address != null) {
        userObject.set("address", address.getFormatted(), xcontext);
    }

    // Email
    if (userInfo.getEmail() != null) {
        userObject.set("email", userInfo.getEmail().toUnicodeString(), xcontext);
    }

    // Last name
    if (userInfo.getFamilyName() != null) {
        userObject.set("last_name", userInfo.getFamilyName(), xcontext);
    }

    // First name
    if (userInfo.getGivenName() != null) {
        userObject.set("first_name", userInfo.getGivenName(), xcontext);
    }

    // Phone
    if (userInfo.getPhoneNumber() != null) {
        userObject.set("phone", userInfo.getPhoneNumber(), xcontext);
    }

    // Default locale
    if (userInfo.getLocale() != null) {
        userObject.set("default_language", Locale.forLanguageTag(userInfo.getLocale()).toString(), xcontext);
    }

    // Time Zone
    if (userInfo.getZoneinfo() != null) {
        userObject.set("timezone", userInfo.getZoneinfo(), xcontext);
    }

    // Website
    if (userInfo.getWebsite() != null) {
        userObject.set("blog", userInfo.getWebsite().toString(), xcontext);
    }

    // Avatar
    if (userInfo.getPicture() != null) {
        try {
            String filename = FilenameUtils.getName(userInfo.getPicture().toString());
            URLConnection connection = userInfo.getPicture().toURL().openConnection();
            connection.setRequestProperty("User-Agent", this.getClass().getPackage().getImplementationTitle()
                    + '/' + this.getClass().getPackage().getImplementationVersion());
            try (InputStream content = connection.getInputStream()) {
                modifiableDocument.addAttachment(filename, content, xcontext);
            }
            userObject.set("avatar", filename, xcontext);
        } catch (IOException e) {
            this.logger.warn("Failed to get user avatar from URL [{}]: {}", userInfo.getPicture(),
                    ExceptionUtils.getRootCauseMessage(e));
        }
    }

    // XWiki claims
    updateXWikiClaims(modifiableDocument, userObject.getXClass(xcontext), userObject, userInfo, xcontext);

    // Set OIDC fields
    this.store.updateOIDCUser(modifiableDocument, idToken.getIssuer().getValue(),
            userInfo.getSubject().getValue());

    // Prevent data to send with the event
    OIDCUserEventData eventData = new OIDCUserEventData(new NimbusOIDCIdToken(idToken),
            new NimbusOIDCUserInfo(userInfo));

    // Notify
    this.observation.notify(new OIDCUserUpdating(modifiableDocument.getDocumentReference()), modifiableDocument,
            eventData);

    // Apply the modifications
    if (newUser || userDocument.apply(modifiableDocument)) {
        String comment;
        if (newUser) {
            comment = "Create user from OpenID Connect";
        } else {
            comment = "Update user from OpenID Connect";
        }

        xcontext.getWiki().saveDocument(userDocument, comment, xcontext);

        // Now let's add new the user to XWiki.XWikiAllGroup
        if (newUser) {
            xcontext.getWiki().setUserDefaultGroup(userDocument.getFullName(), xcontext);
        }

        // Notify
        this.observation.notify(new OIDCUserUpdated(userDocument.getDocumentReference()), userDocument,
                eventData);
    }

    return new SimplePrincipal(userDocument.getPrefixedFullName());
}

From source file:org.infoglue.common.util.RemoteCacheUpdater.java

/**
 * This method post information to an URL and returns a string.It throws
 * an exception if anything goes wrong./*from  w w w.j  a v  a 2s .c o  m*/
 * (Works like most 'doPost' methods)
 * 
 * @param urlAddress The address of the URL you would like to post to.
 * @param inHash The parameters you would like to post to the URL.
 * @return The result of the postToUrl method as a string.
 * @exception java.lang.Exception
 */

private String postToUrl(String urlAddress, Hashtable inHash) throws Exception {
    URL url = new URL(urlAddress);
    URLConnection urlConn = url.openConnection();
    urlConn.setConnectTimeout(3000);
    urlConn.setReadTimeout(3000);
    urlConn.setAllowUserInteraction(false);
    urlConn.setDoOutput(true);
    urlConn.setDoInput(true);
    urlConn.setUseCaches(false);
    urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    PrintWriter printout = new PrintWriter(urlConn.getOutputStream(), true);
    String argString = "";
    if (inHash != null) {
        argString = toEncodedString(inHash);
    }
    printout.print(argString);
    printout.flush();
    printout.close();
    InputStream inStream = null;
    inStream = urlConn.getInputStream();
    InputStreamReader inStreamReader = new InputStreamReader(inStream);
    BufferedReader buffer = new BufferedReader(inStreamReader);
    StringBuffer strbuf = new StringBuffer();
    String line;
    while ((line = buffer.readLine()) != null) {
        strbuf.append(line);
    }
    String readData = strbuf.toString();
    buffer.close();
    return readData;
}

From source file:org.apache.hadoop.gateway.hdfs.web.KnoxUrlConnectionFactory.java

private void configureConnectionAuthentication(URLConnection connection) throws IOException {
    String password = config.get("knox.webhdfs.password");
    if (password != null) {
        String username = UserGroupInformation.getCurrentUser().getUserName();
        if (password.equals("")) {
            password = new String(promptForPassword("Password for " + username + ": "));
        }//ww w  .  j ava 2 s .co m
        String credentials = username + ":" + password;
        String encodedCredentials = Base64.encodeBase64String(credentials.getBytes()).trim();
        connection.setRequestProperty("Authorization", "Basic " + encodedCredentials);
    }
}

From source file:org.transitime.avl.PollUrlAvlModule.java

/**
 * Actually reads data from feed and processes it by opening up a URL
 * specified by getUrl() and then reading the contents. Calls the abstract
 * method processData() to actually process the input stream.
 * <p>/*from w ww.  j av a  2 s .c om*/
 * This method needs to be overwritten if not real data from a URL
 * 
 * @throws Exception
 *             Throws a generic exception since the processing is done in
 *             the abstract method processData() and it could throw any type
 *             of exception since we don't really know how the AVL feed will
 *             be processed.
 */
protected void getAndProcessData() throws Exception {
    // For logging
    IntervalTimer timer = new IntervalTimer();

    // Get from the AVL feed subclass the URL to use for this feed
    String fullUrl = getUrl();

    // Log what is happening
    logger.info("Getting data from feed using url=" + fullUrl);

    // Create the connection
    URL url = new URL(fullUrl);
    URLConnection con = url.openConnection();

    // Set the timeout so don't wait forever
    int timeoutMsec = AvlConfig.getAvlFeedTimeoutInMSecs();
    con.setConnectTimeout(timeoutMsec);
    con.setReadTimeout(timeoutMsec);

    // Request compressed data to reduce bandwidth used
    if (useCompression)
        con.setRequestProperty("Accept-Encoding", "gzip,deflate");

    // If authentication being used then set user and password
    if (authenticationUser.getValue() != null && authenticationPassword.getValue() != null) {
        String authString = authenticationUser.getValue() + ":" + authenticationPassword.getValue();
        byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
        String authStringEnc = new String(authEncBytes);
        con.setRequestProperty("Authorization", "Basic " + authStringEnc);
    }

    // Set any additional AVL feed specific request headers
    setRequestHeaders(con);

    // Create appropriate input stream depending on whether content is 
    // compressed or not
    InputStream in = con.getInputStream();
    if ("gzip".equals(con.getContentEncoding())) {
        in = new GZIPInputStream(in);
        logger.debug("Returned data is compressed");
    } else {
        logger.debug("Returned data is NOT compressed");
    }

    // For debugging
    logger.debug("Time to access inputstream {} msec", timer.elapsedMsec());

    // Call the abstract method to actually process the data
    timer.resetTimer();
    Collection<AvlReport> avlReportsReadIn = processData(in);
    in.close();
    logger.debug("Time to parse document {} msec", timer.elapsedMsec());

    // Process all the reports read in
    if (shouldProcessAvl.getValue())
        processAvlReports(avlReportsReadIn);
}

From source file:com.bah.applefox.main.plugins.webcrawler.utilities.WebPageCrawl.java

/**
 * Constructor for the WebPageParser object. Runs the page crawl when it is
 * invoked.//from   ww  w  .  j a  v a 2s. c o  m
 * 
 * @param parentURL
 *            - the URL of the page to be parsed
 * @param UserAgent
 *            - the UserAgent of the server request property
 * @throws PageCrawlException
 *             If an error occurs crawling the page.
 */
public WebPageCrawl(String parentURL, String UserAgent, Set<String> authorityLimits) throws PageCrawlException {
    // get the url, check if we can crawl this page.
    URL url;
    try {
        url = new URL(parentURL);
    } catch (MalformedURLException e) {
        throw new PageCrawlException(e);
    }
    robotsTXT = RobotsTXT.get(url, UserAgent);
    this.UserAgent = UserAgent;
    try {
        this.parentURL = url.toURI();
    } catch (URISyntaxException e) {
        throw new PageCrawlException(e);
    }

    if (robotsTXT.allowed(parentURL)) {
        URLConnection con;
        try {
            con = url.openConnection();
        } catch (MalformedURLException e) {
            throw new PageCrawlException(e);
        } catch (IOException e) {
            throw new PageCrawlException(e);
        }
        con.setRequestProperty("User-Agent", UserAgent);
        InputStream stream = null;
        try {
            try {
                stream = con.getInputStream();
            } catch (IOException e) {
                throw new PageCrawlException(e);
            }
            Parser parser = new AutoDetectParser();
            ParagraphContentHandler paragraphGetter = new ParagraphContentHandler();
            BodyContentHandler bodyHandler = new BodyContentHandler(paragraphGetter);
            // link handlers also do image tags
            LinkContentHandler linkHandler = new LinkContentHandler();
            ContentHandler overallHandler = new TeeContentHandler(bodyHandler, linkHandler);
            Metadata metadata = new Metadata();
            ParseContext context = new ParseContext();
            try {
                parser.parse(stream, overallHandler, metadata, context);
            } catch (IOException e) {
                throw new PageCrawlException(e);
            } catch (SAXException e) {
                throw new PageCrawlException(e);
            } catch (TikaException e) {
                throw new PageCrawlException(e);
            }
            // WE FINALLY GET THE DATA!
            String docTitle = metadata.get("title");
            this.title = docTitle != null ? docTitle : "";
            pageContents = paragraphGetter.getParagraphs();
            List<String> images = new ArrayList<String>();
            List<String> links = new ArrayList<String>();
            for (Link link : linkHandler.getLinks()) {
                URI linkURL = this.parentURL.resolve(link.getUri());
                if (authorityLimits.size() > 0 && !authorityLimits.contains(linkURL.getAuthority())) {
                    continue;
                }
                String protocol = linkURL.getScheme();
                if (!protocol.equals("http") && !protocol.equals("https"))
                    continue;
                if (link.isImage()) {
                    images.add(linkURL.toString());
                }
                if (link.isAnchor()) {
                    links.add(linkURL.toString());
                }
            }
            allImages = Collections.unmodifiableList(images);
            allLinks = Collections.unmodifiableList(links);
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    throw new PageCrawlException(e);
                }
            }
        }
    } else {
        title = "";
        pageContents = Collections.emptyList();
        allImages = Collections.emptyList();
        allLinks = Collections.emptyList();
    }

}

From source file:XMLParser.java

public void StartTools(int type) {
    try {//from  w w  w .j  a va2 s .  c o  m
        if (type == 0) {
            try {
                URL url = new URL("http://api.micetigri.fr/maps/search/p" + BootP + "?page=" + BootPg
                        + "&sort=id&direction=asc");
                URLConnection connection = url.openConnection();
                connection.setRequestProperty("User-Agent",
                        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");
                try (BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(connection.getInputStream()))) {
                    while ((line = bufferedReader.readLine()) != null) {
                        if (line.contains("query: ")) {
                            Object data[] = line.split("\"");
                            Codes = (String) data[1];
                        }
                    }
                    if (!Codes.equals("")) {
                        try {
                            if (!BootCodes.equals("")) {
                                BootCodes += ";" + Codes;
                            } else {
                                BootCodes += Codes;
                            }
                            this.jTextPane1.setText(BootCodes);
                            System.out.println(new StringBuilder("[List]: ").append(Codes).toString());
                            if (!this.jCheckBox3.isSelected()) {
                                BootPg++;
                                new java.util.Timer().schedule(new java.util.TimerTask() {
                                    @Override
                                    public void run() {
                                        StartTools(0);
                                    }
                                }, BootDelay);
                            } else {
                                this.StartTools(2);
                            }
                            BootPgCount++;
                        } catch (Exception error) {
                            System.out.println(new StringBuilder("[Codes]: ").append(error).toString());
                        }
                    } else {
                        System.out.println(new StringBuilder("Total (PG): ").append(BootPgCount).toString());
                        this.StartTools(2);
                    }
                }
            } catch (Exception error) {
                System.out.println(new StringBuilder("[0][Exception]: ").append(error).toString());
            }
        } else if (type == 2) {
            try {
                String[] MapCodes = BootCodes.split(";");
                String MapCode = "";
                try {
                    MapCode = MapCodes[BootID];
                } catch (Exception error) {
                    MapCode = "";
                }
                if (StringUtils.isNumeric(MapCode)) {
                    URL url = new URL("http://api.micetigri.fr/maps/xmlnew/" + MapCode);
                    URLConnection connection = url.openConnection();
                    connection.setRequestProperty("User-Agent",
                            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");
                    try (BufferedReader bufferedReader = new BufferedReader(
                            new InputStreamReader(connection.getInputStream()))) {
                        while ((line = bufferedReader.readLine()) != null) {
                            if (line.contains("CODE")) {
                                Object data[] = line.split("\"");
                                Code = Integer.valueOf((String) data[1]);
                            } else if (line.contains("CREATOR")) {
                                Object data[] = line.split("\"");
                                Creator = (String) data[1];
                            } else if (line.contains("XML")) {
                                Object data[] = line.split("\"");
                                xml = decrypt((String) data[1], CRYPT_KEY);
                            } else if (line.contains("PERM")) {
                                Object data[] = line.split("\"");
                                Perm = Integer.valueOf((String) data[1]);
                            }
                        }
                        if (!xml.equals("") && !Creator.equals("")) {
                            BootInfos += (new StringBuilder("Code: '").append(Code).append("'\nCreator: '")
                                    .append(Creator).append("'\nPerma: '").append(Perm).append("'\nXML: '")
                                    .append(xml).append("'\n\n\n\n").toString());
                            this.jTextPane1.setText(BootInfos);
                            BootID++;
                            new java.util.Timer().schedule(new java.util.TimerTask() {
                                @Override
                                public void run() {
                                    StartTools(2);
                                }
                            }, BootDelay);
                        }
                    }
                } else {
                    this.jButton1.setText("START");
                    this.jButton1.setEnabled(true);
                    this.jComboBox1.setEnabled(true);
                    this.jCheckBox1.setEnabled(true);
                    this.jCheckBox2.setEnabled(true);
                    this.jLabel1.setEnabled(true);
                    this.jLabel2.setEnabled(true);
                    this.jCheckBox3.setEnabled(true);
                    if (this.jCheckBox3.isSelected()) {
                        this.jSpinner1.setEnabled(true);
                    }
                    System.out.println(new StringBuilder("Total de mapas: ").append(BootID).toString());
                }
            } catch (Exception error) {
                System.out.println(new StringBuilder("[1][Exception]: ").append(error).toString());
            }
        } else if (type == 1) {
            try {
                URL url = new URL("http://api.micetigri.fr/maps/xmlnew/" + iCode);
                URLConnection connection = url.openConnection();
                connection.setRequestProperty("User-Agent",
                        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");
                try (BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(connection.getInputStream()))) {
                    while ((line = bufferedReader.readLine()) != null) {
                        if (line.contains("CODE")) {
                            Object data[] = line.split("\"");
                            Code = Integer.valueOf((String) data[1]);
                        } else if (line.contains("CREATOR")) {
                            Object data[] = line.split("\"");
                            Creator = (String) data[1];
                        } else if (line.contains("XML")) {
                            Object data[] = line.split("\"");
                            xml = decrypt((String) data[1], CRYPT_KEY);
                        } else if (line.contains("PERM")) {
                            Object data[] = line.split("\"");
                            Perm = Integer.valueOf((String) data[1]);
                        }
                    }
                    if (!xml.equals("") && !Creator.equals("")) {
                        this.jTextPane1.setText(new StringBuilder("Code: '").append(Code)
                                .append("'\nCreator: '").append(Creator).append("'\nPerma: '").append(Perm)
                                .append("'\nXML: '").append(xml).append("'\n").toString());
                    } else {
                        this.jTextPane1.setText("xml not found !");
                    }
                }
                this.jButton1.setEnabled(true);
                this.jLabel3.setEnabled(true);
                this.jTextField1.setEnabled(true);
                this.jCheckBox1.setEnabled(true);
                this.jCheckBox2.setEnabled(false);
                this.jButton1.setText("START");

            } catch (Exception error) {
                System.out.println(new StringBuilder("[1][Exception]: ").append(error).toString());
            }
        }
    } catch (Exception error) {
        System.out.println(new StringBuilder("[ALL][IOException]: ").append(error).toString());
    }
}

From source file:org.gephi.streaming.impl.StreamingControllerImpl.java

private void sendEvent(final StreamingEndpoint endpoint, GraphEvent event) {
    logger.log(Level.FINE, "Sending event {0}", event.toString());
    try {/*from www.j a  v a  2s.c o  m*/
        URL url = new URL(endpoint.getUrl(), endpoint.getUrl().getFile() + "?operation=updateGraph&format="
                + endpoint.getStreamType().getType());

        URLConnection connection = url.openConnection();

        // Workaround for Bug https://issues.apache.org/jira/browse/CODEC-89
        String base64Encoded = Base64
                .encodeBase64String((endpoint.getUser() + ":" + endpoint.getPassword()).getBytes());
        base64Encoded = base64Encoded.replaceAll("\r\n?", "");

        connection.setRequestProperty("Authorization", "Basic " + base64Encoded);

        connection.setDoOutput(true);
        connection.connect();

        StreamWriterFactory writerFactory = Lookup.getDefault().lookup(StreamWriterFactory.class);

        OutputStream out = null;
        try {
            out = connection.getOutputStream();
        } catch (UnknownServiceException e) {
            // protocol doesn't support output
            return;
        }
        StreamWriter writer = writerFactory.createStreamWriter(endpoint.getStreamType(), out);
        writer.handleGraphEvent(event);
        out.flush();
        out.close();
        connection.getInputStream().close();

    } catch (IOException ex) {
        logger.log(Level.FINE, null, ex);
    }
}

From source file:gov.nih.nci.rembrandt.util.IGVHelper.java

private boolean checkIfGPFileExists(String fileName) throws MalformedURLException, IOException {
    BufferedInputStream bis = null;
    try {/*  ww w  . j  a  v a 2s.  c  o  m*/
        String gpUrl = System.getProperty("gov.nih.nci.caintegrator.gp.server");
        gpUrl = gpUrl + "gp/jobResults/" + fileName;
        URL gctFile = new URL(gpUrl);
        //       URL gctFile = new URL("http://ncias-d757-v.nci.nih.gov:8080/gp/jobResults/1080/wwwrf.gct");

        URLConnection conn = gctFile.openConnection();

        String password = System.getProperty("gov.nih.nci.caintegrator.gp.publicuser.password");
        String loginPassword = rembrandtUser + ":" + password;
        String encoded = new sun.misc.BASE64Encoder().encode(loginPassword.getBytes());
        conn.setRequestProperty("Authorization", "Basic " + encoded);

        bis = new BufferedInputStream(conn.getInputStream());
        if (bis != null) {
            try {
                bis.close();
                return true;
            } catch (IOException e) {

            }
        }
    } catch (Exception ex) {
        return false;
    }
    return false;

}