@Override public String toString() { return toContentTypeString(); } }
private String getActualFormat(final ContentType contentType) { return (ClientProperty.class.isAssignableFrom(reference) && (contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC) || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML)) ? ContentType.APPLICATION_XML : contentType).toContentTypeString(); }
private Charset getCharset(final BatchQueryOperation operation) { final ContentType contentType = ContentType.parse(operation.getHeaders().getHeader(HttpHeader.CONTENT_TYPE)); if (contentType != null) { final String charsetValue = contentType.getParameter(ContentType.PARAMETER_CHARSET); if (charsetValue == null) { if (contentType.isCompatible(ContentType.APPLICATION_JSON) || contentType.getSubtype().contains("xml")) { return Charset.forName("UTF-8"); } } else { return Charset.forName(charsetValue); } } return Charset.forName("ISO-8859-1"); }
public static boolean isODataMetadataNone(final ContentType contentType) { return contentType.isCompatible(ContentType.APPLICATION_JSON) && ContentType.VALUE_ODATA_METADATA_NONE.equalsIgnoreCase( contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA)); }
/** * Creates a list of {@link AcceptType} objects based on given content type. * @param contentType the content type * @return an immutable one-element list of <code>AcceptType</code> objects that matches only the given content type */ public static List<AcceptType> fromContentType(final ContentType contentType) { return Collections.singletonList(new AcceptType( contentType.getType(), contentType.getSubtype(), contentType.getParameters(), 1F)); }
@Override public ODataSerializer createSerializer(final ContentType contentType) throws SerializerException { ODataSerializer serializer = null; if (contentType.isCompatible(ContentType.APPLICATION_JSON)) { final String metadata = contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA); if (metadata == null || ContentType.VALUE_ODATA_METADATA_MINIMAL.equalsIgnoreCase(metadata) || ContentType.VALUE_ODATA_METADATA_NONE.equalsIgnoreCase(metadata) || ContentType.VALUE_ODATA_METADATA_FULL.equalsIgnoreCase(metadata)) { serializer = new ODataJsonSerializer(contentType, new Constantsv00()); } } else if (contentType.isCompatible(ContentType.APPLICATION_XML) || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML)) { serializer = new ODataXmlSerializer(); } if (serializer == null) { throw new SerializerException("Unsupported format: " + contentType.toContentTypeString(), SerializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString()); } else { return serializer; } }
static void writeError(ServletRequest request, TeiidProcessingException e, HttpServletResponse httpResponse, int statusCode) throws IOException { httpResponse.setStatus(statusCode); ContentType contentType = ContentType.parse(request.getContentType()); PrintWriter writer = httpResponse.getWriter(); String code = e.getCode()==null?"":e.getCode(); //$NON-NLS-1$ String message = e.getMessage()==null?"":e.getMessage(); //$NON-NLS-1$ if (contentType == null || contentType.isCompatible(ContentType.APPLICATION_JSON)) { httpResponse.setHeader(HttpHeader.CONTENT_TYPE, ContentType.APPLICATION_JSON.toContentTypeString()); writer.write("{ \"error\": { \"code\": \""+StringEscapeUtils.escapeJson(code)+"\", \"message\": \""+StringEscapeUtils.escapeJson(message)+"\" } }"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } else { httpResponse.setHeader(HttpHeader.CONTENT_TYPE, ContentType.APPLICATION_XML.toContentTypeString()); writer.write("<m:error xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\"><m:code>"+StringEscapeUtils.escapeXml10(code)+"</m:code><m:message>"+StringEscapeUtils.escapeXml10(message)+"</m:message></m:error>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } writer.close(); }
type = ContentType.create(contentType); } catch (final IllegalArgumentException e) { throw new BatchDeserializerException("Invalid content type.", e, BatchDeserializerException.MessageKeys.INVALID_CONTENT_TYPE, Integer.toString(line)); if (type.isCompatible(expected)) { return type; } else { throw new BatchDeserializerException("Content type is not the expected content type", BatchDeserializerException.MessageKeys.UNEXPECTED_CONTENT_TYPE, Integer.toString(line), expected.toContentTypeString(), type.toContentTypeString());
@Override public ClientPrimitiveValue getBody() { if (value == null) { final ContentType contentType = ContentType.parse(getContentType()); try { value = odataClient.getObjectFactory().newPrimitiveValueBuilder(). setType(contentType.isCompatible(ContentType.TEXT_PLAIN) ? EdmPrimitiveTypeKind.String : EdmPrimitiveTypeKind.Stream). setValue(IOUtils.toString(getRawResponse())).build(); } catch (Exception e) { throw new HttpClientException(e); } finally { this.close(); } } return value; } }
public ContentType getRequestContentType() { if (this.request.getHeader(HttpHeader.CONTENT_TYPE) != null) { return ContentType.parse(this.request.getHeader(HttpHeader.CONTENT_TYPE)); } return ContentType.APPLICATION_OCTET_STREAM; }
public ClientODataDeserializerImpl(final boolean serverMode, final ContentType contentType) { this.contentType = contentType; if (contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC) || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML) || contentType.isCompatible(ContentType.APPLICATION_XML)) { deserializer = new AtomDeserializer(); } else { deserializer = new JsonDeserializer(serverMode); } }
try { formatTypes = AcceptType.fromContentType(contentType == null ? ContentType.create(formatOption.getFormat()) : contentType); } catch (final IllegalArgumentException e) { throw new AcceptHeaderContentNegotiatorException( "unsupported accept content type: " + requestedContentType + " != " + supportedContentTypes, ContentNegotiatorException.MessageKeys.UNSUPPORTED_CONTENT_TYPE, requestedContentType.toContentTypeString());
/** * Creates a {@link ContentType} based on given input string (<code>format</code>). Supported format is * <code>Media Type</code> format as defined in RFC 7231, chapter 3.1.1.1. * * @param format a string in format as defined in RFC 7231, chapter 3.1.1.1 * @return a new {@link ContentType} object * @throws IllegalArgumentException if input string is not parseable */ public static ContentType create(final String format) throws IllegalArgumentException { if (format == null) { throw new IllegalArgumentException("Parameter format MUST NOT be NULL."); } List<String> typeSubtype = new ArrayList<String>(); Map<String, String> parameters = new HashMap<String, String>(); parse(format, typeSubtype, parameters); return new ContentType(typeSubtype.get(0), typeSubtype.get(1), parameters); }
/** * Parses the given input string (<code>format</code>) and returns created {@link ContentType} if input was valid or * return <code>NULL</code> if input was not parseable. * * For the definition of the supported format see {@link #create(String)}. * * @param format a string in format as defined in RFC 7231, chapter 3.1.1.1 * @return a new <code>ContentType</code> object */ public static ContentType parse(final String format) { try { return ContentType.create(format); } catch (IllegalArgumentException e) { return null; } }
public static boolean isODataIEEE754Compatible(final ContentType contentType) { return Boolean.TRUE.toString().equalsIgnoreCase( contentType.getParameter(ContentType.PARAMETER_IEEE754_COMPATIBLE)); } }
private boolean isIEEE754Compatible() { final String parameter = contentType.getParameters().get(ContentType.PARAMETER_IEEE754_COMPATIBLE); return parameter == null ? false : "true".equalsIgnoreCase(parameter); }
@Override public ODataSerializer createSerializer(final ContentType contentType) throws SerializerException { ODataSerializer serializer = null; if (contentType.isCompatible(ContentType.APPLICATION_JSON)) { final String metadata = contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA); if (metadata == null || ContentType.VALUE_ODATA_METADATA_MINIMAL.equalsIgnoreCase(metadata) || ContentType.VALUE_ODATA_METADATA_NONE.equalsIgnoreCase(metadata) || ContentType.VALUE_ODATA_METADATA_FULL.equalsIgnoreCase(metadata)) { serializer = new ODataJsonSerializer(contentType, new Constantsv00()); } } else if (contentType.isCompatible(ContentType.APPLICATION_XML) || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML)) { serializer = new ODataXmlSerializer(); } if (serializer == null) { throw new SerializerException("Unsupported format: " + contentType.toContentTypeString(), SerializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString()); } else { return serializer; } }
type = ContentType.create(contentType); } catch (final IllegalArgumentException e) { throw new BatchDeserializerException("Invalid content type.", e, BatchDeserializerException.MessageKeys.INVALID_CONTENT_TYPE, Integer.toString(line)); if (type.isCompatible(expected)) { return type; } else { throw new BatchDeserializerException("Content type is not the expected content type", BatchDeserializerException.MessageKeys.UNEXPECTED_CONTENT_TYPE, Integer.toString(line), expected.toContentTypeString(), type.toContentTypeString());
@Override public ClientPrimitiveValue getBody() { if (value == null) { final ContentType contentType = ContentType.parse(getContentType()); try { value = odataClient.getObjectFactory().newPrimitiveValueBuilder(). setType(contentType.isCompatible(ContentType.TEXT_PLAIN) ? EdmPrimitiveTypeKind.String : EdmPrimitiveTypeKind.Stream). setValue(IOUtils.toString(getRawResponse())).build(); } catch (Exception e) { throw new HttpClientException(e); } finally { this.close(); } } return value; } }
public ContentType getRequestContentType() { if (this.request.getHeader(HttpHeader.CONTENT_TYPE) != null) { return ContentType.parse(this.request.getHeader(HttpHeader.CONTENT_TYPE)); } return ContentType.APPLICATION_OCTET_STREAM; }