private void onException(Throwable e, Response r, boolean mapped) {
    if (request.isTracingEnabled()) {
      Response.Status s = Response.Status.fromStatusCode(r.getStatus());
      if (s != null) {
        request.trace(
            String.format(
                "mapped exception to response: %s -> %d (%s)",
                ReflectionHelper.objectToString(e), r.getStatus(), s.getReasonPhrase()));
      } else {
        request.trace(
            String.format(
                "mapped exception to response: %s -> %d",
                ReflectionHelper.objectToString(e), r.getStatus()));
      }
    }

    if (!mapped && r.getStatus() >= 500) {
      logException(e, r, Level.SEVERE);
    } else if (LOGGER.isLoggable(Level.FINE)) {
      logException(e, r, Level.FINE);
    }

    setResponse(r);

    this.mappedThrowable = e;

    if (getEntity() != null && getHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE) == null) {
      Object m = request.getProperties().get(HttpMethodRule.CONTENT_TYPE_PROPERTY);
      if (m != null) {
        request.getProperties().remove(HttpMethodRule.CONTENT_TYPE_PROPERTY);
        getHttpHeaders().putSingle(HttpHeaders.CONTENT_TYPE, m);
      }
    }
  }
 private ContainerRequest mockContainerRequest() {
   ContainerRequest containerRequest = mock(ContainerRequest.class);
   when(containerRequest.getBaseUri()).thenReturn(baseUri);
   when(containerRequest.getRequestUri()).thenReturn(requestUri);
   when(containerRequest.getMethod()).thenReturn(httpMethod);
   when(containerRequest.getRequestHeaders()).thenReturn(requestHeaders);
   return containerRequest;
 }
