Search in sources :

Example 81 with MediaType

use of org.springframework.http.MediaType in project service-proxy by membrane.

the class Header method getBestAcceptedType.

/**
 * Tries to determines the index of the best content type.
 */
public int getBestAcceptedType(MediaType[] supported) {
    String accept = getFirstValue(ACCEPT);
    if (accept == null)
        return -1;
    List<MediaType> m;
    try {
        m = MediaType.parseMediaTypes(accept);
    } catch (IllegalArgumentException e) {
        return -1;
    }
    MediaType.sortByQualityValue(m);
    for (MediaType t : m) for (int i = 0; i < supported.length; i++) if (t.includes(supported[i]))
        return i;
    return -1;
}
Also used : MediaType(org.springframework.http.MediaType)

Example 82 with MediaType

use of org.springframework.http.MediaType in project com.revolsys.open by revolsys.

the class AbstractHttpMessageConverter method getMediaTypes.

public static List<MediaType> getMediaTypes(final Collection<?> contentTypes) {
    final List<MediaType> mediaTypes = new ArrayList<>();
    if (contentTypes != null) {
        for (final Object mediaTypeObject : contentTypes) {
            if (mediaTypeObject instanceof MediaType) {
                final MediaType mediaType = (MediaType) mediaTypeObject;
                mediaTypes.add(mediaType);
            } else if (mediaTypeObject != null) {
                mediaTypes.add(MediaType.parseMediaType(mediaTypeObject.toString()));
            }
        }
    }
    return mediaTypes;
}
Also used : ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType)

Example 83 with MediaType

use of org.springframework.http.MediaType in project com.revolsys.open by revolsys.

the class PageInfoHttpMessageConverter method writeMethod.

private void writeMethod(final XmlWriter writer, final String url, final PageInfo pageInfo, final String method, final Map<String, Object> values) {
    final Collection<ParameterInfo> parameters = pageInfo.getParameters();
    final boolean hasParameters = !parameters.isEmpty();
    if (hasParameters) {
        if (hasParameters) {
            writer.startTag(HtmlElem.DIV);
            writer.attribute(HtmlAttr.CLASS, "form");
            writer.startTag(HtmlElem.FORM);
            writer.attribute(HtmlAttr.ACTION, url);
            writer.attribute(HtmlAttr.TARGET, "_top");
            if (method.equals("post")) {
                boolean isFormData = false;
                for (final MediaType mediaType : pageInfo.getInputContentTypes()) {
                    if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType)) {
                        isFormData = true;
                    }
                }
                if (isFormData) {
                    writer.attribute(HtmlAttr.ENCTYPE, MediaType.MULTIPART_FORM_DATA.toString());
                }
            }
            writer.attribute(HtmlAttr.METHOD, method);
            writer.startTag(HtmlElem.DIV);
            writer.attribute(HtmlAttr.CLASS, "objectView");
            writer.startTag(HtmlElem.TABLE);
            writer.attribute(HtmlAttr.CLASS, "data");
            writer.startTag(HtmlElem.TBODY);
            for (final ParameterInfo parameter : parameters) {
                final Map<Object, Object> options = parameter.getAllowedValues();
                if (options.isEmpty()) {
                    if (parameter.getType().equals(DataTypes.BASE64_BINARY)) {
                        writeHtmlFileField(writer, parameter, values);
                    } else {
                        writeHtmlField(writer, parameter, values);
                    }
                } else {
                    writeHtmlSelect(writer, parameter, values, options);
                }
            }
        }
        final List<MediaType> mediaTypes = pageInfo.getMediaTypes();
        if (!mediaTypes.isEmpty()) {
            final ParameterInfo parameter = new ParameterInfo("format", true, DataTypes.STRING, "Select the file format to return the result data in.", mediaTypes);
            writeHtmlSelect(writer, parameter, values, mediaTypes);
        }
        writer.endTag(HtmlElem.TBODY);
        writer.endTag(HtmlElem.TABLE);
        writer.endTag(HtmlElem.DIV);
        writer.startTag(HtmlElem.DIV);
        writer.attribute(HtmlAttr.CLASS, "actionMenu");
        HtmlUtil.serializeSubmitInput(writer, "Submit", "submit");
        writer.endTag(HtmlElem.DIV);
        writer.endTag(HtmlElem.FORM);
        writer.endTag(HtmlElem.DIV);
    }
}
Also used : MediaType(org.springframework.http.MediaType) ParameterInfo(com.revolsys.ui.model.ParameterInfo)

Example 84 with MediaType

use of org.springframework.http.MediaType in project com.revolsys.open by revolsys.

the class PageInfo method setInputContentTypes.

public void setInputContentTypes(final MediaType... inputContentTypes) {
    final ArrayList<MediaType> mediaTypes = new ArrayList<>();
    for (final MediaType mediaType : inputContentTypes) {
        mediaTypes.add(mediaType);
    }
    setInputContentTypes(mediaTypes);
}
Also used : ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType)

Example 85 with MediaType

use of org.springframework.http.MediaType in project com.revolsys.open by revolsys.

the class GeometryHttpMessageConverter method write.

@Override
public void write(final Geometry geometry, final MediaType mediaType, final HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    if (!HttpServletUtils.getResponse().isCommitted()) {
        MediaType actualMediaType;
        if (mediaType == null) {
            actualMediaType = getDefaultMediaType();
        } else {
            actualMediaType = mediaType;
        }
        if (actualMediaType != null) {
            final Charset charset = HttpServletUtils.setContentTypeWithCharset(outputMessage, actualMediaType);
            final OutputStream body = outputMessage.getBody();
            final String mediaTypeString = actualMediaType.getType() + "/" + actualMediaType.getSubtype();
            final GeometryWriterFactory writerFactory = IoFactory.factoryByMediaType(GeometryWriterFactory.class, mediaTypeString);
            if (writerFactory == null) {
                throw new IllegalArgumentException("Media type " + actualMediaType + " not supported");
            } else {
                final String baseName = HttpServletUtils.getRequestBaseFileName();
                final GeometryWriter writer = writerFactory.newGeometryWriter(baseName, body, charset);
                writer.write(geometry);
                writer.close();
            }
        }
    }
}
Also used : OutputStream(java.io.OutputStream) MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset) GeometryWriter(com.revolsys.geometry.io.GeometryWriter) GeometryWriterFactory(com.revolsys.geometry.io.GeometryWriterFactory)

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