Example usage for org.springframework.http MediaType toString

List of usage examples for org.springframework.http MediaType toString

Introduction

In this page you can find the example usage for org.springframework.http MediaType toString.

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:eionet.webq.web.controller.FileDownloadController.java

/**
 * Sets content type header to response.
 *
 * @param response    http response/* w w w.  j  a v  a  2  s. c  o  m*/
 * @param contentType content type
 */
private void setContentType(HttpServletResponse response, MediaType contentType) {
    if (contentType != null) {
        response.setContentType(contentType.toString());
        response.setCharacterEncoding("utf-8");
    }
}

From source file:org.apache.geode.management.internal.web.controllers.ShellCommandsControllerProcessCommandTest.java

private void assertThatContentTypeEquals(ResponseEntity<InputStreamResource> response, MediaType mediaType) {
    assertThat(response.getHeaders().get(HttpHeaders.CONTENT_TYPE)).containsExactly(mediaType.toString());

}

From source file:org.encuestame.oauth.AbstractOAuthSupport.java

/**
 * Define customizable list of {@link HttpMessageConverter}.
 * @param converters/*from  www  . j a v  a 2  s  .  c o m*/
 */
public AbstractOAuthSupport(final List<HttpMessageConverter<?>> converters) {
    this.restTemplate = new RestTemplate();
    this.restTemplate.setMessageConverters(converters);
    log.debug("OAuth Converters Size " + this.restTemplate.getMessageConverters().size());
    if (log.isDebugEnabled()) {
        for (HttpMessageConverter<?> httpMessageConverter : this.restTemplate.getMessageConverters()) {
            log.debug("--- OAuth Converters " + httpMessageConverter);
            for (MediaType medidaType : httpMessageConverter.getSupportedMediaTypes()) {
                log.debug("------ Converter Media Type " + medidaType.toString());
            }
        }
    }
}

From source file:org.openrepose.filters.ratelimiting.RateLimitingHandler.java

private FilterAction noUpstreamResponse(HttpServletRequestWrapper request,
        HttpServletResponseWrapper response) {
    try {//from w  w  w  .java2 s  .  co m
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        final MediaType mediaType = rateLimitingServiceHelper.queryActiveLimits(request,
                originalPreferredAccept, outputStream);
        response.setOutput(new ByteArrayInputStream(outputStream.toByteArray()));
        response.setContentType(mediaType.toString());
        response.setStatus(SC_OK);
    } catch (Exception e) {
        LOG.error("Failure when querying limits. Reason: " + e.getMessage(), e);
        response.setStatus(SC_INTERNAL_SERVER_ERROR);
    }

    return FilterAction.RETURN;
}

From source file:org.openrepose.filters.ratelimiting.RateLimitingHandler.java

