コード例 #1
0
    private static ContainerRequest ensureValidRequest(final ContainerRequest request)
        throws IllegalStateException {
      if (request.getMethod().equals("GET")) {
        throw new IllegalStateException(LocalizationMessages.FORM_PARAM_METHOD_ERROR());
      }

      if (!MediaTypes.typeEqual(
          MediaType.APPLICATION_FORM_URLENCODED_TYPE, request.getMediaType())) {
        throw new IllegalStateException(LocalizationMessages.FORM_PARAM_CONTENT_TYPE_ERROR());
      }
      return request;
    }
コード例 #2
0
    private Form getFormParameters(ContainerRequest request) {
      if (MediaTypes.typeEqual(
          MediaType.APPLICATION_FORM_URLENCODED_TYPE, request.getMediaType())) {
        request.bufferEntity();
        Form form;
        if (decode) {
          form = request.readEntity(Form.class);
        } else {
          Annotation[] annotations = new Annotation[1];
          annotations[0] = encodedAnnotation;
          form = request.readEntity(Form.class, annotations);
        }

        return (form == null ? new Form() : form);
      } else {
        return new Form();
      }
    }
コード例 #3
0
  protected MultiPart readMultiPart(
      final Class<MultiPart> type,
      final Type genericType,
      final Annotation[] annotations,
      final MediaType mediaType,
      final MultivaluedMap<String, String> headers,
      final InputStream stream)
      throws IOException, MIMEParsingException {
    final MIMEMessage mimeMessage =
        new MIMEMessage(stream, mediaType.getParameters().get("boundary"), mimeConfig);

    boolean formData = MediaTypes.typeEqual(mediaType, MediaType.MULTIPART_FORM_DATA_TYPE);
    MultiPart multiPart = formData ? new FormDataMultiPart() : new MultiPart();

    final MessageBodyWorkers workers = messageBodyWorkers.get();
    multiPart.setMessageBodyWorkers(workers);

    MultivaluedMap<String, String> multiPartHeaders = multiPart.getHeaders();
    for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
      List<String> values = entry.getValue();

      for (String value : values) {
        multiPartHeaders.add(entry.getKey(), value);
      }
    }

    boolean fileNameFix;
    if (!formData) {
      multiPart.setMediaType(mediaType);
      fileNameFix = false;
    } else {
      // see if the User-Agent header corresponds to some version of MS Internet Explorer
      // if so, need to set fileNameFix to true to handle issue
      // http://java.net/jira/browse/JERSEY-759
      String userAgent = headers.getFirst(HttpHeaders.USER_AGENT);
      fileNameFix = userAgent != null && userAgent.contains(" MSIE ");
    }

    for (MIMEPart mimePart : mimeMessage.getAttachments()) {
      BodyPart bodyPart = formData ? new FormDataBodyPart(fileNameFix) : new BodyPart();

      // Configure providers.
      bodyPart.setMessageBodyWorkers(workers);

      // Copy headers.
      for (Header header : mimePart.getAllHeaders()) {
        bodyPart.getHeaders().add(header.getName(), header.getValue());
      }

      try {
        String contentType = bodyPart.getHeaders().getFirst("Content-Type");
        if (contentType != null) bodyPart.setMediaType(MediaType.valueOf(contentType));

        bodyPart.getContentDisposition();
      } catch (IllegalArgumentException ex) {
        throw new WebApplicationException(ex, Status.BAD_REQUEST);
      }

      // Copy data into a BodyPartEntity structure.
      bodyPart.setEntity(new BodyPartEntity(mimePart));

      // Add this BodyPart to our MultiPart.
      multiPart.getBodyParts().add(bodyPart);
    }

    return multiPart;
  }