Пример #1
0
  /** @see com.amazonaws.http.HttpResponseHandler#handle(com.amazonaws.http.HttpResponse) */
  public AmazonWebServiceResponse<T> handle(HttpResponse response) throws Exception {
    log.trace("Parsing service response JSON");

    String CRC32Checksum = response.getHeaders().get("x-amz-crc32");
    CRC32ChecksumCalculatingInputStream crc32ChecksumInputStream = null;

    JsonParser jsonParser = null;

    if (!needsConnectionLeftOpen) {
      if (CRC32Checksum != null) {
        crc32ChecksumInputStream = new CRC32ChecksumCalculatingInputStream(response.getContent());
        jsonParser = jsonFactory.createParser(crc32ChecksumInputStream);
      } else {
        jsonParser = jsonFactory.createParser(response.getContent());
      }
    }

    try {
      AmazonWebServiceResponse<T> awsResponse = new AmazonWebServiceResponse<T>();
      JsonUnmarshallerContext unmarshallerContext =
          new JsonUnmarshallerContext(jsonParser, response);
      registerAdditionalMetadataExpressions(unmarshallerContext);

      T result = responseUnmarshaller.unmarshall(unmarshallerContext);

      if (CRC32Checksum != null) {
        long serverSideCRC = Long.parseLong(CRC32Checksum);
        long clientSideCRC = crc32ChecksumInputStream.getCRC32Checksum();
        if (clientSideCRC != serverSideCRC) {
          throw new CRC32MismatchException(
              "Client calculated crc32 checksum didn't match that calculated by server side");
        }
      }

      awsResponse.setResult(result);

      Map<String, String> metadata = unmarshallerContext.getMetadata();
      metadata.put(ResponseMetadata.AWS_REQUEST_ID, response.getHeaders().get("x-amzn-RequestId"));
      awsResponse.setResponseMetadata(new ResponseMetadata(metadata));

      log.trace("Done parsing service response");
      return awsResponse;
    } finally {
      if (!needsConnectionLeftOpen) {
        try {
          jsonParser.close();
        } catch (IOException e) {
          log.warn("Error closing json parser", e);
        }
      }
    }
  }
    public static JsonErrorResponse fromResponse(HttpResponse response) throws IOException {
      int statusCode = response.getStatusCode();

      // parse error body
      Map<String, String> map =
          JsonUtils.jsonToMap(new BufferedReader(new InputStreamReader(response.getContent())));

      /*
       * Services using AWS JSON 1.1 protocol with HTTP binding send the
       * error type information in the response headers, instead of the
       * content.
       */
      String errorCode = response.getHeaders().get(X_AMZN_ERROR_TYPE);
      if (errorCode != null) {
        int separator = errorCode.indexOf(':');
        if (separator != -1) {
          errorCode = errorCode.substring(0, separator);
        }
      } else if (map.containsKey("__type")) {
        // check body otherwise
        String type = map.get("__type");
        int separator = type.lastIndexOf("#");
        errorCode = type.substring(separator + 1);
      }

      return new JsonErrorResponse(statusCode, errorCode, map);
    }
  /** @see com.amazonaws.http.HttpResponseHandler#handle(com.amazonaws.http.HttpResponse) */
  public AmazonWebServiceResponse<T> handle(HttpResponse response) throws Exception {
    log.trace("Parsing service response JSON");
    JsonParser jsonParser = jsonFactory.createJsonParser(response.getContent());
    try {
      AmazonWebServiceResponse<T> awsResponse = new AmazonWebServiceResponse<T>();
      JsonUnmarshallerContext unmarshallerContext = new JsonUnmarshallerContext(jsonParser);

      // TODO: Anything else to do for JSON metadata?
      unmarshallerContext.registerMetadataExpression(
          "ResponseMetadata/RequestId", 2, ResponseMetadata.AWS_REQUEST_ID);
      unmarshallerContext.registerMetadataExpression(
          "requestId", 2, ResponseMetadata.AWS_REQUEST_ID);
      registerAdditionalMetadataExpressions(unmarshallerContext);

      T result = responseUnmarshaller.unmarshall(unmarshallerContext);
      awsResponse.setResult(result);

      Map<String, String> metadata = unmarshallerContext.getMetadata();
      awsResponse.setResponseMetadata(new ResponseMetadata(metadata));

      log.trace("Done parsing service response");
      return awsResponse;
    } finally {
      try {
        jsonParser.close();
      } catch (Exception e) {
      }
    }
  }
  /**
   * Handles a successful response from a service call by unmarshalling the results using the
   * specified response handler.
   *
   * @param <T> The type of object expected in the response.
   * @param request The original request that generated the response being handled.
   * @param responseHandler The response unmarshaller used to interpret the contents of the
   *     response.
   * @param method The HTTP method that was invoked, and contains the contents of the response.
   * @param executionContext Extra state information about the request currently being executed.
   * @return The contents of the response, unmarshalled using the specified response handler.
   * @throws IOException If any problems were encountered reading the response contents from the
   *     HTTP method object.
   */
  private <T> T handleResponse(
      Request<?> request,
      HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler,
      HttpRequestBase method,
      org.apache.http.HttpResponse apacheHttpResponse,
      ExecutionContext executionContext)
      throws IOException {

    HttpResponse httpResponse = createResponse(method, request, apacheHttpResponse);
    if (responseHandler.needsConnectionLeftOpen() && method instanceof HttpEntityEnclosingRequest) {
      HttpEntityEnclosingRequest httpEntityEnclosingRequest = (HttpEntityEnclosingRequest) method;
      httpResponse.setContent(new HttpMethodReleaseInputStream(httpEntityEnclosingRequest));
    }

    try {
      CountingInputStream countingInputStream = null;
      if (System.getProperty(PROFILING_SYSTEM_PROPERTY) != null) {
        countingInputStream = new CountingInputStream(httpResponse.getContent());
        httpResponse.setContent(countingInputStream);
      }

      long startTime = System.currentTimeMillis();
      AmazonWebServiceResponse<? extends T> awsResponse = responseHandler.handle(httpResponse);
      long endTime = System.currentTimeMillis();

      if (System.getProperty(PROFILING_SYSTEM_PROPERTY) != null) {
        if (executionContext.getTimingInfo() != null) {
          TimingInfo timingInfo = executionContext.getTimingInfo();
          TimingInfo responseProcessingTiming = new TimingInfo(startTime, endTime);
          timingInfo.addSubMeasurement(
              RESPONSE_PROCESSING_SUBMEASUREMENT, responseProcessingTiming);

          if (countingInputStream != null) {
            responseProcessingTiming.addCounter(
                BYTES_PROCESSED_COUNTER, countingInputStream.getByteCount());
          }
        }
      }

      if (awsResponse == null) throw new RuntimeException("Unable to unmarshall response metadata");

      responseMetadataCache.add(request.getOriginalRequest(), awsResponse.getResponseMetadata());

      if (requestLog.isDebugEnabled()) {
        requestLog.debug(
            "Received successful response: "
                + apacheHttpResponse.getStatusLine().getStatusCode()
                + ", AWS Request ID: "
                + awsResponse.getRequestId());
      }

      return awsResponse.getResult();
    } catch (Exception e) {
      String errorMessage = "Unable to unmarshall response (" + e.getMessage() + ")";
      log.warn(errorMessage, e);
      throw new AmazonClientException(errorMessage, e);
    }
  }
