Beispiel #1
0
 /**
  * Creates the query portion of a URI based on a list of NameValuePairs.
  *
  * @return The requested string.
  */
 protected String getQueryString() {
   final List<NameValuePair> params = getUrlParameters();
   String queryString = null;
   if (params != null && !params.isEmpty()) {
     final StringBuilder sb = new StringBuilder(params.size() * 10);
     for (NameValuePair param : params) {
       if (param == null) {
         continue;
       }
       sb.append(param.getName());
       sb.append("=");
       sb.append(param.getValue() == null ? "" : param.getValue());
       sb.append("&");
     }
     String potentialQueryString = sb.toString();
     if (potentialQueryString.length() == 0) {
       potentialQueryString = null;
     } else if (potentialQueryString.endsWith("&")) {
       potentialQueryString = potentialQueryString.substring(0, potentialQueryString.length() - 1);
     }
     // URLEncoder.encode cannot be used since it encodes things in a way the api does not expect,
     // particularly
     // dates.
     queryString = potentialQueryString;
   }
   return queryString;
 }
Beispiel #2
0
  /**
   * Parses the access token for a given subscriber provided the subscriber has completed the
   * authentication process via SMS.
   *
   * @param requestURL The callback URL along with the query parameters as called by the system upon
   *     SMS subscription by the user. Query parameters must include access_token and
   *     subscription_number.
   * @return Access token and subscriber number
   * @throws GlobeApiException
   */
  public AccessTokenResponse getAccessToken(String requestURL) throws GlobeApiException {
    try {
      URIBuilder builder = new URIBuilder(requestURL);
      List<NameValuePair> queryParams = builder.getQueryParams();

      String accessToken = null;
      String subscriberNumber = null;

      for (NameValuePair pair : queryParams) {
        if ("access_token".equals(pair.getName())) {
          accessToken = pair.getValue();
        } else if ("subscriber_number".equals(pair.getName())) {
          subscriberNumber = pair.getValue();
        }
      }

      if (accessToken == null || subscriberNumber == null) {
        throw new GlobeApiException("Request URL cannot be parsed");
      } else {
        return new AccessTokenResponse(accessToken, subscriberNumber);
      }
    } catch (URISyntaxException e) {
      throw new GlobeApiException("Request URL cannot be parsed", e);
    }
  }
  public List<NameValuePair> addnew_crypt_K_param(List<NameValuePair> param, String url) {
    ArrayList<NameValuePair> localArrayList = new ArrayList<NameValuePair>();

    generate_new_aes_key();

    localArrayList.add(new BasicNameValuePair("K", get_K()));

    Iterator<NameValuePair> i = param.iterator();
    boolean is_login = false;

    if (url.indexOf("login?") != -1 || url.indexOf("regist?") != -1) {
      is_login = true;
      ON = true;
    }
    while (i.hasNext()) {

      NameValuePair n = i.next();
      //		String val = Base64.encodeBase64String(new_encrypt_cn(E_CODE.AES,n.getValue()));
      String val =
          new String(Base64.encode(new_encrypt_cn(E_CODE.AES, n.getValue()), Base64.DEFAULT));

      if (is_login == true) {
        //			localArrayList.add(new BasicNameValuePair(n.getName(),
        // Base64.encodeBase64String(new_encrypt_cn(E_CODE.RSA_EP, val))));
        localArrayList.add(
            new BasicNameValuePair(
                n.getName(),
                new String(Base64.encode(new_encrypt_cn(E_CODE.RSA_EP, val), Base64.DEFAULT))));
      } else {
        localArrayList.add(new BasicNameValuePair(n.getName(), val));
      }
    }
    return localArrayList;
  }
