예제 #1
0
 public T header(String name, String value) {
   Validate.notEmpty(name, "Header name must not be empty");
   Validate.notNull(value, "Header value must not be null");
   removeHeader(name); // ensures we don't get an "accept-encoding" and a "Accept-Encoding"
   headers.put(name, value);
   return (T) this;
 }
예제 #2
0
 public Connection data(String... keyvals) {
   Validate.notNull(keyvals, "Data key value pairs must not be null");
   Validate.isTrue(keyvals.length % 2 == 0, "Must supply an even number of key value pairs");
   for (int i = 0; i < keyvals.length; i += 2) {
     String key = keyvals[i];
     String value = keyvals[i + 1];
     Validate.notEmpty(key, "Data key must not be empty");
     Validate.notNull(value, "Data value must not be null");
     req.data(KeyVal.create(key, value));
   }
   return this;
 }
예제 #3
0
  public static String getMetadataApplicationId(Context context) {
    Validate.notNull(context, "context");

    FacebookSdk.sdkInitialize(context);

    return FacebookSdk.getApplicationId();
  }
예제 #4
0
  public static String getMetadataApplicationId(Context context) {
    Validate.notNull(context, "context");

    Settings.loadDefaultsFromMetadata(context);

    return Settings.getApplicationId();
  }
예제 #5
0
 public Connection data(Map<String, String> data) {
   Validate.notNull(data, "Data map must not be null");
   for (Map.Entry<String, String> entry : data.entrySet()) {
     req.data(KeyVal.create(entry.getKey(), entry.getValue()));
   }
   return this;
 }
예제 #6
0
 public Connection url(String url) {
   Validate.notEmpty(url, "Must supply a valid URL");
   try {
     req.url(new URL(url));
   } catch (MalformedURLException e) {
     throw new IllegalArgumentException("Malformed URL: " + url, e);
   }
   return this;
 }
예제 #7
0
 private String getHeaderCaseInsensitive(String name) {
   Validate.notNull(name, "Header name must not be null");
   // quick evals for common case of title case, lower case, then scan for mixed
   String value = headers.get(name);
   if (value == null) value = headers.get(name.toLowerCase());
   if (value == null) {
     Map.Entry<String, String> entry = scanHeaders(name);
     if (entry != null) value = entry.getValue();
   }
   return value;
 }
예제 #8
0
 public String body() {
   Validate.isTrue(
       executed,
       "Request must be executed (with .execute(), .get(), or .post() before getting response body");
   // charset gets set from header on execute, and from meta-equiv on parse. parse may not have
   // happened yet
   String body;
   if (charset == null)
     body = Charset.forName(DataUtil.defaultCharset).decode(byteData).toString();
   else body = Charset.forName(charset).decode(byteData).toString();
   byteData.rewind();
   return body;
 }
예제 #9
0
 public Document parse() throws IOException {
   Validate.isTrue(
       executed,
       "Request must be executed (with .execute(), .get(), or .post() before parsing response");
   if (!req.ignoreContentType()
       && (contentType == null
           || !(contentType.startsWith("text/")
               || contentType.startsWith("application/xml")
               || contentType.startsWith("application/xhtml+xml"))))
     throw new IOException(
         String.format(
             "Unhandled content type \"%s\" on URL %s. Must be text/*, application/xml, or application/xhtml+xml",
             contentType, url.toString()));
   Document doc = DataUtil.parseByteData(byteData, charset, url.toExternalForm());
   byteData.rewind();
   charset = doc.outputSettings().charset().name(); // update charset from meta-equiv, possibly
   return doc;
 }