Пример #5
0
  public static void send(HttpResponse response, Channel channel, @Nullable HttpRequest request) {
    ChannelBuffer content = response.getContent();
    setContentLength(
        response, content == ChannelBuffers.EMPTY_BUFFER ? 0 : content.readableBytes());

    boolean keepAlive = request != null && addKeepAliveIfNeed(response, request);
    addCommonHeaders(response);
    send(response, channel, !keepAlive);
  }
  /**
   * Handles a successful response from a service call by unmarshalling the results using the
   * specified response handler.
   *
   * @param <T> The type of object expected in the response.
   * @param request The original request that generated the response being handled.
   * @param responseHandler The response unmarshaller used to interpret the contents of the
   *     response.
   * @param method The HTTP method that was invoked, and contains the contents of the response.
   * @param executionContext Extra state information about the request currently being executed.
   * @return The contents of the response, unmarshalled using the specified response handler.
   * @throws IOException If any problems were encountered reading the response contents from the
   *     HTTP method object.
   */
  private <T> T handleResponse(
      Request<?> request,
      HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler,
      HttpRequestBase method,
      org.apache.http.HttpResponse apacheHttpResponse,
      ExecutionContext executionContext)
      throws IOException {

    HttpResponse httpResponse = createResponse(method, request, apacheHttpResponse);
    if (responseHandler.needsConnectionLeftOpen() && method instanceof HttpEntityEnclosingRequest) {
      HttpEntityEnclosingRequest httpEntityEnclosingRequest = (HttpEntityEnclosingRequest) method;
      httpResponse.setContent(new HttpMethodReleaseInputStream(httpEntityEnclosingRequest));
    }

    try {
      CountingInputStream countingInputStream = null;
      if (System.getProperty(PROFILING_SYSTEM_PROPERTY) != null) {
        countingInputStream = new CountingInputStream(httpResponse.getContent());
        httpResponse.setContent(countingInputStream);
      }

      AWSRequestMetrics awsRequestMetrics = executionContext.getAwsRequestMetrics();
      awsRequestMetrics.startEvent(Field.ResponseProcessingTime.name());
      AmazonWebServiceResponse<? extends T> awsResponse = responseHandler.handle(httpResponse);
      awsRequestMetrics.endEvent(Field.ResponseProcessingTime.name());
      if (countingInputStream != null) {
        awsRequestMetrics.setCounter(
            Field.BytesProcessed.name(), countingInputStream.getByteCount());
      }

      if (awsResponse == null) throw new RuntimeException("Unable to unmarshall response metadata");

      responseMetadataCache.add(request.getOriginalRequest(), awsResponse.getResponseMetadata());

      if (requestLog.isDebugEnabled()) {
        requestLog.debug(
            "Received successful response: "
                + apacheHttpResponse.getStatusLine().getStatusCode()
                + ", AWS Request ID: "
                + awsResponse.getRequestId());
      }
      awsRequestMetrics.addProperty(Field.AWSRequestID.name(), awsResponse.getRequestId());

      return awsResponse.getResult();
    } catch (CRC32MismatchException e) {
      throw e;
    } catch (Exception e) {
      String errorMessage = "Unable to unmarshall response (" + e.getMessage() + ")";
      throw new AmazonClientException(errorMessage, e);
    }
  }
  @Test
  public void convertsResponses_failure() throws JSONException {
    Response response = new Response();
    response.setStatus(ErrorCodes.NO_SUCH_ELEMENT);
    response.setValue(ImmutableMap.of("color", "red"));

    HttpResponse converted = codec.encode(response);
    assertThat(converted.getStatus(), is(HTTP_INTERNAL_ERROR));
    assertThat(converted.getHeader(CONTENT_TYPE), is(JSON_UTF_8.toString()));

    Response rebuilt =
        new JsonToBeanConverter()
            .convert(Response.class, new String(converted.getContent(), UTF_8));

    assertEquals(response.getStatus(), rebuilt.getStatus());
    assertEquals(response.getState(), rebuilt.getState());
    assertEquals(response.getSessionId(), rebuilt.getSessionId());
    assertEquals(response.getValue(), rebuilt.getValue());
  }