Beispiel #4
0
 public static zzkq zza(zzae com_google_android_gms_analytics_internal_zzae, String str) {
   zzkq com_google_android_gms_internal_zzkq = null;
   zzv.zzr(com_google_android_gms_analytics_internal_zzae);
   if (TextUtils.isEmpty(str)) {
     return com_google_android_gms_internal_zzkq;
   }
   try {
     List<NameValuePair> parse =
         URLEncodedUtils.parse(new URI("?" + str), HttpRequest.CHARSET_UTF8);
     Map hashMap = new HashMap(parse.size());
     for (NameValuePair nameValuePair : parse) {
       hashMap.put(nameValuePair.getName(), nameValuePair.getValue());
     }
     zzkq com_google_android_gms_internal_zzkq2 = new zzkq();
     com_google_android_gms_internal_zzkq2.zzcU((String) hashMap.get("utm_content"));
     com_google_android_gms_internal_zzkq2.zzcS((String) hashMap.get("utm_medium"));
     com_google_android_gms_internal_zzkq2.setName((String) hashMap.get("utm_campaign"));
     com_google_android_gms_internal_zzkq2.zzcR((String) hashMap.get("utm_source"));
     com_google_android_gms_internal_zzkq2.zzcT((String) hashMap.get("utm_term"));
     com_google_android_gms_internal_zzkq2.zzcV((String) hashMap.get("utm_id"));
     com_google_android_gms_internal_zzkq2.zzcW((String) hashMap.get("anid"));
     com_google_android_gms_internal_zzkq2.zzcX((String) hashMap.get("gclid"));
     com_google_android_gms_internal_zzkq2.zzcY((String) hashMap.get("dclid"));
     com_google_android_gms_internal_zzkq2.zzcZ((String) hashMap.get("aclid"));
     return com_google_android_gms_internal_zzkq2;
   } catch (URISyntaxException e) {
     com_google_android_gms_analytics_internal_zzae.zzd("No valid campaign data found", e);
     return com_google_android_gms_internal_zzkq;
   }
 }
Beispiel #5
0
 public static zzol zza(zzaf com_google_android_gms_analytics_internal_zzaf, String str) {
   zzol com_google_android_gms_internal_zzol = null;
   zzx.zzv(com_google_android_gms_analytics_internal_zzaf);
   if (TextUtils.isEmpty(str)) {
     return com_google_android_gms_internal_zzol;
   }
   try {
     List<NameValuePair> parse = URLEncodedUtils.parse(new URI("?" + str), HTTP.UTF_8);
     Map hashMap = new HashMap(parse.size());
     for (NameValuePair nameValuePair : parse) {
       hashMap.put(nameValuePair.getName(), nameValuePair.getValue());
     }
     zzol com_google_android_gms_internal_zzol2 = new zzol();
     com_google_android_gms_internal_zzol2.zzdL((String) hashMap.get("utm_content"));
     com_google_android_gms_internal_zzol2.zzdJ((String) hashMap.get("utm_medium"));
     com_google_android_gms_internal_zzol2.setName((String) hashMap.get("utm_campaign"));
     com_google_android_gms_internal_zzol2.zzdI((String) hashMap.get("utm_source"));
     com_google_android_gms_internal_zzol2.zzdK((String) hashMap.get("utm_term"));
     com_google_android_gms_internal_zzol2.zzdM((String) hashMap.get("utm_id"));
     com_google_android_gms_internal_zzol2.zzdN((String) hashMap.get("anid"));
     com_google_android_gms_internal_zzol2.zzdO((String) hashMap.get("gclid"));
     com_google_android_gms_internal_zzol2.zzdP((String) hashMap.get("dclid"));
     com_google_android_gms_internal_zzol2.zzdQ((String) hashMap.get("aclid"));
     return com_google_android_gms_internal_zzol2;
   } catch (URISyntaxException e) {
     com_google_android_gms_analytics_internal_zzaf.zzd("No valid campaign data found", e);
     return com_google_android_gms_internal_zzol;
   }
 }
Beispiel #6
0
  /**
   * Sends a POST request with a file and returns the response.
   *
   * @param endpoint The endpoint to send the request to.
   * @param file The file to upload
   * @param responseClass The class to deserialise the Json response to. Can be null if no response
   *     message is expected.
   * @param fields Any name-value pairs to serialise
   * @param <T> The type to deserialise the response to.
   * @return A {@link Response} containing the deserialised body, if any.
   * @throws IOException If an error occurs.
   * @see MultipartEntityBuilder
   */
  public <T> Response<T> post(
      Endpoint endpoint, File file, Class<T> responseClass, NameValuePair... fields)
      throws IOException {
    if (file == null) {
      return post(endpoint, responseClass, fields);
    } // deal with null case

    // Create the request
    HttpPost post = new HttpPost(endpoint.url());
    post.setHeaders(combineHeaders());

    // Add fields as text pairs
    MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
    for (NameValuePair field : fields) {
      multipartEntityBuilder.addTextBody(field.getName(), field.getValue());
    }
    // Add file as binary
    FileBody bin = new FileBody(file);
    multipartEntityBuilder.addPart("file", bin);

    // Set the body
    post.setEntity(multipartEntityBuilder.build());

    // Send the request and process the response
    try (CloseableHttpResponse response = httpClient().execute(post)) {
      T body = deserialiseResponseMessage(response, responseClass);
      return new Response<>(response.getStatusLine(), body);
    }
  }
