/* GET request */
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    String signingSecret = SageoneConstants.SIGNING_SECRET;
    String accessToken = req.getParameter("access_token");
    String endpoint = "https://api.sageone.com/" + req.getParameter("endpoint");
    String nonce = Nonce.generateNonce();
    String authHeader = "Bearer " + accessToken;
    TreeMap<String, String> params = new TreeMap<String, String>();
    SageoneSigner s = new SageoneSigner("get", endpoint, params, nonce, signingSecret, accessToken);
    String signature = s.signature();

    try {
      HttpURLConnection httpConn = HttpUtility.sendGetRequest(endpoint);
      httpConn.setRequestProperty("Authorization", authHeader);
      httpConn.setRequestProperty("X-Signature", signature);
      httpConn.setRequestProperty("X-Nonce", nonce);
      httpConn.setRequestProperty("Accept", "*/*");
      httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      httpConn.setRequestProperty("User-Agent", "SageOneSampleApp");
      String[] response = HttpUtility.readMultipleLinesResponse();
      JSONObject jsonResponse = new JSONObject(response[0]);
      Gson gson = new GsonBuilder().setPrettyPrinting().create();
      String prettyJson = gson.toJson(jsonResponse);

      resp.getWriter().println("<html>");
      resp.getWriter().println("<head>");
      resp.getWriter()
          .println("<link type=\"text/css\" rel=\"stylesheet\" href=\"sample_app.css\">");
      resp.getWriter()
          .println(
              "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css\" integrity=\"sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7\" crossorigin=\"anonymous\">");
      resp.getWriter()
          .println(
              "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css\" integrity=\"sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r\" crossorigin=\"anonymous\">");
      resp.getWriter()
          .println(
              "<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js\" integrity=\"sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS\" crossorigin=\"anonymous\"></script>");
      resp.getWriter().println("<title>Response</title>");
      resp.getWriter().println("</head>");
      resp.getWriter().println("<body>");
      resp.getWriter().println("<header class=\"navbar navbar-fixed-top navbar-inverse\">");
      resp.getWriter().println("<div class='container'>");
      resp.getWriter()
          .println("<a id=\"logo\" href=\"/SageOneSampleApp\">Sage One API Sample App</a>");
      resp.getWriter().println("</div>");
      resp.getWriter().println("</header>");
      resp.getWriter().println("<div class='container'>");
      resp.getWriter().println("<h1>Sage One Data</h1>");
      resp.getWriter().println("<pre>" + prettyJson + "</pre>");
      resp.getWriter().println("</body>");
      resp.getWriter().println("</html>");
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    HttpUtility.disconnect();
  }
Exemplo n.º 2
0
  // Once collected, you get only one visit from the root tag block (hopefully HTML)
  public void visit(TagBlock tb) {
    // We are not going to parse attribute information in pages that don't have an HTML tag block
    if (tb.startTag.tagName.equalsIgnoreCase("HTML")) {
      HTMLElement he;
      TagBlock tbe;

      // Let's go looking for the head tag
      for (Enumeration be = tb.body.elements(); be.hasMoreElements(); ) {
        he = (HTMLElement) be.nextElement();

        if (he.getClass() == TagBlock.class) {
          tbe = (TagBlock) he;

          if (tbe.startTag.tagName.equalsIgnoreCase("HEAD")) {
            // Found the head tag block, now let's look for the page title and any meta tags
            for (Enumeration hbe = tbe.body.elements(); hbe.hasMoreElements(); ) {
              he = (HTMLElement) hbe.nextElement();

              if (he.getClass() == TagBlock.class) {
                tbe = (TagBlock) he;

                if (tbe.startTag.tagName.equalsIgnoreCase("TITLE")) {
                  // Should only be one item in title body, hopefully it's a text element
                  for (Enumeration e = tbe.body.elements(); e.hasMoreElements(); ) {
                    he = (HTMLElement) e.nextElement();

                    if (he.getClass() == Text.class) {
                      pageTitle = HttpUtility.HtmlDecode(((Text) he).text).Trim();
                      break;
                    }
                  }
                }
              } else if (he.getClass() == Tag.class) {
                Tag tg = (Tag) he;

                // See if this is a META tag
                if (tg.tagName.equalsIgnoreCase("META")) {
                  // Convert this tag and it's attributes into a more .NET
                  // friendly collection
                  MetaTag metaTag = new MetaTag(tg);

                  // Copy attributes in Java vector into .NET ArrayList...
                  for (Enumeration ae = tg.attributeList.attributes.elements();
                      ae.hasMoreElements(); )
                    metaTag.get_Attributes().Add((Attribute) ae.nextElement());

                  tagList.Add(metaTag);
                }
              }
            }

            // Once we've found the head element, we can leave...
            break;
          }
        }
      }
    }
  }