Example usage for java.net URLConnection addRequestProperty

List of usage examples for java.net URLConnection addRequestProperty

Introduction

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

Prototype

public void addRequestProperty(String key, String value) 

Source Link

Document

Adds a general request property specified by a key-value pair.

Usage

From source file:jp.go.nict.langrid.serviceexecutor.google.GoogleTranslation.java

@SuppressWarnings("unchecked")
private String invokeTranslation(Language sourceLang, Language targetLang, String source)
        throws InvalidParameterException, ProcessFailedException {
    InputStream is = null;//  ww  w .  j  a  va 2  s  .  c o m
    try {
        URLConnection con = new URL(TRANSLATE_URL).openConnection();
        con.setDoOutput(true);
        con.setReadTimeout(timeoutMillis);
        con.addRequestProperty("Referer", referer);
        writeParameters(con, sourceLang, targetLang, source);

        is = con.getInputStream();
        String json = StreamUtil.readAsString(is, CharsetUtil.newUTF8Decoder());
        try {
            TranslationResult result = JSON.decode(json, TranslationResult.class);
            ResponseData responseData = result.responseData;
            int status = result.responseStatus;
            String key = sourceLang.getCode() + ":" + targetLang.getCode();
            if (status == 200) {
                langPairCache.putInCache(key, true);
                return StringEscapeUtils.unescapeHtml(responseData.translatedText.replaceAll("'", "'"));
            } else {
                String details = result.responseDetails;
                if (details.equals("invalid translation language pair")) {
                    langPairCache.putInCache(key, false);
                    languagePairValidator.get().getUniqueLanguagePair(Collections.EMPTY_LIST);
                }
                throw new ProcessFailedException(result.responseStatus + ": " + details);
            }
        } catch (JSONException e) {
            String message = "failed to read translated text: " + json;
            logger.log(Level.WARNING, message, e);
            throw new ProcessFailedException(message);
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, "failed to execute service.", e);
        throw new ProcessFailedException(e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:com.typhoon.newsreader.engine.ChannelRefresh.java

public List<FeedsListItem> parser(String link) {
    if (link.contains("www.24h.com.vn")) {
        try {//from   w w w. ja va  2 s.  c  o m
            URL url = new URL(link);
            URLConnection connection = url.openConnection();
            connection.addRequestProperty("http.agent", USER_AGENT);
            InputSource input = new InputSource(url.openStream());
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setValidating(false);
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();

            reader.setContentHandler(this);
            reader.parse(input);

            return getFeedsList();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    } else {
        try {
            // URL url= new URL(link);
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(link);
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setValidating(false);
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            reader.setContentHandler(this);
            InputSource inStream = new InputSource();
            inStream.setCharacterStream(new StringReader(EntityUtils.toString(entity)));
            reader.parse(inStream);
            return getFeedsList();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
            return null;
        } catch (SAXException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

From source file:net.ae97.totalpermissions.update.UpdateChecker.java

@Override
public void run() {
    if (disabled) {
        return;//from  w  w w . jav a 2 s . c om
    }
    try {
        URL url = new URL("https://api.curseforge.com/servermods/files?projectIds=54850");
        URLConnection conn = url.openConnection();
        conn.setConnectTimeout(5000);
        if (apikey != null && !apikey.isEmpty() && !apikey.equals("PUT_API_KEY_HERE")) {
            conn.addRequestProperty("X-API-Key", apikey);
        }
        conn.addRequestProperty("User-Agent", "Updater - TotalPermissions-v" + pluginVersion);
        conn.setDoOutput(true);

        BufferedReader reader = null;
        JsonElement details = null;
        try {
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String json = reader.readLine();
            JsonArray array = new JsonParser().parse(json).getAsJsonArray();
            details = array.get(array.size() - 1);
        } catch (IOException e) {
            if (e.getMessage().contains("HTTP response code: 403")) {
                throw new IOException("CurseAPI rejected API-KEY", e);
            }
            throw e;
        } finally {
            if (reader != null) {
                reader.close();
            }
        }

        if (details == null) {
            return;
        }

        String onlineVersion = details.getAsJsonObject().get("name").getAsString();

        if (!checkForUpdate(pluginVersion, onlineVersion)) {
            return;
        }

        pluginLogger.log(Level.INFO, "Update found! Current: {0} Latest: {1}",
                new String[] { StringUtils.join(getVersionInts(pluginVersion), "."),
                        StringUtils.join(getVersionInts(onlineVersion), ".") });

        if (!download) {
            return;
        }

        String downloadLink = details.getAsJsonObject().get("downloadUrl").getAsString();

        String pluginFileName = pluginFile.getName();

        if (!pluginFileName.equalsIgnoreCase(pluginName + ".jar")) {
            pluginLogger.log(Level.WARNING, "FILE NAME IS NOT {0}.jar! Forcing rename", pluginName);
            pluginFile.deleteOnExit();
        }

        File output = new File(Bukkit.getUpdateFolderFile(), pluginName + ".jar");
        download(downloadLink, output);

    } catch (MalformedURLException ex) {
        pluginLogger.log(Level.SEVERE, "URL could not be created", ex);
    } catch (IOException ex) {
        pluginLogger.log(Level.SEVERE, "Error occurred on checking for update for " + pluginName, ex);
    }
}

From source file:jp.go.nict.langrid.serviceexecutor.google.GoogleTranslation.java

@SuppressWarnings("unchecked")
private ArrayList<String> invokeBatchTranslation(Language sourceLang, Language targetLang, String[] sources)
        throws InvalidParameterException, ProcessFailedException {
    InputStream is = null;// w w  w  .  j a  v a  2  s .  c o  m
    try {
        URLConnection con = new URL(TRANSLATE_URL).openConnection();
        con.setDoOutput(true);
        con.setReadTimeout(timeoutMillis);
        con.addRequestProperty("Referer", referer);
        writeParameters(con, sourceLang, targetLang, sources);

        is = con.getInputStream();
        String json = StreamUtil.readAsString(is, CharsetUtil.newUTF8Decoder());
        try {
            BatchTranslationResult result = JSON.decode(json, BatchTranslationResult.class);
            int status = result.responseStatus;
            String key = sourceLang.getCode() + ":" + targetLang.getCode();

            if (status == 200) {
                langPairCache.putInCache(key, true);
                ArrayList<String> resultArray = new ArrayList<String>();
                for (TranslationResult r : result.responseData) {
                    resultArray.add(StringEscapeUtils
                            .unescapeHtml(r.responseData.translatedText.replaceAll("&#39;", "'")));
                }
                return resultArray;
            } else {
                String details = result.responseDetails;
                if (details.equals("invalid translation language pair")) {
                    langPairCache.putInCache(key, false);
                    languagePairValidator.get().getUniqueLanguagePair(Collections.EMPTY_LIST);
                }
                throw new ProcessFailedException(result.responseStatus + ": " + details);
            }
        } catch (JSONException e) {
            String message = "failed to read translated text: " + json;
            logger.log(Level.WARNING, message, e);
            throw new ProcessFailedException(message);
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, "failed to execute service.", e);
        throw new ProcessFailedException(e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:com.gmail.bleedobsidian.itemcase.Updater.java

/**
 * Query ServerMods API for project variables.
 *
 * @return If successful or not./*from   ww w.j a v  a2  s  .c  o m*/
 */
private boolean query() {
    try {
        final URLConnection con = this.url.openConnection();
        con.setConnectTimeout(5000);
        con.setReadTimeout(5000);

        if (this.apiKey != null) {
            con.addRequestProperty("X-API-Key", this.apiKey);
        }

        con.addRequestProperty("User-Agent", this.plugin.getName() + " Updater");

        con.setDoOutput(true);

        final BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
        final String response = reader.readLine();

        final JSONArray array = (JSONArray) JSONValue.parse(response);

        if (array.size() == 0) {
            this.result = UpdateResult.ERROR_ID;
            return false;
        }

        this.versionName = (String) ((JSONObject) array.get(array.size() - 1)).get("name");
        this.versionLink = (String) ((JSONObject) array.get(array.size() - 1)).get("downloadUrl");
        this.versionType = (String) ((JSONObject) array.get(array.size() - 1)).get("releaseType");
        this.versionGameVersion = (String) ((JSONObject) array.get(array.size() - 1)).get("gameVersion");

        return true;
    } catch (IOException e) {
        if (e.getMessage().contains("HTTP response code: 403")) {
            this.result = UpdateResult.ERROR_APIKEY;
        } else {
            this.result = UpdateResult.ERROR_SERVER;
        }

        return false;
    }

}

From source file:net.femtoparsec.jwhois.impl.provider.RIPEDatabaseHttpProvider.java

@Override
protected void setUpConnection(URLConnection connection) {
    super.setUpConnection(connection);

    String acceptValue;/*  w  ww  .j  a va2s  .  com*/
    switch (this.getResultFormat()) {
    case JSON:
        acceptValue = "application/json";
        break;
    case XML:
        acceptValue = "text/xml";
        break;
    default:
        acceptValue = null;
    }

    if (acceptValue != null) {
        connection.addRequestProperty("Accept", acceptValue);
    }
}

From source file:org.apache.marmotta.ucuenca.wk.commons.function.SemanticDistance.java

private synchronized String http(String s) throws SQLException, IOException {

    Statement stmt = conn.createStatement();
    String sql;//from   w ww. j av  a 2 s. com
    sql = "SELECT * FROM cache where cache.key='" + commonservices.getMD5(s) + "'";
    java.sql.ResultSet rs = stmt.executeQuery(sql);
    String resp = "";
    if (rs.next()) {
        resp = rs.getString("value");
        rs.close();
        stmt.close();
    } else {
        rs.close();
        stmt.close();
        final URL url = new URL(s);
        final URLConnection connection = url.openConnection();
        connection.setConnectTimeout(60000);
        connection.setReadTimeout(60000);
        connection.addRequestProperty("User-Agent",
                "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0");
        connection.addRequestProperty("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        final Scanner reader = new Scanner(connection.getInputStream(), "UTF-8");
        while (reader.hasNextLine()) {
            final String line = reader.nextLine();
            resp += line + "\n";
        }
        reader.close();

        try {
            JsonParser parser = new JsonParser();
            parser.parse(resp);
            PreparedStatement stmt2 = conn.prepareStatement("INSERT INTO cache (key, value) values (?, ?)");
            stmt2.setString(1, commonservices.getMD5(s));
            stmt2.setString(2, resp);
            stmt2.executeUpdate();
            stmt2.close();
        } catch (Exception e) {

        }
    }

    return resp;
}

From source file:myGroup.gwt.client.ui.MyUI.java

private Container getItemContiner() {
    IndexedContainer container = new IndexedContainer();
    container.addContainerProperty("First", String.class, "1st");
    container.addContainerProperty("Second", String.class, "2nd");
    //        WebClient client = WebClient.create("http://195.228.45.136:8181/cxf/test");
    //        client = client.accept("application/json")
    //                .type("application/json")
    //                .path("/say/list");
    //        TestResp testResp = client.get(TestResp.class);
    URL url = null;//from w w  w  .j a v a2  s . c o  m
    URLConnection connection = null;
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    try {
        url = new URL("http://195.228.45.136:8181/cxf/test/user/" + user.getUserId() + "/item");
        connection = url.openConnection();
        connection.addRequestProperty("Referer", "WishBox frontend");
    } catch (Exception e) {
        e.printStackTrace();
    }

    String line;
    StringBuilder builder = new StringBuilder();
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    Gson gson = new Gson();
    ObjectMapper mapper = new ObjectMapper();
    Collection<Item> testRespList = null;
    try {
        testRespList = mapper.readValue(builder.toString(), new TypeReference<Collection<Item>>() {
        });
    } catch (IOException e) {
        System.out.println("input: " + builder.toString());
        System.out.println("hiba: " + e.getMessage());
        e.printStackTrace();
    }
    //TestResp[] testResps = gson.fromJson(builder.toString(), TestResp[].class);

    for (Item testResp : testRespList) {
        com.vaadin.data.Item item = container.addItem(testResp.toString());
        item.getItemProperty("First").setValue(testResp.getName());
        item.getItemProperty("Second").setValue(testResp.getPattern());
    }
    return container;
}

From source file:com.fluidops.iwb.deepzoom.ImageLoader.java

/**
 * Loads an image from a URL to a local file
 * @param uri - The URL from which to obtain the image
 * @param file - The file to store the image to
 * @return/*  w ww .  ja  v  a  2 s  .  c o m*/
 */

public boolean loadGoogleImage(URI uri, Map<URI, Set<Value>> facets, File file) {
    String url;
    URL u;
    InputStream is = null;
    BufferedInputStream dis = null;
    int b;
    try {
        URL googleUrl;
        ReadDataManager dm = EndpointImpl.api().getDataManager();
        String label = dm.getLabel(uri);
        // TODO: currently hard coded logic for data sets 
        // should find more flexible logic
        if (uri.stringValue().startsWith("http://www.ckan.net/"))
            label += " logo";

        googleUrl = new URL("http://ajax.googleapis.com/ajax/services/search/images?v=1.0&" + "q="
                + URLEncoder.encode(label, "UTF-8"));

        URLConnection connection = googleUrl.openConnection();
        connection.addRequestProperty("Referer", "http://iwb.fluidops.com/");

        String content = GenUtil.readUrl(connection.getInputStream());

        try {
            JSONObject json = new JSONObject(content);

            JSONObject response = json.getJSONObject("responseData");
            JSONArray results = response.getJSONArray("results");
            JSONObject result = results.getJSONObject(0);
            url = result.getString("unescapedUrl");

        }

        catch (JSONException e) {
            return false;
        }

        u = new URL(url);

        try {
            is = u.openStream();
        } catch (IOException e) {
            System.out.println("File could not be found: " + url);

            // quick fix: 200px-Image n/a, try smaller pic (for Wikipedia images)
            url = url.replace("200px", "94px");
            u = new URL(url);
            try {
                is = u.openStream();
            } catch (IOException e2) {
                System.out.println("also smaller image not available");
                return false;
            }
        }
        dis = new BufferedInputStream(is);
        BufferedOutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(file));
            while ((b = dis.read()) != -1) {
                out.write(b);
            }
            out.flush();
        } finally {
            IOUtils.closeQuietly(out);
        }
    } catch (MalformedURLException mue) {
        logger.error(mue.getMessage(), mue);
        return false;

    } catch (IOException ioe) {
        logger.error(ioe.getMessage(), ioe);
        return false;
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(dis);
    }
    return true;
}

From source file:edu.indiana.d2i.htrc.dataapi.DataAPIWrapper.java

private long retrieveContents(String requestURL, Map<String, Map<String, String>> volPageContents) {
    long totalTime = 0;
    long startTime = 0;
    long endTime = 0;

    InputStream inputStream = null;

    try {//from   w w  w.j ava2  s  .  com

        if (isSecureConn)
            initSSL(isSelfSigned);

        startTime = System.currentTimeMillis();
        URL url = new URL(requestURL);
        URLConnection connection = url.openConnection();

        connection.addRequestProperty("Authorization", "Bearer " + oauth2Token);

        if (isSecureConn)
            assert connection instanceof HttpsURLConnection;
        else
            assert connection instanceof HttpURLConnection;

        HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
        httpURLConnection.setRequestMethod("GET");

        if (httpURLConnection.getResponseCode() == 200) {
            inputStream = httpURLConnection.getInputStream();
            openZipStream(inputStream, volPageContents, isConcat);

            endTime = System.currentTimeMillis();
            totalTime = endTime - startTime;

        } else {
            int responseCode = httpURLConnection.getResponseCode();
            log.error("Server response code : " + responseCode);
            inputStream = httpURLConnection.getInputStream();
            String responseBody = OAuthUtils.saveStreamAsString(inputStream);

            log.error(responseBody);

            /**
             * Currently server doesn't provide a way to renew the token, or
             * there is no response code particularly for the token
             * expiration, 500 will be returned in this case, but it can
             * also indicate other sorts of errors. In the following code we
             * just renew the token anyway
             * 
             */
            if (responseCode == 500)
                authenticate();

        }

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        log.error("MalformedURLException : " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        log.error("IOException : " + e.getMessage());
        e.printStackTrace();
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        log.error("KeyManagementException : " + e.getMessage());
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        log.error("NoSuchAlgorithmException : " + e.getMessage());
        e.printStackTrace();
    } catch (OAuthSystemException e) {
        // TODO Auto-generated catch block
        log.error("OAuthSystemException : " + e.getMessage());
        e.printStackTrace();
    } catch (OAuthProblemException e) {
        // TODO Auto-generated catch block
        log.error("OAuthProblemException : " + e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            if (inputStream != null)
                inputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            log.error("Failed to close zip inputstream");
            log.error("IOException : " + e.getMessage());
            e.printStackTrace();
        }
    }

    return totalTime;
}