コード例 #1
0
  @Test
  public void parseMediaTypes() throws Exception {
    String s = "text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c";
    List<MediaType> mediaTypes = MediaType.parseMediaTypes(s);
    assertNotNull("No media types returned", mediaTypes);
    assertEquals("Invalid amount of media types", 4, mediaTypes.size());

    mediaTypes = MediaType.parseMediaTypes(null);
    assertNotNull("No media types returned", mediaTypes);
    assertEquals("Invalid amount of media types", 0, mediaTypes.size());
  }
コード例 #2
0
  @Test
  public void acceptHeader() throws Exception {
    this.builder.accept(MediaType.TEXT_HTML, MediaType.APPLICATION_XML);

    MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
    List<String> accept = Collections.list(request.getHeaders("Accept"));
    List<MediaType> result = MediaType.parseMediaTypes(accept.get(0));

    assertEquals(1, accept.size());
    assertEquals("text/html", result.get(0).toString());
    assertEquals("application/xml", result.get(1).toString());
  }
コード例 #3
0
 private MediaType getMediaTypes(HttpServletRequest request) {
   String acceptHeader = request.getHeader(ACCEPT_HEADER);
   if (StringUtils.hasText(acceptHeader)) {
     try {
       List<MediaType> acceptableMediaTypes = MediaType.parseMediaTypes(acceptHeader);
       return acceptableMediaTypes.isEmpty() ? null : acceptableMediaTypes.get(0);
     } catch (IllegalArgumentException ex) {
       if (logger.isDebugEnabled()) {
         logger.debug("Could not parse accept header [" + acceptHeader + "]: " + ex.getMessage());
       }
       return null;
     }
   }
   return null;
 }
コード例 #4
0
 private Set<ProduceMediaTypeExpression> parseExpressions(String[] produces, String[] headers) {
   Set<ProduceMediaTypeExpression> result = new LinkedHashSet<ProduceMediaTypeExpression>();
   if (headers != null) {
     for (String header : headers) {
       HeaderExpression expr = new HeaderExpression(header);
       if ("Accept".equalsIgnoreCase(expr.name)) {
         for (MediaType mediaType : MediaType.parseMediaTypes(expr.value)) {
           result.add(new ProduceMediaTypeExpression(mediaType, expr.isNegated));
         }
       }
     }
   }
   if (produces != null) {
     for (String produce : produces) {
       result.add(new ProduceMediaTypeExpression(produce));
     }
   }
   return result;
 }
コード例 #5
0
ファイル: UaaRequestMatcher.java プロジェクト: xiebing/uaa
  public boolean matches(HttpServletRequest request) {
    if (logger.isDebugEnabled()) {
      logger.debug(
          "Checking match of request : '"
              + request.getRequestURI()
              + "'; against '"
              + request.getContextPath()
              + path
              + "' with parameters="
              + parameters);
    }

    if (!request.getRequestURI().startsWith(request.getContextPath() + path)) {
      return false;
    }

    boolean parameterMatch = parameters.isEmpty();
    for (String key : parameters.keySet()) {
      String value = request.getParameter(key);
      parameterMatch = value != null ? value.startsWith(parameters.get(key)) : false;
    }
    if (accepts == null && parameterMatch) {
      return true;
    }

    if (request.getHeader("Accept") == null && parameterMatch) {
      return true;
    }

    // TODO: Use mime-type priorities
    for (MediaType acceptHeader : MediaType.parseMediaTypes(request.getHeader("Accept"))) {
      for (MediaType accept : accepts) {
        if (acceptHeader.includes(accept) && parameterMatch) {
          return true;
        }
      }
    }

    return false;
  }
コード例 #6
0
 /**
  * Determines the list of {@link MediaType} for the given {@link HttpServletRequest}.
  *
  * <p>The default implementation invokes {@link #getMediaTypeFromFilename(String)} if {@linkplain
  * #setFavorPathExtension favorPathExtension} property is <code>true</code>. If the property is
  * <code>false</code>, or when a media type cannot be determined from the request path, this
  * method will inspect the {@code Accept} header of the request.
  *
  * <p>This method can be overridden to provide a different algorithm.
  *
  * @param request the current servlet request
  * @return the list of media types requested, if any
  */
 protected List<MediaType> getMediaTypes(HttpServletRequest request) {
   if (this.favorPathExtension) {
     String requestUri = urlPathHelper.getLookupPathForRequest(request);
     String filename = WebUtils.extractFullFilenameFromUrlPath(requestUri);
     MediaType mediaType = getMediaTypeFromFilename(filename);
     if (mediaType != null) {
       if (logger.isDebugEnabled()) {
         logger.debug(
             "Requested media type is '" + mediaType + "' (based on filename '" + filename + "')");
       }
       return Collections.singletonList(mediaType);
     }
   }
   if (this.favorParameter) {
     if (request.getParameter(this.parameterName) != null) {
       String parameterValue = request.getParameter(this.parameterName);
       MediaType mediaType = getMediaTypeFromParameter(parameterValue);
       if (mediaType != null) {
         if (logger.isDebugEnabled()) {
           logger.debug(
               "Requested media type is '"
                   + mediaType
                   + "' (based on parameter '"
                   + this.parameterName
                   + "'='"
                   + parameterValue
                   + "')");
         }
         return Collections.singletonList(mediaType);
       }
     }
   }
   if (!this.ignoreAcceptHeader) {
     String acceptHeader = request.getHeader(ACCEPT_HEADER);
     if (StringUtils.hasText(acceptHeader)) {
       try {
         List<MediaType> acceptableMediaTypes = MediaType.parseMediaTypes(acceptHeader);
         List<MediaType> producibleMediaTypes = getProducibleMediaTypes(request);
         Set<MediaType> compatibleMediaTypes = new LinkedHashSet<MediaType>();
         for (MediaType acceptable : acceptableMediaTypes) {
           for (MediaType producible : producibleMediaTypes) {
             if (acceptable.isCompatibleWith(producible)) {
               compatibleMediaTypes.add(getMostSpecificMediaType(acceptable, producible));
             }
           }
         }
         List<MediaType> mediaTypes = new ArrayList<MediaType>(compatibleMediaTypes);
         MediaType.sortByQualityValue(mediaTypes);
         if (logger.isDebugEnabled()) {
           logger.debug(
               "Requested media types are "
                   + mediaTypes
                   + " based on Accept header types "
                   + "and producible media types "
                   + producibleMediaTypes
                   + ")");
         }
         return mediaTypes;
       } catch (IllegalArgumentException ex) {
         if (logger.isDebugEnabled()) {
           logger.debug(
               "Could not parse accept header [" + acceptHeader + "]: " + ex.getMessage());
         }
         return null;
       }
     }
   }
   if (this.defaultContentType != null) {
     if (logger.isDebugEnabled()) {
       logger.debug(
           "Requested media types is "
               + this.defaultContentType
               + " (based on defaultContentType property)");
     }
     return Collections.singletonList(this.defaultContentType);
   } else {
     return Collections.emptyList();
   }
 }