Beispiel #7
0
 public static String encodeParameters(
     List<NameValuePair> postParams, String splitter, boolean quot, boolean sort) {
   if (sort)
     Collections.sort(
         postParams,
         new Comparator<NameValuePair>() {
           @Override
           public int compare(NameValuePair o1, NameValuePair o2) {
             return o1.getName().compareTo(o2.getName());
           }
         });
   StringBuilder buf = new StringBuilder();
   for (NameValuePair pair : postParams) {
     if (buf.length() != 0) {
       if (quot) {
         buf.append("\"");
       }
       buf.append(splitter);
     }
     buf.append(encode(pair.getName())).append("=");
     if (quot) {
       buf.append("\"");
     }
     buf.append(encode(pair.getValue()));
   }
   if (buf.length() != 0) {
     if (quot) {
       buf.append("\"");
     }
   }
   return buf.toString();
 }
 private static JsonNode encodeParamsAsJson(final List<NameValuePair> params) {
   final Map<String, String> map = new HashMap<String, String>(params.size());
   for (final NameValuePair nameValuePair : params) {
     map.put(nameValuePair.getName(), nameValuePair.getValue());
   }
   return new ObjectMapper().valueToTree(map);
 }
Beispiel #9
0
 public static String post(String url, NameValuePair[] nameValuePair) throws ApiException {
   Map<String, String> paramsMap = new HashMap<String, String>();
   for (NameValuePair t : nameValuePair) {
     paramsMap.put(t.getName(), t.getValue());
   }
   return post(url, paramsMap);
 }
Beispiel #10
0
  /**
   * Returns a list of {@link NameValuePair NameValuePairs} as parsed from an {@link HttpEntity}.
   * The encoding is taken from the entity's Content-Encoding header.
   *
   * <p>This is typically used while parsing an HTTP POST.
   *
   * @param entity The entity to parse
   * @throws IOException If there was an exception getting the entity's data.
   */
  public static List<NameValuePair> parse(final HttpEntity entity) throws IOException {
    List<NameValuePair> result = Collections.emptyList();

    String contentType = null;
    String charset = null;

    Header h = entity.getContentType();
    if (h != null) {
      HeaderElement[] elems = h.getElements();
      if (elems.length > 0) {
        HeaderElement elem = elems[0];
        contentType = elem.getName();
        NameValuePair param = elem.getParameterByName("charset");
        if (param != null) {
          charset = param.getValue();
        }
      }
    }

    if (contentType != null && contentType.equalsIgnoreCase(CONTENT_TYPE)) {
      final String content = EntityUtils.toString(entity, HTTP.ASCII);
      if (content != null && content.length() > 0) {
        result = new ArrayList<NameValuePair>();
        parse(result, new Scanner(content), charset);
      }
    }
    return result;
  }
  public BrowserMobHttpResponse execute() {
    // deal with PUT/POST requests
    if (method instanceof HttpEntityEnclosingRequestBase) {
      HttpEntityEnclosingRequestBase enclodingRequest = (HttpEntityEnclosingRequestBase) method;

      if (!nvps.isEmpty()) {
        try {
          if (!multiPart) {
            enclodingRequest.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
          } else {
            for (NameValuePair nvp : nvps) {
              multipartEntity.addPart(nvp.getName(), new StringBody(nvp.getValue()));
            }
            enclodingRequest.setEntity(multipartEntity);
          }
        } catch (UnsupportedEncodingException e) {
          LOG.severe("Could not find UTF-8 encoding, something is really wrong", e);
        }
      } else if (multipartEntity != null) {
        enclodingRequest.setEntity(multipartEntity);
      } else if (byteArrayEntity != null) {
        enclodingRequest.setEntity(byteArrayEntity);
      } else if (stringEntity != null) {
        enclodingRequest.setEntity(stringEntity);
      } else if (inputStreamEntity != null) {
        enclodingRequest.setEntity(inputStreamEntity);
      }
    }

    return client.execute(this);
  }
Beispiel #12
0
  /** Returns an HttpEntity containing all request parameters */
  public HttpEntity getEntity() {

    if (bodyEntity != null) {
      return bodyEntity;
    }

    HttpEntity result = null;

    if (fileParams != null && !fileParams.isEmpty()) {

      MultipartEntity multipartEntity =
          new MultipartEntity(HttpMultipartMode.STRICT, boundary, Charset.forName(charset));

      if (bodyParams != null && !bodyParams.isEmpty()) {
        for (NameValuePair param : bodyParams) {
          try {
            multipartEntity.addPart(param.getName(), new StringBody(param.getValue()));
          } catch (UnsupportedEncodingException e) {
            LogUtils.e(e.getMessage(), e);
          }
        }
      }

      for (ConcurrentHashMap.Entry<String, ContentBody> entry : fileParams.entrySet()) {
        multipartEntity.addPart(entry.getKey(), entry.getValue());
      }

      result = multipartEntity;
    } else if (bodyParams != null && !bodyParams.isEmpty()) {
      result = new BodyParamsEntity(bodyParams, charset);
    }

    return result;
  }
