Example #1
0
  public static byte[] convertToJSON(
      IExtensionHelpers helpers, IHttpRequestResponse requestResponse) {

    byte[] request = requestResponse.getRequest();

    if (Objects.equals(helpers.analyzeRequest(request).getMethod(), "GET")) {
      request = helpers.toggleRequestMethod(request);
    }

    IRequestInfo requestInfo = helpers.analyzeRequest(request);

    int bodyOffset = requestInfo.getBodyOffset();

    byte content_type = requestInfo.getContentType();

    String body = new String(request, bodyOffset, request.length - bodyOffset);

    String json = "";

    Boolean success = true;

    try {
      if (content_type == 3) {
        JSONObject xmlJSONObject = XML.toJSONObject(body);
        json = xmlJSONObject.toString(2);
      } else if (content_type == 0 || content_type == 1) {
        Map<String, String> params = splitQuery(body);
        Gson gson = new Gson();
        json = gson.toJson(params);
      } else {
        json = body;
      }
    } catch (Exception e) {
      success = false;
    }

    if (!success) {
      return request;
    } else {

      List<String> headers;

      headers = helpers.analyzeRequest(request).getHeaders();

      Iterator<String> iter = headers.iterator();
      while (iter.hasNext()) {
        if (iter.next().contains("Content-Type")) iter.remove();
      }

      headers.add("Content-Type: application/json;charset=UTF-8");

      return helpers.buildHttpMessage(headers, json.getBytes());
    }
  }
