List of usage examples for org.apache.http.entity ContentType get
public static ContentType get(HttpEntity httpEntity) throws ParseException, UnsupportedCharsetException
From source file:cn.wanghaomiao.seimi.core.SeimiProcessor.java
private Response renderResponse(HttpResponse httpResponse, Request request, HttpContext httpContext) { Response seimiResponse = new Response(); HttpEntity entity = httpResponse.getEntity(); seimiResponse.setHttpResponse(httpResponse); seimiResponse.setReponseEntity(entity); seimiResponse.setRealUrl(getRealUrl(httpContext)); seimiResponse.setUrl(request.getUrl()); seimiResponse.setRequest(request);//www. j a v a 2 s.c om seimiResponse.setMeta(request.getMeta()); if (entity != null) { Header referer = httpResponse.getFirstHeader("Referer"); if (referer != null) { seimiResponse.setReferer(referer.getValue()); } if (!entity.getContentType().getValue().contains("image")) { seimiResponse.setBodyType(BodyType.TEXT); try { seimiResponse.setData(EntityUtils.toByteArray(entity)); ContentType contentType = ContentType.get(entity); Charset charset = contentType.getCharset(); if (charset == null) { seimiResponse.setContent(new String(seimiResponse.getData(), "ISO-8859-1")); String docCharset = renderRealCharset(seimiResponse); seimiResponse.setContent( new String(seimiResponse.getContent().getBytes("ISO-8859-1"), docCharset)); seimiResponse.setCharset(docCharset); } else { seimiResponse.setContent(new String(seimiResponse.getData(), charset)); seimiResponse.setCharset(charset.name()); } } catch (Exception e) { e.printStackTrace(); logger.error("no content data"); } } else { seimiResponse.setBodyType(BodyType.BINARY); try { seimiResponse.setData(EntityUtils.toByteArray(entity)); seimiResponse.setContent(StringUtils.substringAfterLast(request.getUrl(), "/")); } catch (Exception e) { logger.error("no data can be read from httpResponse"); } } } return seimiResponse; }
From source file:com.serphacker.serposcope.scraper.http.ScrapClient.java
public Charset getDetectedCharset() { ContentType contentType = null;//from w w w .j av a 2 s .c o m try { contentType = ContentType.get(response.getEntity()); } catch (Exception ex) { } Charset charset = null; if (contentType != null) { try { charset = contentType.getCharset(); } catch (final Exception ex) { } if (charset == null) { if (contentType.getMimeType().contains("text/html")) { charset = detectCharsetFromHtmlMeta(); } } } return charset; }
From source file:at.bitfire.davdroid.webdav.WebDavResource.java
public void get(String acceptedMimeTypes) throws URISyntaxException, IOException, HttpException, DavException { HttpGetHC4 get = new HttpGetHC4(location); get.addHeader("Accept", acceptedMimeTypes); @Cleanup/* w w w. j a v a 2 s . c o m*/ CloseableHttpResponse response = httpClient.execute(get, context); checkResponse(response); HttpEntity entity = response.getEntity(); if (entity == null) throw new DavNoContentException(); properties.contentType = ContentType.get(entity); content = EntityUtilsHC4.toByteArray(entity); }
From source file:org.esigate.Driver.java
/** * Performs rendering on an HttpResponse. * <p>//from w w w . ja v a 2 s. c o m * Rendering is only performed if page can be parsed. * * @param pageUrl * The remove url from which the body was retrieved. * @param originalRequest * The request received by esigate. * @param response * The response which will be rendered. * @param renderers * list of renderers to apply. * @return The rendered response, or the original response if if was not parsed. * @throws HttpErrorPage * @throws IOException */ private CloseableHttpResponse performRendering(String pageUrl, DriverRequest originalRequest, CloseableHttpResponse response, Renderer[] renderers) throws HttpErrorPage, IOException { if (!contentTypeHelper.isTextContentType(response)) { LOG.debug("'{}' is binary on no transformation to apply: was forwarded without modification.", pageUrl); return response; } LOG.debug("'{}' is text : will apply renderers.", pageUrl); // Get response body String currentValue = HttpResponseUtils.toString(response, this.eventManager); // Perform rendering currentValue = performRendering(pageUrl, originalRequest, response, currentValue, renderers); // Generate the new response. HttpEntity transformedHttpEntity = new StringEntity(currentValue, ContentType.get(response.getEntity())); CloseableHttpResponse transformedResponse = BasicCloseableHttpResponse .adapt(new BasicHttpResponse(response.getStatusLine())); transformedResponse.setHeaders(response.getAllHeaders()); transformedResponse.setEntity(transformedHttpEntity); return transformedResponse; }
From source file:com.sonyericsson.hudson.plugins.gerrit.trigger.playback.GerritMissedEventsPlaybackManager.java
/** * * @param config Gerrit config for server. * @param url URL to use./*from ww w. j ava2 s. c o m*/ * @return String of gerrit events. */ protected String getEventsFromEventsLogPlugin(IGerritHudsonTriggerConfig config, String url) { logger.debug("({}) Going to GET: {}", serverName, url); HttpResponse execute = null; try { execute = HttpUtils.performHTTPGet(config, url); } catch (IOException e) { logger.warn(e.getMessage(), e); return ""; } int statusCode = execute.getStatusLine().getStatusCode(); logger.debug("Received status code: {} for server: {}", statusCode, serverName); if (statusCode == HttpURLConnection.HTTP_OK) { try { HttpEntity entity = execute.getEntity(); if (entity != null) { ContentType contentType = ContentType.get(entity); if (contentType == null) { contentType = ContentType.DEFAULT_TEXT; } Charset charset = contentType.getCharset(); if (charset == null) { charset = Charset.defaultCharset(); } InputStream bodyStream = entity.getContent(); String body = IOUtils.toString(bodyStream, charset.name()); logger.debug(body); return body; } } catch (IOException ioe) { logger.warn(ioe.getMessage(), ioe); } } logger.warn("Not successful at requesting missed events from {} plugin. (errorcode: {})", EVENTS_LOG_PLUGIN_NAME, statusCode); return ""; }
From source file:at.bitfire.davdroid.webdav.WebDavResource.java
/** * Process a 207 Multi-status response as defined in RFC 4918 "13. Multi-Status Response" *//* w w w. j a v a2s.co m*/ protected void processMultiStatus(HttpResponse response) throws IOException, HttpException, DavException { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_MULTI_STATUS) throw new DavNoMultiStatusException(); HttpEntity entity = response.getEntity(); if (entity == null) throw new DavNoContentException(); properties.contentType = ContentType.get(entity); @Cleanup InputStream content = entity.getContent(); DavMultistatus multiStatus; try { Serializer serializer = new Persister(); multiStatus = serializer.read(DavMultistatus.class, content, false); } catch (Exception ex) { throw new DavException("Couldn't parse Multi-Status response on REPORT multi-get", ex); } if (multiStatus.response == null) // empty response return; // member list will be built from response List<WebDavResource> members = new LinkedList<>(); // iterate through all resources (either ourselves or member) for (DavResponse singleResponse : multiStatus.response) { URI href; try { href = location.resolve(URIUtils.parseURI(singleResponse.href.href, false)); } catch (Exception ex) { Log.w(TAG, "Ignoring illegal member URI in multi-status response", ex); continue; } Log.d(TAG, "Processing multi-status element: " + href); // process known properties Properties properties = new Properties(); byte[] data = null; // in <response>, either <status> or <propstat> must be present if (singleResponse.status != null) { // method 1 (status of resource as a whole) StatusLine status = BasicLineParserHC4.parseStatusLine(singleResponse.status, new BasicLineParserHC4()); checkResponse(status); } else for (DavPropstat singlePropstat : singleResponse.propstat) { // method 2 (propstat) StatusLine status = BasicLineParserHC4.parseStatusLine(singlePropstat.status, new BasicLineParserHC4()); // ignore information about missing properties etc. if (status.getStatusCode() / 100 != 1 && status.getStatusCode() / 100 != 2) continue; DavProp prop = singlePropstat.prop; properties.process(prop); if (prop.calendarData != null && prop.calendarData.ical != null) data = prop.calendarData.ical.getBytes(); else if (prop.addressData != null && prop.addressData.vcard != null) data = prop.addressData.vcard.getBytes(); } // about which resource is this response? if (properties.isCollection) // ensure trailing slashs for collections href = URIUtils.ensureTrailingSlash(href); if (location.equals(href) || URIUtils.ensureTrailingSlash(location).equals(href)) { // about ourselves this.properties = properties; this.content = data; } else { // about a member WebDavResource member = new WebDavResource(this, href); member.properties = properties; member.content = data; members.add(member); } } this.members = members; }
From source file:org.dcache.srm.client.HttpClientSender.java
private static String getMimeType(HttpEntity entity) { ContentType contentType = ContentType.get(entity); return (contentType == null) ? null : contentType.getMimeType(); }
From source file:org.dcache.srm.client.HttpClientSender.java
/** * Extracts the SOAP response from an HttpResponse. *//*from w w w. j ava2s . c o m*/ protected Message extractResponse(MessageContext msgContext, HttpResponse response) throws IOException { int returnCode = response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); if (entity != null && returnCode > 199 && returnCode < 300) { // SOAP return is OK - so fall through } else if (entity != null && msgContext.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS) { // For now, if we're SOAP 1.2, fall through, since the range of // valid result codes is much greater } else if (entity != null && returnCode > 499 && returnCode < 600 && Objects.equals(getMimeType(entity), "text/xml")) { // SOAP Fault should be in here - so fall through } else { String statusMessage = response.getStatusLine().getReasonPhrase(); AxisFault fault = new AxisFault("HTTP", "(" + returnCode + ")" + statusMessage, null, null); fault.setFaultDetailString("Return code: " + String.valueOf(returnCode) + (entity == null ? "" : "\n" + EntityUtils.toString(entity))); fault.addFaultDetail(Constants.QNAME_FAULTDETAIL_HTTPERRORCODE, String.valueOf(returnCode)); throw fault; } Header contentLocation = response.getFirstHeader(HttpHeaders.CONTENT_LOCATION); Message outMsg = new Message(entity.getContent(), false, Objects.toString(ContentType.get(entity), null), (contentLocation == null) ? null : contentLocation.getValue()); // Transfer HTTP headers of HTTP message to MIME headers of SOAP message MimeHeaders responseMimeHeaders = outMsg.getMimeHeaders(); for (Header responseHeader : response.getAllHeaders()) { responseMimeHeaders.addHeader(responseHeader.getName(), responseHeader.getValue()); } outMsg.setMessageType(Message.RESPONSE); return outMsg; }
From source file:com.mirth.connect.connectors.http.HttpDispatcher.java
@Override public Response send(ConnectorProperties connectorProperties, ConnectorMessage connectorMessage) { HttpDispatcherProperties httpDispatcherProperties = (HttpDispatcherProperties) connectorProperties; eventController.dispatchEvent(new ConnectionStatusEvent(getChannelId(), getMetaDataId(), getDestinationName(), ConnectionStatusEventType.WRITING)); String responseData = null;/* ww w. j a v a 2s . c o m*/ String responseError = null; String responseStatusMessage = null; Status responseStatus = Status.QUEUED; boolean validateResponse = false; CloseableHttpClient client = null; HttpRequestBase httpMethod = null; CloseableHttpResponse httpResponse = null; File tempFile = null; int socketTimeout = NumberUtils.toInt(httpDispatcherProperties.getSocketTimeout(), 30000); try { configuration.configureDispatcher(this, httpDispatcherProperties); long dispatcherId = getDispatcherId(); client = clients.get(dispatcherId); if (client == null) { BasicHttpClientConnectionManager httpClientConnectionManager = new BasicHttpClientConnectionManager( socketFactoryRegistry.build()); httpClientConnectionManager .setSocketConfig(SocketConfig.custom().setSoTimeout(socketTimeout).build()); HttpClientBuilder clientBuilder = HttpClients.custom() .setConnectionManager(httpClientConnectionManager); HttpUtil.configureClientBuilder(clientBuilder); if (httpDispatcherProperties.isUseProxyServer()) { clientBuilder.setRoutePlanner(new DynamicProxyRoutePlanner()); } client = clientBuilder.build(); clients.put(dispatcherId, client); } URI hostURI = new URI(httpDispatcherProperties.getHost()); String host = hostURI.getHost(); String scheme = hostURI.getScheme(); int port = hostURI.getPort(); if (port == -1) { if (scheme.equalsIgnoreCase("https")) { port = 443; } else { port = 80; } } // Parse the content type field first, and then add the charset if needed ContentType contentType = ContentType.parse(httpDispatcherProperties.getContentType()); Charset charset = null; if (contentType.getCharset() == null) { charset = Charset.forName(CharsetUtils.getEncoding(httpDispatcherProperties.getCharset())); } else { charset = contentType.getCharset(); } if (httpDispatcherProperties.isMultipart()) { tempFile = File.createTempFile(UUID.randomUUID().toString(), ".tmp"); } HttpHost target = new HttpHost(host, port, scheme); httpMethod = buildHttpRequest(hostURI, httpDispatcherProperties, connectorMessage, tempFile, contentType, charset); HttpClientContext context = HttpClientContext.create(); // authentication if (httpDispatcherProperties.isUseAuthentication()) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM); Credentials credentials = new UsernamePasswordCredentials(httpDispatcherProperties.getUsername(), httpDispatcherProperties.getPassword()); credsProvider.setCredentials(authScope, credentials); AuthCache authCache = new BasicAuthCache(); RegistryBuilder<AuthSchemeProvider> registryBuilder = RegistryBuilder.<AuthSchemeProvider>create(); if (AuthSchemes.DIGEST.equalsIgnoreCase(httpDispatcherProperties.getAuthenticationType())) { logger.debug("using Digest authentication"); registryBuilder.register(AuthSchemes.DIGEST, new DigestSchemeFactory(charset)); if (httpDispatcherProperties.isUsePreemptiveAuthentication()) { processDigestChallenge(authCache, target, credentials, httpMethod, context); } } else { logger.debug("using Basic authentication"); registryBuilder.register(AuthSchemes.BASIC, new BasicSchemeFactory(charset)); if (httpDispatcherProperties.isUsePreemptiveAuthentication()) { authCache.put(target, new BasicScheme()); } } context.setCredentialsProvider(credsProvider); context.setAuthSchemeRegistry(registryBuilder.build()); context.setAuthCache(authCache); logger.debug("using authentication with credentials: " + credentials); } RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(socketTimeout) .setSocketTimeout(socketTimeout).setStaleConnectionCheckEnabled(true).build(); context.setRequestConfig(requestConfig); // Set proxy information if (httpDispatcherProperties.isUseProxyServer()) { context.setAttribute(PROXY_CONTEXT_KEY, new HttpHost(httpDispatcherProperties.getProxyAddress(), Integer.parseInt(httpDispatcherProperties.getProxyPort()))); } // execute the method logger.debug( "executing method: type=" + httpMethod.getMethod() + ", uri=" + httpMethod.getURI().toString()); httpResponse = client.execute(target, httpMethod, context); StatusLine statusLine = httpResponse.getStatusLine(); int statusCode = statusLine.getStatusCode(); logger.debug("received status code: " + statusCode); Map<String, List<String>> headers = new HashMap<String, List<String>>(); for (Header header : httpResponse.getAllHeaders()) { List<String> list = headers.get(header.getName()); if (list == null) { list = new ArrayList<String>(); headers.put(header.getName(), list); } list.add(header.getValue()); } connectorMessage.getConnectorMap().put("responseStatusLine", statusLine.toString()); connectorMessage.getConnectorMap().put("responseHeaders", new MessageHeaders(new CaseInsensitiveMap(headers))); ContentType responseContentType = ContentType.get(httpResponse.getEntity()); if (responseContentType == null) { responseContentType = ContentType.TEXT_PLAIN; } Charset responseCharset = charset; if (responseContentType.getCharset() != null) { responseCharset = responseContentType.getCharset(); } final String responseBinaryMimeTypes = httpDispatcherProperties.getResponseBinaryMimeTypes(); BinaryContentTypeResolver binaryContentTypeResolver = new BinaryContentTypeResolver() { @Override public boolean isBinaryContentType(ContentType contentType) { return HttpDispatcher.this.isBinaryContentType(responseBinaryMimeTypes, contentType); } }; /* * First parse out the body of the HTTP response. Depending on the connector settings, * this could end up being a string encoded with the response charset, a byte array * representing the raw response payload, or a MimeMultipart object. */ Object responseBody = ""; // The entity could be null in certain cases such as 204 responses if (httpResponse.getEntity() != null) { // Only parse multipart if XML Body is selected and Parse Multipart is enabled if (httpDispatcherProperties.isResponseXmlBody() && httpDispatcherProperties.isResponseParseMultipart() && responseContentType.getMimeType().startsWith(FileUploadBase.MULTIPART)) { responseBody = new MimeMultipart(new ByteArrayDataSource(httpResponse.getEntity().getContent(), responseContentType.toString())); } else if (binaryContentTypeResolver.isBinaryContentType(responseContentType)) { responseBody = IOUtils.toByteArray(httpResponse.getEntity().getContent()); } else { responseBody = IOUtils.toString(httpResponse.getEntity().getContent(), responseCharset); } } /* * Now that we have the response body, we need to create the actual Response message * data. Depending on the connector settings this could be our custom serialized XML, a * Base64 string encoded from the raw response payload, or a string encoded from the * payload with the request charset. */ if (httpDispatcherProperties.isResponseXmlBody()) { responseData = HttpMessageConverter.httpResponseToXml(statusLine.toString(), headers, responseBody, responseContentType, httpDispatcherProperties.isResponseParseMultipart(), httpDispatcherProperties.isResponseIncludeMetadata(), binaryContentTypeResolver); } else if (responseBody instanceof byte[]) { responseData = new String(Base64Util.encodeBase64((byte[]) responseBody), "US-ASCII"); } else { responseData = (String) responseBody; } validateResponse = httpDispatcherProperties.getDestinationConnectorProperties().isValidateResponse(); if (statusCode < HttpStatus.SC_BAD_REQUEST) { responseStatus = Status.SENT; } else { eventController.dispatchEvent(new ErrorEvent(getChannelId(), getMetaDataId(), connectorMessage.getMessageId(), ErrorEventType.DESTINATION_CONNECTOR, getDestinationName(), connectorProperties.getName(), "Received error response from HTTP server.", null)); responseStatusMessage = ErrorMessageBuilder .buildErrorResponse("Received error response from HTTP server.", null); responseError = ErrorMessageBuilder.buildErrorMessage(connectorProperties.getName(), responseData, null); } } catch (Exception e) { eventController.dispatchEvent(new ErrorEvent(getChannelId(), getMetaDataId(), connectorMessage.getMessageId(), ErrorEventType.DESTINATION_CONNECTOR, getDestinationName(), connectorProperties.getName(), "Error connecting to HTTP server.", e)); responseStatusMessage = ErrorMessageBuilder.buildErrorResponse("Error connecting to HTTP server", e); responseError = ErrorMessageBuilder.buildErrorMessage(connectorProperties.getName(), "Error connecting to HTTP server", e); } finally { try { HttpClientUtils.closeQuietly(httpResponse); // Delete temp files if we created them if (tempFile != null) { tempFile.delete(); tempFile = null; } } finally { eventController.dispatchEvent(new ConnectionStatusEvent(getChannelId(), getMetaDataId(), getDestinationName(), ConnectionStatusEventType.IDLE)); } } return new Response(responseStatus, responseData, responseStatusMessage, responseError, validateResponse); }