protected String getRallyXML(String apiUrl) throws Exception {
    String responseXML = "";

    DefaultHttpClient httpClient = new DefaultHttpClient();

    Base64 base64 = new Base64();
    String encodeString =
        new String(base64.encode((rallyApiHttpUsername + ":" + rallyApiHttpPassword).getBytes()));

    HttpGet httpGet = new HttpGet(apiUrl);
    httpGet.addHeader("Authorization", "Basic " + encodeString);
    HttpResponse response = httpClient.execute(httpGet);
    HttpEntity entity = response.getEntity();

    if (entity != null) {

      InputStreamReader reader = new InputStreamReader(entity.getContent());
      BufferedReader br = new BufferedReader(reader);

      StringBuilder sb = new StringBuilder();
      String line = "";
      while ((line = br.readLine()) != null) {
        sb.append(line);
      }

      responseXML = sb.toString();
    }

    log.debug("responseXML=" + responseXML);

    return responseXML;
  }
Beispiel #2
0
 private boolean fetchApp(String url, String username, String password) throws JSONException {
   try {
     if (username == "null") {
       username = null;
     }
     if (password == "null") {
       password = null;
     }
     HttpResponse response = makeRequest(url, username, password);
     StatusLine sl = response.getStatusLine();
     int code = sl.getStatusCode();
     HttpEntity entity = response.getEntity();
     InputStream content = entity.getContent();
     if (code != 200) {
       return false;
     } else {
       ZipInputStream data = new ZipInputStream(content);
       return saveAndVerify(data);
     }
   } catch (ClientProtocolException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     return false;
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     return false;
   }
 }
 public Result<InputStream> getInputStream(String endpoint) throws IOException {
   HttpClient cli = createClient();
   HttpGet get = createGet(endpoint);
   HttpResponse res = cli.execute(get);
   Object result = null;
   if (res.getStatusLine().getStatusCode() == 200) result = res.getEntity().getContent();
   else {
     RestResponse resp = new RestResponse(res);
     result = unmarshalPOJO(resp, InputStream.class);
     cli.getConnectionManager().shutdown();
   }
   return new Result<InputStream>(result);
 }
Beispiel #4
0
    /**
     * Parse the room read response received from reservation backend.
     *
     * <p>This will request the retry if the response status code differs from 200, 201 or 4xx.
     *
     * @param response The Apache HTTP client response
     * @throws IOException
     * @throws ParseException
     * @throws FaultTolerantRESTRequest.RetryRequestedException
     */
    @Override
    protected void parse(HttpResponse response)
        throws IOException, ParseException, FaultTolerantRESTRequest.RetryRequestedException {

      int statusCode = response.getStatusLine().getStatusCode();

      logger.info("STATUS CODE: " + statusCode);

      if (200 == statusCode || 201 == statusCode) {
        // OK
        conference = readConferenceResponse(conference, response);

        result = new ApiResult(statusCode, conference);
      } else if ((statusCode >= 400) && (statusCode < 500)) {
        // Client side error, indicates that the reservation
        // backend do not want the resubmission
        ErrorResponse error = readErrorResponse(response);

        result = new ApiResult(statusCode, error);

      } else {

        // Unusual status code, request the retry
        throw new FaultTolerantRESTRequest.RetryRequestedException();
      }
    }
 private void showUpdatesStatus(Future<HttpResponse> status) {
   String versionMsg = "<h3 align=center>Cannot get version information </h3>";
   try {
     HttpResponse response = status.get();
     updateStatusLabel.setText("");
     String file = null;
     for (Header header : response.getHeaders("Content-Disposition")) {
       Matcher matcher = Pattern.compile("filename=\"([^\"]+)\"").matcher(header.getValue());
       while (matcher.find()) {
         file = matcher.group(1);
       }
     }
     if (file != null) {
       String[] installerPatterns = {
         "PdfMetadataEditor-(\\d+)\\.(\\d+)\\.(\\d+)-installer.jar",
         "pdf-metadata-edit-(\\d+)\\.(\\d+)\\.(\\d+)-installer.jar",
       };
       Version.VersionTuple current = Version.get();
       Version.VersionTuple latest = null;
       for (String pattern : installerPatterns) {
         latest = new Version.VersionTuple(file, pattern);
         if (latest.parseSuccess) {
           break;
         }
       }
       if (current.cmp(latest) < 0) {
         versionMsg =
             "<h3 align=center>New version available: <a href='http://broken-by.me/pdf-metadata-editor/#download'>"
                 + latest.getAsString()
                 + "</a> , current: "
                 + current.getAsString()
                 + "</h3>";
         updateStatusLabel.setText("Newer version available:" + latest.getAsString());
       } else {
         versionMsg =
             "<h3 align=center>Version " + current.getAsString() + " is the latest version</h3>";
       }
     }
   } catch (InterruptedException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
   } catch (ExecutionException e1) {
     versionMsg += "<h4 align=center>Error: " + e1.getCause().getLocalizedMessage() + "</h4>";
   } finally {
     txtpnDf.setText(aboutMsg + versionMsg);
   }
 }
