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:io.druid.server.sql.SQLRunner.java

public static void main(String[] args) throws Exception {

    Options options = new Options();
    options.addOption("h", "help", false, "help");
    options.addOption("v", false, "verbose");
    options.addOption("e", "host", true, "endpoint [hostname:port]");

    CommandLine cmd = new GnuParser().parse(options, args);

    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("SQLRunner", options);
        System.exit(2);/*from w w  w  .  j  av  a  2  s .c  o m*/
    }

    String hostname = cmd.getOptionValue("e", "localhost:8080");
    String sql = cmd.getArgs().length > 0 ? cmd.getArgs()[0] : STATEMENT;

    ObjectMapper objectMapper = new DefaultObjectMapper();
    ObjectWriter jsonWriter = objectMapper.writerWithDefaultPrettyPrinter();

    CharStream stream = new ANTLRInputStream(sql);
    DruidSQLLexer lexer = new DruidSQLLexer(stream);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    DruidSQLParser parser = new DruidSQLParser(tokenStream);
    lexer.removeErrorListeners();
    parser.removeErrorListeners();

    lexer.addErrorListener(ConsoleErrorListener.INSTANCE);
    parser.addErrorListener(ConsoleErrorListener.INSTANCE);

    try {
        DruidSQLParser.QueryContext queryContext = parser.query();
        if (parser.getNumberOfSyntaxErrors() > 0)
            throw new IllegalStateException();
        //      parser.setBuildParseTree(true);
        //      System.err.println(q.toStringTree(parser));
    } catch (Exception e) {
        String msg = e.getMessage();
        if (msg != null)
            System.err.println(e);
        System.exit(1);
    }

    final Query query;
    final TypeReference typeRef;
    boolean groupBy = false;
    if (parser.groupByDimensions.isEmpty()) {
        query = Druids.newTimeseriesQueryBuilder().dataSource(parser.getDataSource())
                .aggregators(new ArrayList<AggregatorFactory>(parser.aggregators.values()))
                .postAggregators(parser.postAggregators).intervals(parser.intervals)
                .granularity(parser.granularity).filters(parser.filter).build();

        typeRef = new TypeReference<List<Result<TimeseriesResultValue>>>() {
        };
    } else {
        query = GroupByQuery.builder().setDataSource(parser.getDataSource())
                .setAggregatorSpecs(new ArrayList<AggregatorFactory>(parser.aggregators.values()))
                .setPostAggregatorSpecs(parser.postAggregators).setInterval(parser.intervals)
                .setGranularity(parser.granularity).setDimFilter(parser.filter)
                .setDimensions(new ArrayList<DimensionSpec>(parser.groupByDimensions.values())).build();

        typeRef = new TypeReference<List<Row>>() {
        };
        groupBy = true;
    }

    String queryStr = jsonWriter.writeValueAsString(query);
    if (cmd.hasOption("v"))
        System.err.println(queryStr);

    URL url = new URL(String.format("http://%s/druid/v2/?pretty", hostname));
    final URLConnection urlConnection = url.openConnection();
    urlConnection.addRequestProperty("content-type", MediaType.APPLICATION_JSON);
    urlConnection.getOutputStream().write(StringUtils.toUtf8(queryStr));
    BufferedReader stdInput = new BufferedReader(
            new InputStreamReader(urlConnection.getInputStream(), Charsets.UTF_8));

    Object res = objectMapper.readValue(stdInput, typeRef);

    Joiner tabJoiner = Joiner.on("\t");

    if (groupBy) {
        List<Row> rows = (List<Row>) res;
        Iterable<String> dimensions = Iterables.transform(parser.groupByDimensions.values(),
                new Function<DimensionSpec, String>() {
                    @Override
                    public String apply(@Nullable DimensionSpec input) {
                        return input.getOutputName();
                    }
                });

        System.out.println(
                tabJoiner.join(Iterables.concat(Lists.newArrayList("timestamp"), dimensions, parser.fields)));
        for (final Row r : rows) {
            System.out.println(tabJoiner.join(Iterables.concat(
                    Lists.newArrayList(parser.granularity.toDateTime(r.getTimestampFromEpoch())),
                    Iterables.transform(parser.groupByDimensions.values(),
                            new Function<DimensionSpec, String>() {
                                @Override
                                public String apply(@Nullable DimensionSpec input) {
                                    return Joiner.on(",").join(r.getDimension(input.getOutputName()));
                                }
                            }),
                    Iterables.transform(parser.fields, new Function<String, Object>() {
                        @Override
                        public Object apply(@Nullable String input) {
                            return r.getFloatMetric(input);
                        }
                    }))));
        }
    } else {
        List<Result<TimeseriesResultValue>> rows = (List<Result<TimeseriesResultValue>>) res;
        System.out.println(tabJoiner.join(Iterables.concat(Lists.newArrayList("timestamp"), parser.fields)));
        for (final Result<TimeseriesResultValue> r : rows) {
            System.out.println(tabJoiner.join(Iterables.concat(Lists.newArrayList(r.getTimestamp()),
                    Lists.transform(parser.fields, new Function<String, Object>() {
                        @Override
                        public Object apply(@Nullable String input) {
                            return r.getValue().getMetric(input);
                        }
                    }))));
        }
    }

    CloseQuietly.close(stdInput);
}