Beispiel #13
0
 /**
  * Constructs a BannerForm representing the given page on Banner. You must specify whether to
  * send a POST request or a GET request. If the URL contains a question mark followed by a
  * series of parameters (such as /somepage?x=y&a=1&foo=bar), this is a GET request. If the page
  * is accessed by filling out a form and clicking a submit button, and the URL of the result
  * page does not contain a question mark followed by the parameter list, this is a POST request;
  * you will need to get the parameter names and values from the HTML source. The parameters are
  * specified as NameValuePairs, a class provided by Apache for precisely this purpose.
  *
  * @param path the path (not including the protocol or domain) of the page on Banner
  * @param isPOST true if this page expects an HTTP POST request, false if it expects a GET
  * @param parms an array of parameter names mapped to default values
  */
 public BannerForm(String path, boolean isPOST, NameValuePair... parms) {
   this.path = path;
   this.isPOST = isPOST;
   defaultParms = parms;
   currentParms = new HashMap<String, String>();
   for (NameValuePair parm : parms) currentParms.put(parm.getName(), parm.getValue());
 }
Beispiel #14
0
 public HttpRequest addQueryStringParams(List<NameValuePair> nameValuePairs) {
   if (nameValuePairs != null) {
     for (NameValuePair nameValuePair : nameValuePairs) {
       uriBuilder.addParameter(nameValuePair.getName(), nameValuePair.getValue());
     }
   }
   return this;
 }
 public RequestParameters(final List<NameValuePair> pairs) {
   parameters = new LinkedHashMap<String, String>();
   final Iterator<NameValuePair> it = pairs.iterator();
   while (it.hasNext()) {
     final NameValuePair pair = it.next();
     parameters.put(pair.getName(), pair.getValue());
   }
 }
 private String getQueryStringParam(String query, String key) {
   List<NameValuePair> params = URLEncodedUtils.parse(query, Charset.defaultCharset());
   for (NameValuePair pair : params) {
     if (key.equals(pair.getName())) {
       return pair.getValue();
     }
   }
   return null;
 }
 public static List<NameValuePair> removeEntry(List<NameValuePair> params, String key) {
   List<NameValuePair> newList = new ArrayList<NameValuePair>();
   for (NameValuePair nvp : params) {
     if (!nvp.getName().equals(key)) {
       newList.add(nvp);
     }
   }
   return newList;
 }
 private List<NameValuePair> stripNulls(NameValuePair... nameValuePairs) {
   List<NameValuePair> params = new ArrayList<NameValuePair>();
   for (int i = 0; i < nameValuePairs.length; i++) {
     NameValuePair param = nameValuePairs[i];
     if (param.getValue() != null) {
       params.add(param);
     }
   }
   return params;
 }
Beispiel #19
0
  public static Map<String, String> convert(List<NameValuePair> nv) {

    Map<String, String> map = new HashMap<String, String>();

    for (NameValuePair nameValue : nv) {
      map.put(nameValue.getName(), nameValue.getValue());
    }

    return map;
  }
 public static String getString(List<NameValuePair> params, String key) {
   String retVal = "";
   for (NameValuePair nvp : params) {
     if (nvp.getName().equals(key)) {
       retVal = nvp.getValue();
       break;
     }
   }
   return retVal;
 }
 public void addBodyParameter(List<NameValuePair> nameValuePairs) {
   if (bodyParams == null) {
     bodyParams = new LinkedHashMap<String, NameValuePair>();
   }
   if (nameValuePairs != null && nameValuePairs.size() > 0) {
     for (NameValuePair pair : nameValuePairs) {
       bodyParams.put(pair.getName(), pair);
     }
   }
 }