Example #2
0
  public static byte[] convertToXML(IExtensionHelpers helpers, IHttpRequestResponse requestResponse)
      throws Exception {

    byte[] request = requestResponse.getRequest();

    if (Objects.equals(helpers.analyzeRequest(request).getMethod(), "GET")) {
      request = helpers.toggleRequestMethod(request);
    }

    IRequestInfo requestInfo = helpers.analyzeRequest(request);

    int bodyOffset = requestInfo.getBodyOffset();

    byte content_type = requestInfo.getContentType();

    String body = new String(request, bodyOffset, request.length - bodyOffset, "UTF-8");

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc =
        new Document() {
          public String getNodeName() {
            return null;
          }

          public String getNodeValue() throws DOMException {
            return null;
          }

          public void setNodeValue(String nodeValue) throws DOMException {}

          public short getNodeType() {
            return 0;
          }

          public Node getParentNode() {
            return null;
          }

          public NodeList getChildNodes() {
            return null;
          }

          public Node getFirstChild() {
            return null;
          }

          public Node getLastChild() {
            return null;
          }

          public Node getPreviousSibling() {
            return null;
          }

          public Node getNextSibling() {
            return null;
          }

          public NamedNodeMap getAttributes() {
            return null;
          }

          public Document getOwnerDocument() {
            return null;
          }

          public Node insertBefore(Node newChild, Node refChild) throws DOMException {
            return null;
          }

          public Node replaceChild(Node newChild, Node oldChild) throws DOMException {
            return null;
          }

          public Node removeChild(Node oldChild) throws DOMException {
            return null;
          }

          public Node appendChild(Node newChild) throws DOMException {
            return null;
          }

          public boolean hasChildNodes() {
            return false;
          }

          public Node cloneNode(boolean deep) {
            return null;
          }

          public void normalize() {}

          public boolean isSupported(String feature, String version) {
            return false;
          }

          public String getNamespaceURI() {
            return null;
          }

          public String getPrefix() {
            return null;
          }

          public void setPrefix(String prefix) throws DOMException {}

          public String getLocalName() {
            return null;
          }

          public boolean hasAttributes() {
            return false;
          }

          public String getBaseURI() {
            return null;
          }

          public short compareDocumentPosition(Node other) throws DOMException {
            return 0;
          }

          public String getTextContent() throws DOMException {
            return null;
          }

          public void setTextContent(String textContent) throws DOMException {}

          public boolean isSameNode(Node other) {
            return false;
          }

          public String lookupPrefix(String namespaceURI) {
            return null;
          }

          public boolean isDefaultNamespace(String namespaceURI) {
            return false;
          }

          public String lookupNamespaceURI(String prefix) {
            return null;
          }

          public boolean isEqualNode(Node arg) {
            return false;
          }

          public Object getFeature(String feature, String version) {
            return null;
          }

          public Object setUserData(String key, Object data, UserDataHandler handler) {
            return null;
          }

          public Object getUserData(String key) {
            return null;
          }

          public DocumentType getDoctype() {
            return null;
          }

          public DOMImplementation getImplementation() {
            return null;
          }

          public Element getDocumentElement() {
            return null;
          }

          public Element createElement(String tagName) throws DOMException {
            return null;
          }

          public DocumentFragment createDocumentFragment() {
            return null;
          }

          public Text createTextNode(String data) {
            return null;
          }

          public Comment createComment(String data) {
            return null;
          }

          public CDATASection createCDATASection(String data) throws DOMException {
            return null;
          }

          public ProcessingInstruction createProcessingInstruction(String target, String data)
              throws DOMException {
            return null;
          }

          public Attr createAttribute(String name) throws DOMException {
            return null;
          }

          public EntityReference createEntityReference(String name) throws DOMException {
            return null;
          }

          public NodeList getElementsByTagName(String tagname) {
            return null;
          }

          public Node importNode(Node importedNode, boolean deep) throws DOMException {
            return null;
          }

          public Element createElementNS(String namespaceURI, String qualifiedName)
              throws DOMException {
            return null;
          }

          public Attr createAttributeNS(String namespaceURI, String qualifiedName)
              throws DOMException {
            return null;
          }

          public NodeList getElementsByTagNameNS(String namespaceURI, String localName) {
            return null;
          }

          public Element getElementById(String elementId) {
            return null;
          }

          public String getInputEncoding() {
            return null;
          }

          public String getXmlEncoding() {
            return null;
          }

          public boolean getXmlStandalone() {
            return false;
          }

          public void setXmlStandalone(boolean xmlStandalone) throws DOMException {}

          public String getXmlVersion() {
            return null;
          }

          public void setXmlVersion(String xmlVersion) throws DOMException {}

          public boolean getStrictErrorChecking() {
            return false;
          }

          public void setStrictErrorChecking(boolean strictErrorChecking) {}

          public String getDocumentURI() {
            return null;
          }

          public void setDocumentURI(String documentURI) {}

          public Node adoptNode(Node source) throws DOMException {
            return null;
          }

          public DOMConfiguration getDomConfig() {
            return null;
          }

          public void normalizeDocument() {}

          public Node renameNode(Node n, String namespaceURI, String qualifiedName)
              throws DOMException {
            return null;
          }
        };

    StringBuilder xml = new StringBuilder();
    xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
    xml.append("<root>");

    if (content_type == 0 || content_type == 1) {

      Map<String, String> params = splitQuery(body);
      Gson gson = new Gson();
      body = gson.toJson(params);
    }

    Boolean success = true;

    try {
      JSONObject json = new JSONObject(body);

      xml.append(XML.toString(json));
      xml.append("</root>");

      DocumentBuilder builder = factory.newDocumentBuilder();

      ByteArrayInputStream input = new ByteArrayInputStream(xml.toString().getBytes("UTF-8"));
      doc = builder.parse(input);

    } catch (Exception e) {
      success = false;
    }

    if (!success) {
      return null;
    } else {

      List<String> headers;

      headers = helpers.analyzeRequest(request).getHeaders();

      Iterator<String> iter = headers.iterator();
      while (iter.hasNext()) {
        if (iter.next().contains("Content-Type")) iter.remove();
      }

      headers.add("Content-Type: application/xml;charset=UTF-8");

      return helpers.buildHttpMessage(headers, prettyPrint(doc).getBytes());
    }
  }
 public void generateSubAwardLineItems(BudgetSubAwards subAward, Budget budget) {
   BudgetDecimal amountChargeFA = new BudgetDecimal(25000);
   String directLtCostElement =
       getParameterService()
           .getParameterValueAsString(
               BudgetDocument.class, Constants.SUBCONTRACTOR_DIRECT_LT_25K_PARAM);
   String directGtCostElement =
       getParameterService()
           .getParameterValueAsString(
               BudgetDocument.class, Constants.SUBCONTRACTOR_DIRECT_GT_25K_PARAM);
   String inDirectLtCostElement =
       getParameterService()
           .getParameterValueAsString(
               BudgetDocument.class, Constants.SUBCONTRACTOR_F_AND_A_LT_25K_PARAM);
   String inDirectGtCostElement =
       getParameterService()
           .getParameterValueAsString(
               BudgetDocument.class, Constants.SUBCONTRACTOR_F_AND_A_GT_25K_PARAM);
   for (BudgetSubAwardPeriodDetail detail : subAward.getBudgetSubAwardPeriodDetails()) {
     BudgetPeriod budgetPeriod = findBudgetPeriod(detail, budget);
     List<BudgetLineItem> currentLineItems =
         findSubAwardLineItems(budgetPeriod, subAward.getSubAwardNumber());
     // zero out existing line items before recalculating
     for (BudgetLineItem item : currentLineItems) {
       item.setDirectCost(BudgetDecimal.ZERO);
       item.setCostSharingAmount(BudgetDecimal.ZERO);
       item.setSubAwardNumber(subAward.getSubAwardNumber());
       item.setLineItemDescription(subAward.getOrganizationName());
     }
     if (BudgetDecimal.returnZeroIfNull(detail.getDirectCost()).isNonZero()
         || hasBeenChanged(detail, true)) {
       BudgetDecimal ltValue = lesserValue(detail.getDirectCost(), amountChargeFA);
       BudgetDecimal gtValue = detail.getDirectCost().subtract(ltValue);
       if (ltValue.isNonZero()) {
         BudgetLineItem lt =
             findOrCreateLineItem(
                 currentLineItems, detail, subAward, budgetPeriod, directLtCostElement);
         lt.setLineItemCost(ltValue);
       }
       if (gtValue.isNonZero()) {
         BudgetLineItem gt =
             findOrCreateLineItem(
                 currentLineItems, detail, subAward, budgetPeriod, directGtCostElement);
         gt.setLineItemCost(gtValue);
       }
       amountChargeFA = amountChargeFA.subtract(ltValue);
     }
     if (BudgetDecimal.returnZeroIfNull(detail.getIndirectCost()).isNonZero()
         || hasBeenChanged(detail, false)) {
       BudgetDecimal ltValue = lesserValue(detail.getIndirectCost(), amountChargeFA);
       BudgetDecimal gtValue = detail.getIndirectCost().subtract(ltValue);
       if (ltValue.isNonZero()) {
         BudgetLineItem lt =
             findOrCreateLineItem(
                 currentLineItems, detail, subAward, budgetPeriod, inDirectLtCostElement);
         lt.setLineItemCost(ltValue);
       }
       if (gtValue.isNonZero()) {
         BudgetLineItem gt =
             findOrCreateLineItem(
                 currentLineItems, detail, subAward, budgetPeriod, inDirectGtCostElement);
         gt.setLineItemCost(gtValue);
       }
       amountChargeFA = amountChargeFA.subtract(ltValue);
     }
     Collections.sort(
         currentLineItems,
         new Comparator<BudgetLineItem>() {
           public int compare(BudgetLineItem arg0, BudgetLineItem arg1) {
             return arg0.getLineItemNumber().compareTo(arg1.getLineItemNumber());
           }
         });
     Iterator<BudgetLineItem> iter = currentLineItems.iterator();
     while (iter.hasNext()) {
       BudgetLineItem lineItem = iter.next();
       if (BudgetDecimal.returnZeroIfNull(lineItem.getLineItemCost()).isZero()) {
         budgetPeriod.getBudgetLineItems().remove(lineItem);
         iter.remove();
       } else {
         if (!budgetPeriod.getBudgetLineItems().contains(lineItem)) {
           budgetPeriod.getBudgetLineItems().add(lineItem);
         }
       }
     }
     if (!currentLineItems.isEmpty()
         && BudgetDecimal.returnZeroIfNull(detail.getCostShare()).isNonZero()) {
       currentLineItems.get(0).setCostSharingAmount(detail.getCostShare());
     }
   }
 }