From source file:SifterJava.java

public static void main(String[] args) throws Exception {
    /** Enter your sifter account and access key in the 
    ** string fields below *//*from  ww  w. j a  v a 2  s  .  co m*/

    // create URL object to SifterAPI
    String yourAccount = "your-account"; // enter your account name
    String yourDomain = yourAccount + ".sifterapp.com";
    URL sifter = new URL("https", yourDomain, "/api/projects");

    // open connectino to SifterAPI
    URLConnection sifterConnection = sifter.openConnection();

    // add Access Key to header request
    String yourAccessKey = "your-32char-sifterapi-access-key"; // enter your access key
    sifterConnection.setRequestProperty("X-Sifter-Token", yourAccessKey);

    // add Accept: application/json to header - also not necessary
    sifterConnection.addRequestProperty("Accept", "application/json");

    // URLconnection.connect() not necessary
    // getInputStream will connect

    // don't make a content handler, org.json reads a stream!

    // create buffer and open input stream
    BufferedReader in = new BufferedReader(new InputStreamReader(sifterConnection.getInputStream()));
    // don't readline, create stringbuilder, append, create string

    // construct json tokener from input stream or buffered reader
    JSONTokener x = new JSONTokener(in);

    // initialize "projects" JSONObject from string
    JSONObject projects = new JSONObject(x);

    // prettyprint "projects"
    System.out.println("************ projects ************");
    System.out.println(projects.toString(2));

    // array of projects
    JSONArray array = projects.getJSONArray("projects");
    int arrayLength = array.length();
    JSONObject[] p = new JSONObject[arrayLength];

    // projects
    for (int i = 0; i < arrayLength; i++) {
        p[i] = array.getJSONObject(i);
        System.out.println("************ project: " + (i + 1) + " ************");
        System.out.println(p[i].toString(2));
    }

    // check field names
    String[] checkNames = { "api_url", "archived", "api_issues_url", "milestones_url", "api_milestones_url",
            "api_categories_url", "issues_url", "name", "url", "api_people_url", "primary_company_name" };

    // project field names
    String[] fieldNames = JSONObject.getNames(p[0]);
    int numKeys = p[0].length();
    SifterProj[] proj = new SifterProj[arrayLength];

    for (int j = 0; j < arrayLength; j++) {
        proj[j] = new SifterProj(p[j].get("api_url").toString(), p[j].get("archived").toString(),
                p[j].get("api_issues_url").toString(), p[j].get("milestones_url").toString(),
                p[j].get("api_milestones_url").toString(), p[j].get("api_categories_url").toString(),
                p[j].get("issues_url").toString(), p[j].get("name").toString(), p[j].get("url").toString(),
                p[j].get("api_people_url").toString(), p[j].get("primary_company_name").toString());
        System.out.println("************ project: " + (j + 1) + " ************");
        for (int i = 0; i < numKeys; i++) {
            System.out.print(fieldNames[i]);
            System.out.print(" : ");
            System.out.println(p[j].get(fieldNames[i]));
        }
    }
}

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