Beispiel #22
0
 private MultipartEntity createMultipartEntity(
     String filename, File file, List<NameValuePair> postParams)
     throws UnsupportedEncodingException {
   MultipartEntity entity = new MultipartEntity();
   for (NameValuePair param : postParams) {
     entity.addPart(param.getName(), new StringBody(param.getValue(), Charset.defaultCharset()));
   }
   entity.addPart(filename, new FileBody(file));
   return entity;
 }
 public static long getLong(List<NameValuePair> params, String key) {
   long retVal = 0;
   for (NameValuePair nvp : params) {
     if (nvp.getName().equals(key)) {
       String value = nvp.getValue();
       retVal = Long.parseLong(value);
       break;
     }
   }
   return retVal;
 }
 public static int getInt(List<NameValuePair> params, String key) {
   int retVal = 0;
   for (NameValuePair nvp : params) {
     if (nvp.getName().equals(key)) {
       String value = nvp.getValue();
       retVal = Integer.parseInt(value);
       break;
     }
   }
   return retVal;
 }
  private static QueryHashMap<String, String> parseEventUrl(String url) throws Exception {
    QueryHashMap<String, String> values = new QueryHashMap<String, String>();

    List<NameValuePair> params = URLEncodedUtils.parse(new URI("http://localhost/" + url), "UTF-8");

    for (NameValuePair param : params) {
      values.put(param.getName(), param.getValue());
    }

    return values;
  }
 @Override
 protected List<NameValuePair> getProxyParameters(List<NameValuePair> requestParameters) {
   List<NameValuePair> proxyParameters = new ArrayList<NameValuePair>();
   for (NameValuePair requestParameter : requestParameters) {
     if (!"apiKey".equalsIgnoreCase(requestParameter.getName())) {
       proxyParameters.add(
           new BasicNameValuePair(requestParameter.getName(), requestParameter.getValue()));
     }
   }
   proxyParameters.add(new BasicNameValuePair("key", UITBASE_APIKEY));
   return proxyParameters;
 }
Beispiel #27
0
 private List<NameValuePair> stripNulls(NameValuePair... nameValuePairs) {
   List<NameValuePair> params = new ArrayList<NameValuePair>();
   for (int i = 0; i < nameValuePairs.length; i++) {
     NameValuePair param = nameValuePairs[i];
     if (param.getValue() != null) {
       if (DEBUG) LOG.log(Level.FINE, "Param: " + param);
       System.out.println("Param: " + param);
       params.add(param);
     }
   }
   return params;
 }
Beispiel #28
0
 private String joinAttributes(List<NameValuePair> attr) {
   StringBuilder builder = new StringBuilder();
   int i = 1;
   for (NameValuePair pair : attr) {
     builder.append(pair.getName()).append(":").append(pair.getValue());
     if (i < attr.size()) {
       builder.append(MiPushClient.ACCEPT_TIME_SEPARATOR);
     }
     i++;
   }
   return builder.toString();
 }
  /** {@inheritDoc} */
  @Override
  public int send(List<Argument> arguments) {

    m_arguments = arguments;

    String url = getUrl();
    if (url == null) {
      LOG.warn("send: url argument is null, HttpNotification requires a URL");
      return 1;
    }

    DefaultHttpClient client = new DefaultHttpClient();
    HttpUriRequest method = null;
    List<NameValuePair> posts = getPostArguments();

    if (posts == null) {
      method = new HttpGet(url);
      LOG.info("send: No \"post-\" arguments..., continuing with an HTTP GET using URL: {}", url);
    } else {
      LOG.info(
          "send: Found \"post-\" arguments..., continuing with an HTTP POST using URL: {}", url);
      for (final NameValuePair post : posts) {
        LOG.debug("send: post argument: {} = {}", post.getValue(), post.getName());
      }
      method = new HttpPost(url);
      try {
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(posts, "UTF-8");
        ((HttpPost) method).setEntity(entity);
      } catch (UnsupportedEncodingException e) {
        // Should never happen
      }
    }

    String contents = null;
    int statusCode = -1;
    try {
      HttpResponse response = client.execute(method);
      statusCode = response.getStatusLine().getStatusCode();
      contents = EntityUtils.toString(response.getEntity());
      LOG.info("send: Contents is: {}", contents);
    } catch (IOException e) {
      LOG.error("send: IO problem with HTTP post/response: {}", e);
      throw new RuntimeException("Problem with HTTP post: " + e.getMessage());
    } finally {
      // Do we need to do any cleanup?
      // method.releaseConnection();
    }

    doSql(contents);

    return statusCode;
  }
 @Override
 protected PocketAuthInfo buildInfo(final Response r) throws AccessTokenException {
   if (r.getStatus() >= 400) {
     throw new AccessTokenException(r.asJson().asText());
   } else {
     final List<NameValuePair> list = URLEncodedUtils.parse(r.getBody(), Charset.forName("UTF-8"));
     final Map<String, String> map = new HashMap<String, String>(list.size());
     for (final NameValuePair nameValuePair : list) {
       map.put(nameValuePair.getName(), nameValuePair.getValue());
     }
     return new PocketAuthInfo(map);
   }
 }