Пример #8
0
  private void sendHttpResponse(Channel channel, HttpRequest request, HttpResponse response) {
    if (!channel.isOpen()) {
      return;
    }

    // response的内容已在各Listener中填充
    response.headers().set(Names.CONTENT_LENGTH, response.getContent().readableBytes());
    response.headers().set(Names.SERVER, "NAVI/1.1.4(UNIX)");

    if (!HttpHeaders.isKeepAlive(request)
        || response.getStatus() != HttpResponseStatus.OK
        || ServerConfigure.isChannelClose()) {
      response.headers().set(Names.CONNECTION, "close");
      channel.setAttachment(WRITING);
      ChannelFuture f = channel.write(response);
      f.addListener(ChannelFutureListener.CLOSE);
      f.addListener(
          new ChannelFutureListener() {

            public void operationComplete(ChannelFuture f) throws Exception {
              if (!f.isSuccess()) {
                log.error(f.getCause().getMessage(), f.getCause());
              }
            }
          });
    } else {
      if (request.getProtocolVersion() == HttpVersion.HTTP_1_0) {
        response.headers().add(Names.CONNECTION, "Keep-Alive");
      }
      channel.setAttachment(WRITING);
      ChannelFuture f = channel.write(response);
      f.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
      f.addListener(
          new ChannelFutureListener() {

            public void operationComplete(ChannelFuture f) throws Exception {
              if (!f.isSuccess()) {
                log.error(f.getCause().getMessage(), f.getCause());
              }
            }
          });
    }
  }
 @SuppressWarnings("unchecked")
 @Override
 public String success(HttpResponse hr) throws Exception {
   Map<String, ?> rsp = objectMapper.readValue(hr.getContent(), Map.class);
   return (String) rsp.get(REVISION_FIELD_NAME);
 }
