public Builder setContentType(MediaType mediaType) { this.headersBuilder.put(HeaderNames.CONTENT_TYPE, mediaType.toString()); return this; }
public boolean isWildCardMediaType() { return WILDCARD_ANY.getType().equals(this.type) && WILDCARD_ANY.getSubType().equals(this.subType); }
/** * Computes a score by examining a list of media types (typically from the 'Accept' header) against * a required media type. * * @param mediaTypes The list of media types to examine. * @param requiredMediaType The required media type. * @return A score that indicates if one of the media types in the list matches the required media type. */ protected int scoreByMediaType(List<MediaType> mediaTypes, MediaType requiredMediaType) { int score = MAXIMUM_HEADER_SCORE; boolean match = false; boolean matchWildCard = false; for (MediaType mediaType : mediaTypes) { if (mediaType.matches(requiredMediaType)) { if (mediaType.isWildCardMediaType()) { matchWildCard = true; } match = true; break; } // Lower the score for each subsequent possible match score -= 2; } return match && !matchWildCard ? score : matchWildCard ? WILDCARD_MATCH_SCORE : DEFAULT_SCORE; }
private Map<String, String> buildRenderedData(ProcessorResult result) throws ODataException { LOG.debug("Start to render the data"); Map<String, String> resultMap = new HashMap<>(); if (result.getStatus().getCode() < ODataResponse.Status.NO_CONTENT.getCode()) { String contentType = result.getHeaders().get(CONTENT_TYPE); if (!result.getHeaders().containsKey(CONTENT_TYPE) || contentType.startsWith(MediaType.ATOM_XML.toString())) { // render XML resultMap.put(FORMAT, MediaType.ATOM_XML.toString()); resultMap.put(BODY, getRenderedXML(result)); } else if (contentType.startsWith(MediaType.JSON.toString())) { // render JSON resultMap.put(FORMAT, MediaType.JSON.toString()); resultMap.put(BODY, getRenderedJSON(result)); } } else { resultMap.put(FORMAT, MediaType.HTTP.getSubType()); resultMap.put(BODY, (String) result.getData()); } return resultMap; }
@Test public void getInputStream() throws ODataClientException, IOException { URL url = new URL(basePath + RESPONSE); InputStream stream = caller.getInputStream(singletonMap("Accept", JSON.getType()), url ); StringBuilder sb = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) { String line; while ((line = reader.readLine()) != null) { sb.append(line).append(System.lineSeparator()); } } assertThat(sb.toString(), equalTo(URLTestUtils.loadTextFile(RESPONSE))); }
/** * Calculates score based on given media type. * * @param contentTypeFromRequest content type from the ODatRequest * @param expectedTypes is variable arguments * @return integer value which represent the score */ protected int score(MediaType contentTypeFromRequest, MediaType... expectedTypes) { if (contentTypeFromRequest == null) { return DEFAULT_SCORE; } for (MediaType expected : expectedTypes) { if (contentTypeFromRequest.matches(expected)) { return MAXIMUM_FORMAT_SCORE; } } return DEFAULT_SCORE; } }
public List<MediaType> getAccept() { String acceptHeader = getHeader(HeaderNames.ACCEPT); if (isNullOrEmpty(acceptHeader)) { return Collections.emptyList(); } List<MediaType> mediaTypesBuilder = new ArrayList<>(); for (String part : acceptHeader.split(",")) { mediaTypesBuilder.add(MediaType.fromString(part.trim())); } return Collections.unmodifiableList(mediaTypesBuilder); }
/** * Creates a {@code MediaType} by parsing the specified string. The string must look like a standard MIME type * string, for example "text/html" or "application/xml". There can optionally be parameters present, for example * "text/html; encoding=UTF-8" or "application/xml; q=0.8". * * @param text A string representing a media type. * @return A {@code MediaType} object. * @throws java.lang.IllegalArgumentException If the string is not a valid media type string. */ public static MediaType fromString(String text) { Matcher matcher = MEDIA_TYPE_PATTERN.matcher(text); if (!matcher.matches()) { throw new IllegalArgumentException("Invalid media type string: " + text); } String type; String subType; if (matcher.group(GROUP_INDEX) != null) { type = matcher.group(GROUP_INDEX); subType = matcher.group(GROUP_INDEX); } else { type = matcher.group(TYPE_INDEX); subType = matcher.group(SUBTYPE_INDEX); } Map<String, String> parametersBuilder = new HashMap<>(); Matcher parametersMatcher = PARAMETER_PATTERN.matcher(matcher.group(GROUP_MATCHER_INDEX)); while (parametersMatcher.find()) { parametersBuilder.put(parametersMatcher.group(1), parametersMatcher.group(2)); } return new MediaType(type, subType, Collections.unmodifiableMap(parametersBuilder)); }
private Map<String, String> buildRenderedData(ProcessorResult result) throws ODataException { LOG.debug("Start to render the data"); Map<String, String> resultMap = new HashMap<>(); if (result.getStatus().getCode() < ODataResponse.Status.NO_CONTENT.getCode()) { String contentType = result.getHeaders().get(CONTENT_TYPE); if (!result.getHeaders().containsKey(CONTENT_TYPE) || contentType.startsWith(MediaType.ATOM_XML.toString())) { // render XML resultMap.put(FORMAT, MediaType.ATOM_XML.toString()); resultMap.put(BODY, getRenderedXML(result)); } else if (contentType.startsWith(MediaType.JSON.toString())) { // render JSON resultMap.put(FORMAT, MediaType.JSON.toString()); resultMap.put(BODY, getRenderedJSON(result)); } } else { resultMap.put(FORMAT, MediaType.HTTP.getSubType()); resultMap.put(BODY, (String) result.getData()); } return resultMap; }
@Test public void callEndpoint() throws ODataClientException, MalformedURLException { String response = caller.callEndpoint(singletonMap("Accept", JSON.getType()), new URL(basePath + RESPONSE)); assertThat(response, equalTo(URLTestUtils.loadTextFile(RESPONSE))); }
/** * Calculates score based on given media type. * * @param contentTypeFromRequest content type from the ODatRequest * @param expectedTypes is variable arguments * @return integer value which represent the score */ protected int score(MediaType contentTypeFromRequest, MediaType... expectedTypes) { if (contentTypeFromRequest == null) { return DEFAULT_SCORE; } for (MediaType expected : expectedTypes) { if (contentTypeFromRequest.matches(expected)) { return MAXIMUM_FORMAT_SCORE; } } return DEFAULT_SCORE; } }
public List<MediaType> getAccept() { String acceptHeader = getHeader(HeaderNames.ACCEPT); if (isNullOrEmpty(acceptHeader)) { return Collections.emptyList(); } List<MediaType> mediaTypesBuilder = new ArrayList<>(); for (String part : acceptHeader.split(",")) { mediaTypesBuilder.add(MediaType.fromString(part.trim())); } return Collections.unmodifiableList(mediaTypesBuilder); }
/** * Creates a {@code MediaType} by parsing the specified string. The string must look like a standard MIME type * string, for example "text/html" or "application/xml". There can optionally be parameters present, for example * "text/html; encoding=UTF-8" or "application/xml; q=0.8". * * @param text A string representing a media type. * @return A {@code MediaType} object. * @throws java.lang.IllegalArgumentException If the string is not a valid media type string. */ public static MediaType fromString(String text) { Matcher matcher = MEDIA_TYPE_PATTERN.matcher(text); if (!matcher.matches()) { throw new IllegalArgumentException("Invalid media type string: " + text); } String type; String subType; if (matcher.group(GROUP_INDEX) != null) { type = matcher.group(GROUP_INDEX); subType = matcher.group(GROUP_INDEX); } else { type = matcher.group(TYPE_INDEX); subType = matcher.group(SUBTYPE_INDEX); } Map<String, String> parametersBuilder = new HashMap<>(); Matcher parametersMatcher = PARAMETER_PATTERN.matcher(matcher.group(GROUP_MATCHER_INDEX)); while (parametersMatcher.find()) { parametersBuilder.put(parametersMatcher.group(1), parametersMatcher.group(2)); } return new MediaType(type, subType, Collections.unmodifiableMap(parametersBuilder)); }
public Builder setContentType(MediaType mediaType) { this.headersBuilder.put(HeaderNames.CONTENT_TYPE, mediaType.toString()); return this; }
public boolean isWildCardMediaType() { return WILDCARD_ANY.getType().equals(this.type) && WILDCARD_ANY.getSubType().equals(this.subType); }
@Test public void callEndpointWithoutResponse() throws ODataClientException, MalformedURLException { thrown.expect(ODataClientHttpError.class); thrown.expectMessage("Unable to get response from OData service: No Response"); caller.callEndpoint(singletonMap("Accept", JSON.getType()), new URL(basePath)); }
/** * Computes a score by examining a list of media types (typically from the 'Accept' header) against * a required media type. * * @param mediaTypes The list of media types to examine. * @param requiredMediaType The required media type. * @return A score that indicates if one of the media types in the list matches the required media type. */ protected int scoreByMediaType(List<MediaType> mediaTypes, MediaType requiredMediaType) { int score = MAXIMUM_HEADER_SCORE; boolean match = false; boolean matchWildCard = false; for (MediaType mediaType : mediaTypes) { if (mediaType.matches(requiredMediaType)) { if (mediaType.isWildCardMediaType()) { matchWildCard = true; } match = true; break; } // Lower the score for each subsequent possible match score -= 2; } return match && !matchWildCard ? score : matchWildCard ? WILDCARD_MATCH_SCORE : DEFAULT_SCORE; }
/** * Computes a score by checking the value of the '$format' parameter (if present) against a required media type. * * @param formatOption The option containing the '$format' parameter. * @param requiredMediaType The required media type. * @return A score that indicates if the media type present in the '$format' parameter * matches the required media type. */ protected int scoreByFormat(Option<FormatOption> formatOption, MediaType requiredMediaType) { if (!formatOption.isDefined()) { return DEFAULT_SCORE; } if (formatOption.get().mediaType().matches(requiredMediaType)) { return MAXIMUM_FORMAT_SCORE; } return DEFAULT_SCORE; }
public MediaType getContentType() { String contentTypeHeader = getHeader(HeaderNames.CONTENT_TYPE); return (isNullOrEmpty(contentTypeHeader)) ? null : MediaType.fromString(contentTypeHeader); }
public Builder setContentType(MediaType contentType) { this.headersMap.put(HeaderNames.CONTENT_TYPE, contentType.toString()); return this; }