public static void main(String[] args) {
    URL url;/*from ww  w . j  av  a2 s. c om*/
    try {
        url = new URL("http://ajax.googleapis.com/ajax/services/search/images?v=1.0&" + "q=dbpedia");

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

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

        JSONObject json = new JSONObject(content);
        System.out.println(json);
    } catch (RuntimeException e) {
        logger.error(e.getMessage(), e);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }

}

From source file:com.shahnami.fetch.Controller.JsonReader.java

public static JSONArray readJsonArrayFromUrl(String url) throws IOException, JSONException {
    URLConnection urlConnection = new URL(url).openConnection();
    urlConnection.addRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.2) Gecko/20040803");
    try (InputStream is = urlConnection.getInputStream()) {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        JSONArray json = new JSONArray(jsonText);
        return json;
    }/*  w w  w  .  ja  va  2 s . c  om*/
}

From source file:com.shahnami.fetch.Controller.JsonReader.java

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    URLConnection urlConnection = new URL(url).openConnection();
    urlConnection.addRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.2) Gecko/20040803");
    try (InputStream is = urlConnection.getInputStream()) {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        JSONObject json = new JSONObject(jsonText);
        return json;
    }//ww  w  . ja  v  a2 s  .  c  o m
}

From source file:net.zifnab.google.google.java

public static String[] search(String terms) {
    String key = "ABQIAAAA6KnsrpTIyjEMJ1EqHjKG_xRBaorPdDj7IJd2xMtxtE9DwSoKhRQgazFCenlI-wnGV1574jW06163iw";
    String ip = "173.255.208.207";
    System.out.println("Google-ing: " + terms);
    terms = terms.replace(' ', '+');
    try {/*w  ww .  j  av a 2  s . c om*/
        //Opens URL
        URL googlesearch = new URL("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&" + "q=" + terms
                + "&key=" + key + "&userip=" + ip);
        URLConnection connection = googlesearch.openConnection();
        connection.addRequestProperty("Referer", "zifnab06.net");
        //Read in JSON crap from URL.
        String line;
        StringBuilder results = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while ((line = reader.readLine()) != null) {
            results.append(line + "\n");
        }
        //JSON Crap. Google needs to use XML....
        JSONObject json = new JSONObject(results.toString());
        JSONArray ja = json.getJSONObject("responseData").getJSONArray("results");
        JSONObject j = ja.getJSONObject(0);
        //Returns Title: URL
        String[] ret = { j.toString(), StringEscapeUtils.unescapeHtml4(j.getString("titleNoFormatting")) + ": "
                + URLDecoder.decode(j.getString("url"), "UTF-8") };
        return ret;
    } catch (MalformedURLException e) {
        //Shouldn't EVER hit this.
        System.out.println(e);
        String[] ret = { "error", e.toString() };
        return (ret);
    } catch (IOException e) {
        //Shouldn't EVER hit this.
        System.out.println(e);
        String[] ret = { "error", e.toString() };
        return (ret);

    } catch (Exception e) {
        System.out.println(e);
        String[] ret = { "error", "No results found!" };
        return ret;
    }
}

From source file:disko.utils.GoogleLanguageDetector.java

/**
 * Returns the language code (en, ja, es, pt, etc.) for the language of the given String,
 * or null if the detection failed or was considered unreliable. 
 *  //  ww  w.  j a  v  a 2 s. c  o m
 * @param text
 * @return the language code
 */