Example #3
0
 private ContainerRequest setupContainerAskForRM(int numContainers) {
   LOG.info(
       "Creating new ContainerRequest with "
           + this.maxResourceCapability
           + " and "
           + numContainers);
   ContainerRequest request =
       new ContainerRequest(
           this.maxResourceCapability, null, null, DEFAULT_PRIORITY, numContainers);
   LOG.info("Requested container ask: " + request.toString());
   return request;
 }
 @Override
 public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
   String version = request.getHeaderValue("Version");
   if (!supportedVersions.contains(version)) {
     response.setStatus(301);
     response.setEntity(null);
   }
   return response;
 }
 private void setHeaders(MultivaluedMap<String, Object> headers) {
   this.headers = headers;
   Object location = headers.getFirst(HttpHeaders.LOCATION);
   if (location != null) {
     if (location instanceof URI) {
       final URI locationUri = (URI) location;
       if (!locationUri.isAbsolute()) {
         final URI base =
             (statusType.getStatusCode() == Status.CREATED.getStatusCode())
                 ? request.getAbsolutePath()
                 : request.getBaseUri();
         location =
             UriBuilder.fromUri(base)
                 .path(locationUri.getRawPath())
                 .replaceQuery(locationUri.getRawQuery())
                 .fragment(locationUri.getRawFragment())
                 .build();
       }
       headers.putSingle(HttpHeaders.LOCATION, location);
     }
   }
 }
  /**
   * Map an exception to a response.
   *
   * @param e the exception.
   * @return true if the exception was mapped, otherwise false.
   */
  public boolean mapException(Throwable e) {
    ExceptionMapper em = wa.getExceptionMapperContext().find(e.getClass());
    if (em == null) {
      wa.getResponseListener().onError(Thread.currentThread().getId(), e);

      return false;
    }

    wa.getResponseListener().onMappedException(Thread.currentThread().getId(), e, em);

    if (request.isTracingEnabled()) {
      request.trace(
          String.format(
              "matched exception mapper: %s -> %s",
              ReflectionHelper.objectToString(e), ReflectionHelper.objectToString(em)));
    }

    try {
      Response r = em.toResponse(e);
      if (r == null) r = Response.noContent().build();
      onException(e, r, true);
    } catch (MappableContainerException ex) {
      // If the exception mapper throws a MappableContainerException then
      // rethrow it to the HTTP container
      throw ex;
    } catch (RuntimeException ex) {
      LOGGER.severe(
          "Exception mapper "
              + em
              + " for Throwable "
              + e
              + " threw a RuntimeException when "
              + "attempting to obtain the response");
      Response r = Response.serverError().build();
      onException(ex, r, false);
    }
    return true;
  }
  private void configureTrace(final ContainerResponseWriter crw) {
    final TraceInformation ti =
        (TraceInformation) request.getProperties().get(TraceInformation.class.getName());
    setContainerResponseWriter(
        new ContainerResponseWriter() {
          public OutputStream writeStatusAndHeaders(long contentLength, ContainerResponse response)
              throws IOException {
            ti.addTraceHeaders();
            return crw.writeStatusAndHeaders(contentLength, response);
          }

          public void finish() throws IOException {
            crw.finish();
          }
        });
  }
  /**
   * Constructs a HttpURLConnection to list blobs. Sign with no length specified.
   *
   * @param uri The absolute URI to the blob
   * @param timeout The server timeout interval
   * @param listingContext A set of parameters for the listing operation.
   * @param blobOptions the options to use for the request.
   * @param opContext a tracking object for the request
   * @return a HttpURLConnection configured for the operation.
   * @throws IOException if there is an error opening the connection
   * @throws URISyntaxException if the resource URI is invalid
   * @throws StorageException an exception representing any error which occurred during the
   *     operation.
   * @throws IllegalArgumentException
   */
  public static HttpURLConnection list(
      final URI uri,
      final int timeout,
      final BlobListingContext listingContext,
      final BlobRequestOptions blobOptions,
      final OperationContext opContext)
      throws URISyntaxException, IOException, StorageException {

    final UriQueryBuilder builder = ContainerRequest.getContainerUriQueryBuilder();
    builder.add("comp", "list");

    if (listingContext != null) {
      if (!Utility.isNullOrEmpty(listingContext.getPrefix())) {
        builder.add("prefix", listingContext.getPrefix());
      }

      if (!Utility.isNullOrEmpty(listingContext.getDelimiter())) {
        builder.add("delimiter", listingContext.getDelimiter());
      }

      if (!Utility.isNullOrEmpty(listingContext.getMarker())) {
        builder.add("marker", listingContext.getMarker());
      }

      if (listingContext.getMaxResults() != null && listingContext.getMaxResults() > 0) {
        builder.add("maxresults", listingContext.getMaxResults().toString());
      }

      if (listingContext.getListingDetails().size() > 0) {
        final StringBuilder sb = new StringBuilder();

        boolean started = false;

        if (listingContext.getListingDetails().contains(BlobListingDetails.SNAPSHOTS)) {
          if (!started) {
            started = true;
          } else {
            sb.append(",");
          }

          sb.append("snapshots");
        }

        if (listingContext.getListingDetails().contains(BlobListingDetails.UNCOMMITTED_BLOBS)) {
          if (!started) {
            started = true;
          } else {
            sb.append(",");
          }

          sb.append("uncommittedblobs");
        }

        if (listingContext.getListingDetails().contains(BlobListingDetails.METADATA)) {
          if (!started) {
            started = true;
          } else {
            sb.append(",");
          }

          sb.append("metadata");
        }

        builder.add("include", sb.toString());
      }
    }

    final HttpURLConnection request =
        BlobRequest.createURLConnection(uri, timeout, builder, blobOptions, opContext);

    request.setRequestMethod("GET");

    return request;
  }
  /**
   * Write the response.
   *
   * <p>The status and headers will be written by calling the method {@link
   * ContainerResponseWriter#writeStatusAndHeaders} on the provided {@link ContainerResponseWriter}
   * instance. The {@link OutputStream} returned from that method call is used to write the entity
   * (if any) to that {@link OutputStream}. An appropriate {@link MessageBodyWriter} will be found
   * to write the entity.
   *
   * @throws WebApplicationException if {@link MessageBodyWriter} cannot be found for the entity
   *     with a 500 (Internal Server error) response.
   * @throws java.io.IOException if there is an error writing the entity
   */
  public void write() throws IOException {
    if (isCommitted) return;

    if (request.isTracingEnabled()) {
      configureTrace(responseWriter);
    }

    if (entity == null) {
      isCommitted = true;
      responseWriter.writeStatusAndHeaders(-1, this);
      responseWriter.finish();
      return;
    }

    if (!getHttpHeaders().containsKey(HttpHeaders.VARY)) {
      final String varyHeader = (String) request.getProperties().get(ContainerRequest.VARY_HEADER);
      if (varyHeader != null) {
        getHttpHeaders().add(HttpHeaders.VARY, varyHeader);
      }
    }

    MediaType contentType = getMediaType();
    if (contentType == null) {
      contentType =
          getMessageBodyWorkers()
              .getMessageBodyWriterMediaType(
                  entity.getClass(), entityType, annotations, request.getAcceptableMediaTypes());
      if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype())
        contentType = MediaType.APPLICATION_OCTET_STREAM_TYPE;

      getHttpHeaders().putSingle(HttpHeaders.CONTENT_TYPE, contentType);
    }

    final MessageBodyWriter p =
        getMessageBodyWorkers()
            .getMessageBodyWriter(entity.getClass(), entityType, annotations, contentType);
    if (p == null) {
      String message =
          "A message body writer for Java class "
              + entity.getClass().getName()
              + ", and Java type "
              + entityType
              + ", and MIME media type "
              + contentType
              + " was not found";
      LOGGER.severe(message);
      Map<MediaType, List<MessageBodyWriter>> m = getMessageBodyWorkers().getWriters(contentType);
      LOGGER.severe(
          "The registered message body writers compatible with the MIME media type are:\n"
              + getMessageBodyWorkers().writersToString(m));

      if (request.getMethod().equals("HEAD")) {
        isCommitted = true;
        responseWriter.writeStatusAndHeaders(-1, this);
        responseWriter.finish();
        return;
      } else {
        throw new WebApplicationException(new MessageException(message), 500);
      }
    }

    final long size = p.getSize(entity, entity.getClass(), entityType, annotations, contentType);
    if (request.getMethod().equals("HEAD")) {
      if (size != -1) getHttpHeaders().putSingle(HttpHeaders.CONTENT_LENGTH, Long.toString(size));
      isCommitted = true;
      responseWriter.writeStatusAndHeaders(0, this);
    } else {
      if (request.isTracingEnabled()) {
        request.trace(
            String.format(
                "matched message body writer: %s, \"%s\" -> %s",
                ReflectionHelper.objectToString(entity),
                contentType,
                ReflectionHelper.objectToString(p)));
      }

      if (out == null) out = new CommittingOutputStream(size);
      p.writeTo(
          entity, entity.getClass(), entityType, annotations, contentType, getHttpHeaders(), out);
      if (!isCommitted) {
        isCommitted = true;
        responseWriter.writeStatusAndHeaders(-1, this);
      }
    }
    responseWriter.finish();
  }