Beispiel #6
0
  /**
   * Parses error response.
   *
   * @param response parsed <tt>ErrorResponse</tt>
   * @return <tt>ErrorResponse</tt> parsed from HTTP content stream.
   * @throws IOException if any IO issues occur.
   * @throws ParseException if any issues with JSON parsing occur.
   */
  private ErrorResponse readErrorResponse(HttpResponse response)
      throws IOException, ParseException {
    BufferedReader rd =
        new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    jsonParser.parse(rd, errorJson);

    return errorJson.getResult();
  }
 /**
  * Returns the lock token, which must be retained to unlock the resource
  *
  * @param uri - must be encoded
  * @param owner
  * @return
  * @throws com.ettrema.httpclient.HttpException
  */
 public synchronized String doLock(String uri)
     throws com.ettrema.httpclient.HttpException, NotAuthorizedException, ConflictException,
         BadRequestException, NotFoundException, URISyntaxException {
   notifyStartRequest();
   LockMethod p = new LockMethod(uri);
   try {
     String lockXml = LOCK_XML.replace("${owner}", user);
     HttpEntity requestEntity = new StringEntity(lockXml, "UTF-8");
     p.setEntity(requestEntity);
     HttpResponse resp = host().client.execute(p);
     int result = resp.getStatusLine().getStatusCode();
     Utils.processResultCode(result, uri);
     return p.getLockToken(resp);
   } catch (IOException ex) {
     throw new RuntimeException(ex);
   } finally {
     notifyFinishRequest();
   }
 }
Beispiel #8
0
  public String getRequest(String url, String queryString) {
    String responseText = "";

    try {
      httpGet.setURI(new URI(url + "?" + queryString));
      httpGet.addHeader("Accept", "application/json");

      HttpResponse response = httpClient.execute(httpGet);
      responseText = EntityUtils.toString(response.getEntity());

    } catch (ClientProtocolException e) {

    } catch (IOException e) {

    } catch (URISyntaxException e1) {
    }

    return responseText;
  }
 /**
  * @param newUri - must be fully qualified and correctly encoded
  * @return
  * @throws com.ettrema.httpclient.HttpException
  */
 public synchronized int doMkCol(String newUri)
     throws com.ettrema.httpclient.HttpException, NotAuthorizedException, ConflictException,
         BadRequestException, NotFoundException, URISyntaxException {
   notifyStartRequest();
   MkColMethod p = new MkColMethod(newUri);
   try {
     HttpResponse resp = host().client.execute(p);
     int result = resp.getStatusLine().getStatusCode();
     if (result == 409) {
       // probably means the folder already exists
       return result;
     }
     Utils.processResultCode(result, newUri);
     return result;
   } catch (IOException ex) {
     throw new RuntimeException(ex);
   } finally {
     notifyFinishRequest();
   }
 }
Beispiel #10
0
  public String postRequest(String url, List<NameValuePair> entity) {
    String responseText = "";

    try {
      httpPost.setURI(new URI(url));
      httpPost.addHeader("Accept", "application/json");
      httpPost.setEntity(new UrlEncodedFormEntity(entity));

      HttpResponse response = httpClient.execute(httpPost);
      responseText = EntityUtils.toString(response.getEntity());

    } catch (ClientProtocolException e) {

    } catch (IOException e) {

    } catch (URISyntaxException e1) {
    }

    return responseText;
  }
Beispiel #11
0
  protected String postRallyXML(String apiUrl, String requestXML) throws Exception {
    String responseXML = "";

    DefaultHttpClient httpClient = new DefaultHttpClient();

    Base64 base64 = new Base64();
    String encodeString =
        new String(base64.encode((rallyApiHttpUsername + ":" + rallyApiHttpPassword).getBytes()));

    HttpPost httpPost = new HttpPost(apiUrl);
    httpPost.addHeader("Authorization", "Basic " + encodeString);

    httpPost.setEntity(new StringEntity(requestXML));

    HttpResponse response = httpClient.execute(httpPost);
    HttpEntity entity = response.getEntity();

    responseXML = getEntityString(entity);

    return responseXML;
  }
Beispiel #12
0
  /**
   * Parses JSON string returned in HTTP response and converts it to <tt>Conference</tt> instance.
   *
   * @param conference <tt>Conference</tt> instance that contains the data returned by API endpoint.
   * @param response HTTP response returned by the API endpoint.
   * @return <tt>Conference</tt> instance that contains the data returned by API endpoint.
   * @throws IOException if any IO problems occur.
   * @throws ParseException if any problems with JSON parsing occur.
   */
  private Conference readConferenceResponse(Conference conference, HttpResponse response)
      throws IOException, ParseException {
    BufferedReader rd =
        new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    if (conference != null) {
      conferenceJson.setForUpdate(conference);
    }

    jsonParser.parse(rd, conferenceJson);

    if (conference == null) {
      conference = conferenceJson.getResult();
    }

    logger.info("ID: " + conference.getId());
    logger.info("PIN: " + conference.getPin());
    logger.info("URL: " + conference.getUrl());
    logger.info("SIP ID: " + conference.getSipId());
    logger.info("START TIME: " + conference.getStartTime());
    logger.info("DURATION: " + conference.getDuration());

    return conference;
  }