Example #1
0
  protected String getRallyAPIError(String responseXML) {
    String errorMsg = "";

    try {

      org.jdom.input.SAXBuilder bSAX = new org.jdom.input.SAXBuilder();
      org.jdom.Document doc = bSAX.build(new StringReader(responseXML));
      Element root = doc.getRootElement();

      XPath xpath = XPath.newInstance("//Errors");
      List xlist = xpath.selectNodes(root);

      Iterator iter = xlist.iterator();
      while (iter.hasNext()) {

        Element item = (Element) iter.next();
        errorMsg = item.getChildText("OperationResultError");
      }

    } catch (Exception e) {
      errorMsg = e.toString();
    }

    return errorMsg;
  }
Example #2
0
  public List parsePage(String pageCode) {
    List sections = new ArrayList();
    List folders = new ArrayList();
    List files = new ArrayList();
    int start = pageCode.indexOf("<div id=\"list-view\" class=\"view\"");
    int end = pageCode.indexOf("<div id=\"gallery-view\" class=\"view\"");
    String usefulSection = "";
    if (start != -1 && end != -1) {
      usefulSection = pageCode.substring(start, end);
    } else {
      debug("Could not parse page");
    }
    try {
      DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      InputSource is = new InputSource();
      is.setCharacterStream(new StringReader(usefulSection));
      Document doc = db.parse(is);

      NodeList divs = doc.getElementsByTagName("div");
      for (int i = 0; i < divs.getLength(); i++) {
        Element div = (Element) divs.item(i);
        boolean isFolder = false;
        if (div.getAttribute("class").equals("filename")) {
          NodeList imgs = div.getElementsByTagName("img");
          for (int j = 0; j < imgs.getLength(); j++) {
            Element img = (Element) imgs.item(j);
            if (img.getAttribute("class").indexOf("folder") > 0) {
              isFolder = true;
            } else {
              isFolder = false; // it's a file
            }
          }

          NodeList anchors = div.getElementsByTagName("a");
          Element anchor = (Element) anchors.item(0);
          String attr = anchor.getAttribute("href");
          String fileName = anchor.getAttribute("title");
          String fileURL;
          if (isFolder && !attr.equals("#")) {
            folders.add(attr);
            folders.add(fileName);
          } else if (!isFolder && !attr.equals("#")) {
            // Dropbox uses ajax to get the file for download, so the url isn't enough. We must be
            // sneaky here.
            fileURL = "https://dl.dropbox.com" + attr.substring(23) + "?dl=1";
            files.add(fileURL);
            files.add(fileName);
          }
        }
      }
    } catch (Exception e) {
      debug(e.toString());
    }

    sections.add(files);
    sections.add(folders);

    return sections;
  }
Example #3
0
 public String getData(String url) {
   HttpClient client = new DefaultHttpClient();
   HttpGet get = new HttpGet(url);
   String response = "";
   try {
     ResponseHandler<String> responseHandler = new BasicResponseHandler();
     response = client.execute(get, responseHandler);
   } catch (Exception e) {
     debug(e.toString());
   }
   return response;
 }
Example #4
0
  public void downloadFile(String url, String savePath, String fileName) {

    try {
      URL theURL = new URL(url);
      InputStream input = theURL.openStream();
      OutputStream output = new FileOutputStream(new File(savePath, fileName));
      try {
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
          output.write(buffer, 0, bytesRead);
        }
      } catch (Exception e) {
        debug(e.toString());
      } finally {
        output.close();
      }
    } catch (Exception e) {
      debug(e.toString());
    }
  }
Example #5
0
 public String getFolderName(String pageCode) {
   String usefulSection =
       pageCode.substring(
           pageCode.indexOf("<h3 id=\"breadcrumb\">"),
           pageCode.indexOf("<div id=\"list-view\" class=\"view\""));
   String folderName;
   try {
     DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
     InputSource is = new InputSource();
     is.setCharacterStream(new StringReader(usefulSection));
     Document doc = db.parse(is);
     NodeList divs = doc.getElementsByTagName("h3");
     for (int i = 0; i < divs.getLength(); i++) {
       Element div = (Element) divs.item(i);
       String a = div.getTextContent();
       folderName = a.substring(a.indexOf("/>") + 2).trim();
       return folderName;
     }
   } catch (Exception e) {
     debug(e.toString());
   }
   return "Error!";
 }
  private <T> T executeRequest(final HttpUriRequest request, final ResponseHandler<T> handler)
      throws IOException {
    final CloseableHttpClient client = createClientInstance();
    try {
      final CloseableHttpResponse response = client.execute(request);
      //  Wrap the response in a buffer to facilitate error handlers re-playing the content if the
      // response
      //  size is smaller than the max allowable buffer
      if (response.getEntity().getContentLength() >= 0
          && response.getEntity().getContentLength() < config.getMaxBufferSize()) {
        EntityUtils.updateEntity(response, new BufferedHttpEntity(response.getEntity()));
      }

      //  Explicit check for the authorization status of the API key
      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
        throw new HyppoAuthException(config);
      }

      try {
        log.debug(
            "{} - {} : {}",
            request.getMethod(),
            request.getURI().getPath(),
            response.getStatusLine().getStatusCode());
        return handler.handleResponse(response);
      } finally {
        IOUtils.closeQuietly(response);
      }
    } catch (Exception e) {
      log.error(
          "{} - {} : FAILED - {}", request.getMethod(), request.getURI().getPath(), e.toString());
      throw e;
    } finally {
      IOUtils.closeQuietly(client);
    }
  }