public static String detectLanguage(String text) {

    String language = null;

    try {
        String encoded = URLEncoder.encode(text, "UTF-8");
        URL url = new URL(GOOGLE_LANGUAGE_DETECT_URL + encoded);
        URLConnection connection = DiscoProxySettings.newConnection(url);

        connection.addRequestProperty("Referer", DEFAULT_REFERRER);

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

        JSONObject json = new JSONObject(builder.toString());
        log.debug("Answer for '" + text + "' :" + json);

        JSONObject responseData = json.getJSONObject("responseData");
        double confidence = responseData.getDouble("confidence");
        boolean isReliable = responseData.getBoolean("isReliable");

        language = responseData.getString("language");

        log.debug("Language " + language + "\tConfidence: " + confidence + "\tisReliable:" + isReliable);

        return (isReliable && language.length() > 0) ? language : null;

    } catch (Exception e) {
        log.error("Error detecting language of '" + text + "'", e);
        return null;
    }
}

From source file:mp3downloader.ZingSearch.java

private static String getSearchResult(String term, int searchType)
        throws UnsupportedEncodingException, MalformedURLException, IOException {
    String data = "{\"kw\": \"" + term + "\", \"t\": " + searchType + ", \"rc\": 50}";
    String jsonData = URLEncoder.encode(Base64.getEncoder().encodeToString(data.getBytes("UTF-8")), "UTF-8");
    String signature = hash_hmac(jsonData, privateKey);
    String urlString = searchUrl + "publicKey=" + publicKey + "&signature=" + signature + "&jsondata="
            + jsonData;//w  w  w. j a  v  a 2  s.c  o  m
    URL url = new URL(urlString);
    URLConnection urlConn = url.openConnection();
    urlConn.addRequestProperty("User-Agent",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36");
    InputStream is = urlConn.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
    String line;
    StringBuilder sb = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        sb.append(line);
        sb.append("\n");
    }
    is.close();
    String content = sb.toString();
    return content;
}

From source file:mp3downloader.ZingSearch.java

private static String getDetailResult(String id, String searchType)
        throws UnsupportedEncodingException, MalformedURLException, IOException {
    String data = "{\"id\": \"" + id + "\", \"t\": \"" + searchType + "\"}";
    String jsonData = URLEncoder.encode(Base64.getEncoder().encodeToString(data.getBytes("UTF-8")), "UTF-8");
    String signature = hash_hmac(jsonData, privateKey);
    String urlString = detailUrl + "publicKey=" + publicKey + "&signature=" + signature + "&jsondata="
            + jsonData;// w w  w  .  ja  va 2s.  c o  m
    URL url = new URL(urlString);
    URLConnection urlConn = url.openConnection();
    urlConn.addRequestProperty("User-Agent",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36");
    InputStream is = urlConn.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
    String line;
    StringBuilder sb = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        sb.append(line);
        sb.append("\n");
    }
    is.close();
    String content = sb.toString();
    return content;
}

From source file:me.fromgate.facechat.UpdateChecker.java

private static void updateLastVersion() {
    if (!enableUpdateChecker)
        return;/*from  www.j a  v a  2 s.  c  o m*/
    URL url = null;
    try {
        url = new URL(projectApiUrl);
    } catch (Exception e) {
        log("Failed to create URL: " + projectApiUrl);
        return;
    }
    try {
        URLConnection conn = url.openConnection();
        conn.addRequestProperty("X-API-Key", null);
        conn.addRequestProperty("User-Agent", projectName + " using UpdateChecker (by fromgate)");
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String response = reader.readLine();
        JSONArray array = (JSONArray) JSONValue.parse(response);
        if (array.size() > 0) {
            JSONObject latest = (JSONObject) array.get(array.size() - 1);
            String plugin_name = (String) latest.get("name");
            projectLastVersion = plugin_name.replace(projectName + " v", "").trim();
        }
    } catch (Exception e) {
        log("Failed to check last version");
    }
}