Example #1
0
 /** Close the session. This implies closing any open methods. */
 public synchronized void close() {
   if (this.closed) return; // multiple calls ok
   while (methodList.size() > 0) {
     HTTPMethod m = methodList.get(0);
     m.close(); // forcibly close; will invoke removemethod().
   }
   closed = true;
 }
Example #2
0
 static String getUrlAsString(String url) throws HTTPException {
   try (HTTPMethod m = HTTPFactory.Get(url); ) {
     int status = m.execute();
     String content = null;
     if (status == 200) {
       content = m.getResponseAsString();
     }
     return content;
   }
 }
Example #3
0
 static int putUrlAsString(String content, String url) throws HTTPException {
   int status = 0;
   try {
     try (HTTPMethod m = HTTPFactory.Put(url)) {
       m.setRequestContent(
           new StringEntity(content, ContentType.create("application/text", "UTF-8")));
       status = m.execute();
     }
   } catch (UnsupportedCharsetException uce) {
     throw new HTTPException(uce);
   }
   return status;
 }
Example #4
0
  public Either<IOException, HttpResponse> execUploadRequest(
      String path, List<FormBodyPart> parts, HTTPMethod method, String acceptType) {
    HttpEntityEnclosingRequestBase httpReq = null;
    String url = getUriForPath(path);
    if (method.equals(HTTPMethod.POST)) {
      httpReq = new HttpPost(url);
    } else if (method.equals(HTTPMethod.PUT)) {
      httpReq = new HttpPut(url);
    } else {
      throw new IllegalArgumentException("gotta be this or that: post or put");
    }
    MultipartEntity entity = new MultipartEntity();
    for (FormBodyPart part : parts) {
      entity.addPart(part);
    }
    httpReq.setEntity(entity);

    return execRequest(httpReq, acceptType);
  }
Example #5
0
  public Either<IOException, HttpResponse> execNormalRequest(
      String path, Map<String, String> params, HTTPMethod method, String acceptType) {
    String url = getUriForPath(path);
    if (params == null) {
      params = new HashMap<String, String>();
    }
    HttpUriRequest httpReq = null;
    if (method.equals(HTTPMethod.GET)) {
      HttpGet get = new HttpGet(url);
      for (Map.Entry<String, String> param : params.entrySet()) {
        get.getParams().setParameter(param.getKey(), param.getValue());
      }
      httpReq = get;
    } else {
      HttpEntityEnclosingRequestBase postOrPut = null;
      if (method.equals(HTTPMethod.PUT)) {
        postOrPut = new HttpPut(url);
      } else {
        postOrPut = new HttpPost(url);
      }
      MultipartEntity entity = new MultipartEntity();

      List<NameValuePair> args = new ArrayList<NameValuePair>();
      for (Map.Entry<String, String> param : params.entrySet()) {
        args.add(new BasicNameValuePair(param.getKey(), param.getValue()));
      }
      try {
        postOrPut.setEntity(new UrlEncodedFormEntity(args));
      } catch (UnsupportedEncodingException e) {
        return new Left<IOException, HttpResponse>(e);
      }
      httpReq = postOrPut;
    }

    return execRequest(httpReq, acceptType);
  }