public ContentTypeHeader getContentTypeHeader() { HttpHeader header = getHeader(ContentTypeHeader.KEY); if (header.isPresent()) { return new ContentTypeHeader(header.firstValue()); } return ContentTypeHeader.absent(); }
public static String determineFileExtension(String url, ContentTypeHeader contentTypeHeader, byte[] responseBody) { if (contentTypeHeader.isPresent()) { if (contentTypeHeader.mimeTypePart().contains("json")) { return "json"; } if (contentTypeHeader.mimeTypePart().contains("xml")) { return "xml"; } if (contentTypeHeader.mimeTypePart().contains("text")) { return "txt"; } String extension = COMMON_MIME_TYPES.get(contentTypeHeader.mimeTypePart()); if (extension != null) { return extension; } } String path = URI.create(url).getPath(); String lastPathSegment = substringAfterLast(path, "/"); if (lastPathSegment.indexOf('.') != -1) { return substringAfterLast(lastPathSegment, "."); } return determineTextFileExtension(stringFromBytes(responseBody, contentTypeHeader.charset())); }
public Charset charset() { if (isPresent() && encodingPart().isPresent()) { return Charset.forName(encodingPart().get()); } return Strings.DEFAULT_CHARSET; } }
public ContentTypeHeader or(String stringValue) { return isPresent() ? this : new ContentTypeHeader(stringValue); }
private static HttpEntity buildEntityFrom(Request originalRequest) { ContentTypeHeader contentTypeHeader = originalRequest.contentTypeHeader().or("text/plain"); ContentType contentType = ContentType.create(contentTypeHeader.mimeTypePart(), contentTypeHeader.encodingPart().or("utf-8")); if (originalRequest.containsHeader(TRANSFER_ENCODING) && originalRequest.header(TRANSFER_ENCODING).firstValue().equals("chunked")) { return applyGzipWrapperIfRequired( originalRequest, new InputStreamEntity(new ByteArrayInputStream(originalRequest.getBody()), -1, contentType) ); } return applyGzipWrapperIfRequired( originalRequest, new ByteArrayEntity(originalRequest.getBody()) ); }
@JsonIgnore public String getMimeType() { return headers == null || headers.getContentTypeHeader() == null ? OCTET_STREAM.toString() : headers.getContentTypeHeader().mimeTypePart(); }
private Charset encodingFromContentTypeHeaderOrUtf8() { ContentTypeHeader contentTypeHeader = contentTypeHeader(); if (contentTypeHeader != null) { return contentTypeHeader.charset(); } return UTF_8; }
public static ContentTypeHeader absent() { return new ContentTypeHeader(); }
String extension = ContentTypes.determineFileExtension( stubMapping.getRequest().getUrl(), responseHeaders != null ? responseHeaders.getContentTypeHeader() : ContentTypeHeader.absent(), body);
public static Body ofBinaryOrText(byte[] content, ContentTypeHeader contentTypeHeader) { return new Body(content, ContentTypes.determineIsTextFromMimeType(contentTypeHeader.mimeTypePart())); }
private Charset encodingFromContentTypeHeaderOrUtf8() { ContentTypeHeader contentTypeHeader = contentTypeHeader(); if (contentTypeHeader != null) { return contentTypeHeader.charset(); } return UTF_8; }
@Override public Body getBody() { try { byte[] bytes = ByteStreams.toByteArray(mPart.getInputStream()); HttpHeader header = getHeader(ContentTypeHeader.KEY); ContentTypeHeader contentTypeHeader = header.isPresent() ? new ContentTypeHeader(header.firstValue()) : ContentTypeHeader.absent(); return Body.ofBinaryOrText(bytes, contentTypeHeader); } catch (IOException e) { return throwUnchecked(e, Body.class); } }
private String getMimeType(StubMapping mapping) { HttpHeaders responseHeaders = mapping.getResponse().getHeaders(); if (responseHeaders != null) { ContentTypeHeader contentTypeHeader = responseHeaders.getContentTypeHeader(); return contentTypeHeader != null ? contentTypeHeader.mimeTypePart() : null; } return null; }
@JsonIgnore public Charset getCharset() { return headers == null ? Strings.DEFAULT_CHARSET : headers.getContentTypeHeader().charset(); }
/** * If request body was JSON or XML, use "equalToJson" or "equalToXml" (respectively) in the RequestPattern so it's * easier to read. Otherwise, just use "equalTo" */ @Override public ContentPattern<?> forRequest(Request request) { final String mimeType = request.getHeaders().getContentTypeHeader().mimeTypePart(); if (mimeType != null) { if (mimeType.contains("json")) { return new EqualToJsonPattern(request.getBodyAsString(), ignoreArrayOrder, ignoreExtraElements); } else if (mimeType.contains("xml")) { return new EqualToXmlPattern(request.getBodyAsString()); } else if (mimeType.equals("multipart/form-data")) { // TODO: Need to add a matcher that can handle multipart data properly. For now, just always match return new AnythingPattern(); } else if (!determineIsTextFromMimeType(mimeType)) { return new BinaryEqualToPattern(request.getBody()); } } return new EqualToPattern(request.getBodyAsString(), caseInsensitive); } }
public Response(int status, String statusMessage, String body, HttpHeaders headers, boolean configured, Fault fault, long initialDelay, ChunkedDribbleDelay chunkedDribbleDelay, boolean fromProxy) { this.status = status; this.statusMessage = statusMessage; this.headers = headers; this.bodyStreamSource = StreamSources.forString(body, headers.getContentTypeHeader().charset()); this.configured = configured; this.fault = fault; this.initialDelay = initialDelay; this.chunkedDribbleDelay = chunkedDribbleDelay; this.fromProxy = fromProxy; }
@Override public MatchResult match(ResponseDefinition responseDefinition) { if (!responseDefinition.specifiesBodyContent()) { return MatchResult.noMatch(); } else if ( responseDefinition.getHeaders() != null && ContentTypes.determineIsTextFromMimeType(responseDefinition.getHeaders().getContentTypeHeader().mimeTypePart()) ) { if (responseDefinition.getBody().length() > textSizeThreshold) { return MatchResult.exactMatch(); } else { return MatchResult.noMatch(); } } else { if (responseDefinition.getByteBody().length > binarySizeThreshold) { return MatchResult.exactMatch(); } else { return MatchResult.noMatch(); } } }
public String getBodyAsString() { return Strings.stringFromBytes(getBody(), headers.getContentTypeHeader().charset()); }