Пример #10
0
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
      if (!readingChunks) {
        HttpResponse response = (HttpResponse) e.getMessage();

        StringBuilder sb = new StringBuilder();
        if (LOG.isDebugEnabled()) {
          sb.append("STATUS: ")
              .append(response.getStatus())
              .append(", VERSION: ")
              .append(response.getProtocolVersion())
              .append(", HEADER: ");
        }
        if (!response.getHeaderNames().isEmpty()) {
          for (String name : response.getHeaderNames()) {
            for (String value : response.getHeaders(name)) {
              if (LOG.isDebugEnabled()) {
                sb.append(name + " = " + value);
              }
              if (this.length == -1 && name.equals("Content-Length")) {
                this.length = Long.valueOf(value);
              }
            }
          }
        }
        if (LOG.isDebugEnabled()) {
          LOG.debug(sb.toString());
        }

        if (response.getStatus() == HttpResponseStatus.NO_CONTENT) {
          LOG.info("There are no data corresponding to the request");
          return;
        }

        this.raf = new RandomAccessFile(file, "rw");
        this.fc = raf.getChannel();

        if (response.isChunked()) {
          readingChunks = true;
        } else {
          ChannelBuffer content = response.getContent();
          if (content.readable()) {
            fc.write(content.toByteBuffer());
          }
        }
      } else {
        HttpChunk chunk = (HttpChunk) e.getMessage();
        if (chunk.isLast()) {
          readingChunks = false;
          long fileLength = fc.position();
          fc.close();
          raf.close();
          if (fileLength == length) {
            LOG.info("Data fetch is done (total received bytes: " + fileLength + ")");
          } else {
            LOG.info(
                "Data fetch is done, but cannot get all data "
                    + "(received/total: "
                    + fileLength
                    + "/"
                    + length
                    + ")");
          }
        } else {
          fc.write(chunk.getContent().toByteBuffer());
        }
      }
    }
Пример #11
0
    protected int doInBackground(Socket socket) {
      MagicInputStream input = null;
      BufferedOutputStream output = null;
      try {
        Log.v(TAG, "New connection opened");
        input = new MagicInputStream(new BufferedInputStream(socket.getInputStream()));
        output = new BufferedOutputStream(socket.getOutputStream());

        HttpRequest request = new HttpRequest();
        request.setRequestLine(input.readStringLine());
        String header;
        while (!(header = input.readStringLine()).equals("")) {
          request.addHeader(header);
        }
        // Rest is content now.
        if (request.hasHeader("Content-Length")) {
          int len = Integer.parseInt(request.getHeader("Content-Length").get(0));
          request.readAllContent(input, len);
        }

        // Filter annoying requests
        if (!filterRequest(request)) return 1;

        // Manipulate request
        manipulateRequest(request);

        // Execute
        try {
          HttpResponse response = executeRequest(request);
          if (!whitelistRequest(request)) manipulateResponse(response, request);
          output.write(
              String.format(
                      "HTTP/1.1 %d %s\r\n",
                      response.getResponseCode(), response.getResponseMessage())
                  .getBytes());

          for (Entry<String, List<String>> entry : response.getHeaderPairs().entrySet()) {
            for (String value : entry.getValue())
              output.write(String.format("%s:%s\r\n", entry.getKey(), value).getBytes());
          }
          Log.d(TAG, "Writing content");
          output.write(NEWLINE);
          output.write(response.getContent());
          output.flush();
        } catch (HttpExecuteException e) {
          Log.e(TAG, "Failed to execute HTTP request", e);
        } catch (IOException e) {
          Log.e(TAG, "Failed to execute HTTP request", e);
        }
      } catch (IOException e) {
        Log.i(TAG, "Network communications failed for HTTP connection", e);
      } finally {
        if (input != null)
          try {
            output.close();
            input.close();
            socket.close();
          } catch (IOException e) {
            Log.e(TAG, "Failed to close reader for HTTP connection", e);
          }
      }
      return 0;
    }
Пример #12
0
 @Override
 public DocumentOperationResult success(HttpResponse hr) throws Exception {
   return objectMapper.readValue(hr.getContent(), DocumentOperationResult.class);
 }