public void handleResponse(HttpServletRequestWrapper request, HttpServletResponseWrapper response) {
    try {/*from w  w w.  j ava 2  s  . c o m*/
        if (response.getContentType() != null) {
            //If we have a content type to process, then we should do something about it,
            // else we should ensure that just the repose limits make it through...

            // I have to use mutable state, and that makes me sad, because If's aren't expressions
            InputStream absoluteInputStream;
            if (response.getContentType().equalsIgnoreCase(MediaType.APPLICATION_JSON_VALUE)) {
                //New set up! Grab the upstream json, make it look like XML
                String newXml = UpstreamJsonToXml.convert(response.getOutputStreamAsInputStream());

                //Now we use the new XML we converted from the JSON as the input to the processing stream
                absoluteInputStream = new ByteArrayInputStream(newXml.getBytes(StandardCharsets.UTF_8));
            } else if (response.getContentType().equalsIgnoreCase(MediaType.APPLICATION_XML_VALUE)) {
                //If we got XML from upstream, just read the stream directly
                absoluteInputStream = response.getOutputStreamAsInputStream();
            } else {
                LOG.error("Upstream limits responded with a content type we cannot understand: {}",
                        response.getContentType());
                //Upstream responded with something we cannot talk, we failed to combine upstream limits, return a 502!
                throw new UpstreamException(
                        "Upstream limits responded with a content type we cannot understand: "
                                + response.getContentType());
            }

            //We'll get here if we were able to properly parse JSON, or if we had XML from upstream!
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            final MediaType mediaType = rateLimitingServiceHelper.queryCombinedLimits(request,
                    originalPreferredAccept, absoluteInputStream, outputStream);
            response.setOutput(new ByteArrayInputStream(outputStream.toByteArray()));
            response.setContentType(mediaType.toString());
        } else {
            LOG.warn("NO DATA RECEIVED FROM UPSTREAM limits, only sending regular rate limits!");
            //No data from upstream, so we send the regular stuff no matter what
            noUpstreamResponse(request, response);
        }
    } catch (UpstreamException ue) {
        //I want a 502 returned when upstream didn't respond appropriately
        LOG.error("Failure when querying limits. Reason: " + ue.getMessage(), ue);
        response.setStatus(SC_BAD_GATEWAY);
    } catch (Exception e) {
        LOG.error("Failure when querying limits. Reason: " + e.getMessage(), e);
        response.setStatus(SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:org.springframework.cloud.dataflow.shell.command.HttpCommands.java

private void outputRequest(String method, URI requestUri, MediaType mediaType, String requestData,
        StringBuilder buffer) {//from   w w  w.  jav a2s  .  c  o  m
    buffer.append("> ").append(method).append(' ');
    if (mediaType != null) {
        buffer.append("(").append(mediaType.toString()).append(") ");
    }
    buffer.append(requestUri.toString()).append(" ").append(requestData).append(OsUtils.LINE_SEPARATOR);
}

From source file:org.springframework.http.server.reactive.ServletServerHttpResponse.java

@Override
protected void writeHeaders() {
    for (Map.Entry<String, List<String>> entry : getHeaders().entrySet()) {
        String headerName = entry.getKey();
        for (String headerValue : entry.getValue()) {
            this.response.addHeader(headerName, headerValue);
        }/* ww  w. ja  v  a2 s.c o  m*/
    }
    MediaType contentType = getHeaders().getContentType();
    if (this.response.getContentType() == null && contentType != null) {
        this.response.setContentType(contentType.toString());
    }
    Charset charset = (contentType != null ? contentType.getCharSet() : null);
    if (this.response.getCharacterEncoding() == null && charset != null) {
        this.response.setCharacterEncoding(charset.name());
    }
}

From source file:org.springframework.social.google.api.drive.FileComment.java

public FileComment(String content, MediaType contextType, String contextValue) {
    this(content, contextType.toString(), contextValue);
}

From source file:org.springframework.web.servlet.resource.ResourceHttpRequestHandler.java

/**
 * Set headers on the given servlet response.
 * Called for GET requests as well as HEAD requests.
 * @param response current servlet response
 * @param resource the identified resource (never {@code null})
 * @param mediaType the resource's media type (never {@code null})
 * @throws IOException in case of errors while setting the headers
 *//*  w  ww  .  j a  va 2  s . co m*/
protected void setHeaders(HttpServletResponse response, Resource resource, @Nullable MediaType mediaType)
        throws IOException {

    long length = resource.contentLength();
    if (length > Integer.MAX_VALUE) {
        response.setContentLengthLong(length);
    } else {
        response.setContentLength((int) length);
    }

    if (mediaType != null) {
        response.setContentType(mediaType.toString());
    }
    if (resource instanceof HttpResource) {
        HttpHeaders resourceHeaders = ((HttpResource) resource).getResponseHeaders();
        for (Map.Entry<String, List<String>> entry : resourceHeaders.entrySet()) {
            String headerName = entry.getKey();
            boolean first = true;
            for (String headerValue : entry.getValue()) {
                if (first) {
                    response.setHeader(headerName, headerValue);
                } else {
                    response.addHeader(headerName, headerValue);
                }
                first = false;
            }
        }
    }
    response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
}