Search in sources :

Example 46 with MediaType

use of org.springframework.http.MediaType in project webofneeds by researchstudio-sat.

the class WonEtagHelper method fromEtagValue.

/**
 * Returns a WonEtagHelper if the specified string is a valid ETAG, combining
 * [VERSION] {[DELIMITER] [MEDIA-TYPE]} (curly brackets inticate 'optional')
 * Returns null if the specified etagValue is null or not valid.
 * @param etagValue
 * @return
 */
public static WonEtagHelper fromEtagValue(String etagValue) {
    if (etagValue == null)
        return null;
    etagValue = etagValue.trim();
    if (etagValue.startsWith("W/")) {
        logger.debug("weak etag matching is not supported, cannot " + "process: " + etagValue);
        return null;
    }
    if (!etagValue.startsWith("\"")) {
        logger.debug("etag must start with '\"', cannot process: " + etagValue);
        return null;
    }
    if (!etagValue.endsWith("\"")) {
        logger.debug("etag must end with '\"', cannot process: " + etagValue);
        return null;
    }
    int index = etagValue.indexOf(VERSION_MEDIATYPE_DELIMITER);
    if (index == -1) {
        // delimiter not found. Assume only version
        return new WonEtagHelper(etagValue.substring(1, etagValue.length() - 1), null);
    }
    MediaType mt = null;
    try {
        mt = MediaType.parseMediaType(etagValue.substring(index, etagValue.length() - 1));
    } catch (Exception e) {
        logger.debug("not a valid media type in etag value, cannot process: " + etagValue);
        // not a valid media type
        return null;
    }
    return new WonEtagHelper(etagValue.substring(1, index), mt);
}
Also used : MediaType(org.springframework.http.MediaType)

Example 47 with MediaType

use of org.springframework.http.MediaType in project webofneeds by researchstudio-sat.

the class LinkedDataWebController method readAttachment.

@RequestMapping(value = "${uri.path.data.attachment}/{identifier}", method = RequestMethod.GET, produces = { "application/ld+json", "application/trig", "application/n-quads", "*/*" })
public ResponseEntity<Dataset> readAttachment(HttpServletRequest request, @PathVariable(value = "identifier") String identifier) {
    logger.debug("readAttachment() called");
    URI attachmentURI = uriService.createAttachmentURIForId(identifier);
    DataWithEtag<Dataset> data = linkedDataService.getDatasetForUri(attachmentURI, null);
    if (!data.isNotFound()) {
        HttpHeaders headers = new HttpHeaders();
        addCORSHeader(headers);
        String mimeTypeOfResponse = RdfUtils.findFirst(data.getData(), model -> {
            String content = getObjectOfPropertyAsString(model, CNT.BYTES);
            if (content == null)
                return null;
            return getObjectOfPropertyAsString(model, WONMSG.CONTENT_TYPE);
        });
        if (mimeTypeOfResponse != null) {
            // we found a base64 encoded attachment, we obtained its contentType, so we set
            // it as the
            // contentType of the response.
            Set<MediaType> producibleMediaTypes = new HashSet<>();
            producibleMediaTypes.add(MediaType.valueOf(mimeTypeOfResponse));
            request.setAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, producibleMediaTypes);
        }
        return new ResponseEntity<>(data.getData(), headers, HttpStatus.OK);
    } else {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) Dataset(org.apache.jena.query.Dataset) MediaType(org.springframework.http.MediaType) URI(java.net.URI) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 48 with MediaType

use of org.springframework.http.MediaType in project gocd by gocd.

the class BasicProcessingFilterEntryPoint method getAcceptHeader.

private ArrayList<String> getAcceptHeader(ServletRequest request) {
    ArrayList<String> headers = new ArrayList<>();
    if (request instanceof HttpServletRequest) {
        String accept = ((HttpServletRequest) request).getHeader("Accept");
        if (accept != null) {
            List<MediaType> mediaTypes = MediaType.parseMediaTypes(accept);
            for (MediaType mediaType : mediaTypes) {
                String type = mediaType.getType() + "/" + mediaType.getSubtype();
                headers.add(type);
            }
        }
    }
    return headers;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType)

Example 49 with MediaType

use of org.springframework.http.MediaType in project cas by apereo.

the class JsonUtils method render.

/**
 * Render model and view.
 *
 * @param model    the model
 * @param response the response
 */
@SneakyThrows
public static void render(final Object model, final HttpServletResponse response) {
    final MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    jsonConverter.setPrettyPrint(true);
    final MediaType jsonMimeType = MediaType.APPLICATION_JSON;
    jsonConverter.write(model, jsonMimeType, new ServletServerHttpResponse(response));
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) MediaType(org.springframework.http.MediaType) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) SneakyThrows(lombok.SneakyThrows)

Example 50 with MediaType

use of org.springframework.http.MediaType in project coffeenet-starter by coffeenet.

the class IntegrationCoffeeNetWebSecurityConfigurerAdapter method mediaTypeRequestMatcher.

private static MediaTypeRequestMatcher mediaTypeRequestMatcher(final ContentNegotiationStrategy contentNegotiationStrategy) {
    ContentNegotiationStrategy negotiationStrategy = contentNegotiationStrategy;
    if (negotiationStrategy == null) {
        negotiationStrategy = new HeaderContentNegotiationStrategy();
    }
    MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, APPLICATION_XHTML_XML, new MediaType("image", "*"), TEXT_HTML, TEXT_PLAIN);
    matcher.setIgnoredMediaTypes(singleton(ALL));
    return matcher;
}
Also used : MediaType(org.springframework.http.MediaType) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) MediaTypeRequestMatcher(org.springframework.security.web.util.matcher.MediaTypeRequestMatcher) ContentNegotiationStrategy(org.springframework.web.accept.ContentNegotiationStrategy) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy)

Aggregations

MediaType (org.springframework.http.MediaType)477 Test (org.junit.jupiter.api.Test)177 HttpHeaders (org.springframework.http.HttpHeaders)97 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)58 Charset (java.nio.charset.Charset)42 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)34 List (java.util.List)33 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)33 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)33 HashMap (java.util.HashMap)30 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)30 IOException (java.io.IOException)26 Resource (org.springframework.core.io.Resource)23 ResolvableType (org.springframework.core.ResolvableType)22 HttpEntity (org.springframework.http.HttpEntity)21 ServerWebExchange (org.springframework.web.server.ServerWebExchange)20 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)20 Nullable (org.springframework.lang.Nullable)18 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)18