예제 #10
0
    static Response execute(Connection.Request req, Response previousResponse) throws IOException {
      Validate.notNull(req, "Request must not be null");
      String protocol = req.url().getProtocol();
      Validate.isTrue(
          protocol.equals("http") || protocol.equals("https"),
          "Only http & https protocols supported");

      // set up the request for execution
      if (req.method() == Connection.Method.GET && req.data().size() > 0)
        serialiseRequestUrl(req); // appends query string
      HttpURLConnection conn = createConnection(req);
      conn.connect();
      if (req.method() == Connection.Method.POST) writePost(req.data(), conn.getOutputStream());

      int status = conn.getResponseCode();
      boolean needsRedirect = false;
      if (status != HttpURLConnection.HTTP_OK) {
        if (status == HttpURLConnection.HTTP_MOVED_TEMP
            || status == HttpURLConnection.HTTP_MOVED_PERM
            || status == HttpURLConnection.HTTP_SEE_OTHER) needsRedirect = true;
        else if (!req.ignoreHttpErrors())
          throw new IOException(status + " error loading URL " + req.url().toString());
      }
      Response res = new Response(previousResponse);
      res.setupFromConnection(conn, previousResponse);
      if (needsRedirect && req.followRedirects()) {
        req.method(
            Method
                .GET); // always redirect with a get. any data param from original req are dropped.
        req.data().clear();
        req.url(new URL(req.url(), res.header("Location")));
        for (Map.Entry<String, String> cookie :
            res.cookies.entrySet()) { // add response cookies to request (for e.g. login posts)
          req.cookie(cookie.getKey(), cookie.getValue());
        }
        return execute(req, res);
      }
      res.req = req;

      InputStream bodyStream = null;
      InputStream dataStream = null;
      try {
        dataStream = conn.getErrorStream() != null ? conn.getErrorStream() : conn.getInputStream();
        bodyStream =
            res.hasHeader("Content-Encoding")
                    && res.header("Content-Encoding").equalsIgnoreCase("gzip")
                ? new BufferedInputStream(new GZIPInputStream(dataStream))
                : new BufferedInputStream(dataStream);

        res.byteData = DataUtil.readToByteBuffer(bodyStream);
        res.charset =
            DataUtil.getCharsetFromContentType(
                res.contentType); // may be null, readInputStream deals with it
      } finally {
        if (bodyStream != null) bodyStream.close();
        if (dataStream != null) dataStream.close();
      }

      res.executed = true;
      return res;
    }
예제 #11
0
 public KeyVal value(String value) {
   Validate.notNull(value, "Data value must not be null");
   this.value = value;
   return this;
 }
예제 #12
0
 public Connection referrer(String referrer) {
   Validate.notNull(referrer, "Referrer must not be null");
   req.header("Referer", referrer);
   return this;
 }
예제 #13
0
 public static KeyVal create(String key, String value) {
   Validate.notEmpty(key, "Data key must not be empty");
   Validate.notNull(value, "Data value must not be null");
   return new KeyVal(key, value);
 }
예제 #14
0
 public KeyVal key(String key) {
   Validate.notEmpty(key, "Data key must not be empty");
   this.key = key;
   return this;
 }
예제 #15
0
 public byte[] bodyAsBytes() {
   Validate.isTrue(
       executed,
       "Request must be executed (with .execute(), .get(), or .post() before getting response body");
   return byteData.array();
 }
예제 #16
0
 public Connection userAgent(String userAgent) {
   Validate.notNull(userAgent, "User agent must not be null");
   req.header("User-Agent", userAgent);
   return this;
 }
예제 #17
0
 public T removeHeader(String name) {
   Validate.notEmpty(name, "Header name must not be empty");
   Map.Entry<String, String> entry = scanHeaders(name); // remove is case insensitive too
   if (entry != null) headers.remove(entry.getKey()); // ensures correct case
   return (T) this;
 }
예제 #18
0
 public boolean hasHeader(String name) {
   Validate.notEmpty(name, "Header name must not be empty");
   return getHeaderCaseInsensitive(name) != null;
 }
예제 #19
0
 public String cookie(String name) {
   Validate.notNull(name, "Cookie name must not be null");
   return cookies.get(name);
 }
예제 #20
0
 public T method(Method method) {
   Validate.notNull(method, "Method must not be null");
   this.method = method;
   return (T) this;
 }
예제 #21
0
 public T url(URL url) {
   Validate.notNull(url, "URL must not be null");
   this.url = url;
   return (T) this;
 }
예제 #22
0
 public Request timeout(int millis) {
   Validate.isTrue(millis >= 0, "Timeout milliseconds must be 0 (infinite) or greater");
   timeoutMilliseconds = millis;
   return this;
 }
예제 #23
0
 public T removeCookie(String name) {
   Validate.notEmpty("Cookie name must not be empty");
   cookies.remove(name);
   return (T) this;
 }
예제 #24
0
 public boolean hasCookie(String name) {
   Validate.notEmpty("Cookie name must not be empty");
   return cookies.containsKey(name);
 }
예제 #25
0
 public T cookie(String name, String value) {
   Validate.notEmpty(name, "Cookie name must not be empty");
   Validate.notNull(value, "Cookie value must not be null");
   cookies.put(name, value);
   return (T) this;
 }
예제 #26
0
 public Request data(Connection.KeyVal keyval) {
   Validate.notNull(keyval, "Key val must not be null");
   data.add(keyval);
   return this;
 }
예제 #27
0
 public String header(String name) {
   Validate.notNull(name, "Header name must not be null");
   return getHeaderCaseInsensitive(name);
 }