예제 #1
0
  private static JmesPathExpression getAstFromArgument(
      String argument, Map<String, JmesPathExpression> argumentToAstMap) throws IOException {
    if (argument != null && !argumentToAstMap.containsKey(argument)) {

      ProcessBuilder pb =
          new ProcessBuilder(
              "/apollo/env/SDETools/bin/brazil-runtime-exec",
              "python3.4",
              System.getProperty("codeGenSourceDirectory") + "/bin/jp-to-ast.py",
              argument);
      Process p = pb.start();

      JsonNode jsonNode = mapper.readTree(IOUtils.toString(p.getInputStream()));
      JmesPathExpression ast = fromAstJsonToAstJava(jsonNode);

      argumentToAstMap.put(argument, ast);
      IOUtils.closeQuietly(p.getInputStream(), null);

      return ast;

    } else if (argument != null) {
      return argumentToAstMap.get(argument);
    }
    return null;
  }
  @Override
  public AmazonServiceException handle(HttpResponse httpResponse) throws XMLStreamException {

    final InputStream is = httpResponse.getContent();
    String xmlContent = null;
    /*
     * We don't always get an error response body back from S3. When we send
     * a HEAD request, we don't receive a body, so we'll have to just return
     * what we can.
     */
    if (is == null || httpResponse.getRequest().getHttpMethod() == HttpMethodName.HEAD) {
      return createExceptionFromHeaders(httpResponse, null);
    }

    String content = null;
    try {
      content = IOUtils.toString(is);
    } catch (IOException ioe) {
      if (log.isDebugEnabled()) log.debug("Failed in parsing the error response : ", ioe);
      return createExceptionFromHeaders(httpResponse, null);
    }

    /*
     * XMLInputFactory is not thread safe and hence it is synchronized.
     * Reference :
     * http://itdoc.hitachi.co.jp/manuals/3020/30203Y2210e/EY220140.HTM
     */
    XMLStreamReader reader;
    synchronized (xmlInputFactory) {
      reader =
          xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(content.getBytes(UTF8)));
    }

    try {
      /*
       * target depth is to determine if the XML Error response from the
       * server has any element inside <Error> tag have child tags.
       * Parsing such tags is not supported now. target depth is
       * incremented for every start tag and decremented after every end
       * tag is encountered.
       */
      int targetDepth = 0;
      final AmazonS3ExceptionBuilder exceptionBuilder = new AmazonS3ExceptionBuilder();
      exceptionBuilder.setErrorResponseXml(content);
      exceptionBuilder.setStatusCode(httpResponse.getStatusCode());

      boolean hasErrorTagVisited = false;
      while (reader.hasNext()) {
        int event = reader.next();

        switch (event) {
          case XMLStreamConstants.START_ELEMENT:
            targetDepth++;
            String tagName = reader.getLocalName();
            if (targetDepth == 1 && !S3ErrorTags.Error.toString().equals(tagName))
              return createExceptionFromHeaders(
                  httpResponse,
                  "Unable to parse error response. Error XML Not in proper format." + content);
            if (S3ErrorTags.Error.toString().equals(tagName)) {
              hasErrorTagVisited = true;
            }
            continue;
          case XMLStreamConstants.CHARACTERS:
            xmlContent = reader.getText();
            if (xmlContent != null) xmlContent = xmlContent.trim();
            continue;
          case XMLStreamConstants.END_ELEMENT:
            tagName = reader.getLocalName();
            targetDepth--;
            if (!(hasErrorTagVisited) || targetDepth > 1) {
              return createExceptionFromHeaders(
                  httpResponse,
                  "Unable to parse error response. Error XML Not in proper format." + content);
            }
            if (S3ErrorTags.Message.toString().equals(tagName)) {
              exceptionBuilder.setErrorMessage(xmlContent);
            } else if (S3ErrorTags.Code.toString().equals(tagName)) {
              exceptionBuilder.setErrorCode(xmlContent);
            } else if (S3ErrorTags.RequestId.toString().equals(tagName)) {
              exceptionBuilder.setRequestId(xmlContent);
            } else if (S3ErrorTags.HostId.toString().equals(tagName)) {
              exceptionBuilder.setExtendedRequestId(xmlContent);
            } else {
              exceptionBuilder.addAdditionalDetail(tagName, xmlContent);
            }
            continue;
          case XMLStreamConstants.END_DOCUMENT:
            return exceptionBuilder.build();
        }
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) log.debug("Failed in parsing the error response : " + content, e);
    }
    return createExceptionFromHeaders(httpResponse, content);
  }