Java Code Examples for com.google.api.client.http.HttpResponse#getContentType()
The following examples show how to use
com.google.api.client.http.HttpResponse#getContentType() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TokenResponseException.java From google-oauth-java-client with Apache License 2.0 | 5 votes |
/** * Returns a new instance of {@link TokenResponseException}. * * <p> * If there is a JSON error response, it is parsed using {@link TokenErrorResponse}, which can be * inspected using {@link #getDetails()}. Otherwise, the full response content is read and * included in the exception message. * </p> * * @param jsonFactory JSON factory * @param response HTTP response * @return new instance of {@link TokenErrorResponse} */ public static TokenResponseException from(JsonFactory jsonFactory, HttpResponse response) { HttpResponseException.Builder builder = new HttpResponseException.Builder( response.getStatusCode(), response.getStatusMessage(), response.getHeaders()); // details Preconditions.checkNotNull(jsonFactory); TokenErrorResponse details = null; String detailString = null; String contentType = response.getContentType(); try { if (!response.isSuccessStatusCode() && contentType != null && response.getContent() != null && HttpMediaType.equalsIgnoreParameters(Json.MEDIA_TYPE, contentType)) { details = new JsonObjectParser(jsonFactory).parseAndClose( response.getContent(), response.getContentCharset(), TokenErrorResponse.class); detailString = details.toPrettyString(); } else { detailString = response.parseAsString(); } } catch (IOException exception) { // it would be bad to throw an exception while throwing an exception exception.printStackTrace(); } // message StringBuilder message = HttpResponseException.computeMessageBuffer(response); if (!com.google.api.client.util.Strings.isNullOrEmpty(detailString)) { message.append(StringUtils.LINE_SEPARATOR).append(detailString); builder.setContent(detailString); } builder.setMessage(message.toString()); return new TokenResponseException(builder, details); }
Example 2
Source File: HttpHandler.java From googleads-java-lib with Apache License 2.0 | 5 votes |
/** * Returns a new Axis Message based on the contents of the HTTP response. * * @param httpResponse the HTTP response * @return an Axis Message for the HTTP response * @throws IOException if unable to retrieve the HTTP response's contents * @throws AxisFault if the HTTP response's status or contents indicate an unexpected error, such * as a 405. */ private Message createResponseMessage(HttpResponse httpResponse) throws IOException, AxisFault { int statusCode = httpResponse.getStatusCode(); String contentType = httpResponse.getContentType(); // The conditions below duplicate the logic in CommonsHTTPSender and HTTPSender. boolean shouldParseResponse = (statusCode > 199 && statusCode < 300) || (contentType != null && !contentType.equals("text/html") && statusCode > 499 && statusCode < 600); // Wrap the content input stream in a notifying stream so the stream event listener will be // notified when it is closed. InputStream responseInputStream = new NotifyingInputStream(httpResponse.getContent(), inputStreamEventListener); if (!shouldParseResponse) { // The contents are not an XML response, so throw an AxisFault with // the HTTP status code and message details. String statusMessage = httpResponse.getStatusMessage(); AxisFault axisFault = new AxisFault("HTTP", "(" + statusCode + ")" + statusMessage, null, null); axisFault.addFaultDetail( Constants.QNAME_FAULTDETAIL_HTTPERRORCODE, String.valueOf(statusCode)); try (InputStream stream = responseInputStream) { byte[] contentBytes = ByteStreams.toByteArray(stream); axisFault.setFaultDetailString( Messages.getMessage( "return01", String.valueOf(statusCode), new String(contentBytes, UTF_8))); } throw axisFault; } // Response is an XML response. Do not consume and close the stream in this case, since that // will happen later when the response is deserialized by Axis (as confirmed by unit tests for // this class). Message responseMessage = new Message( responseInputStream, false, contentType, httpResponse.getHeaders().getLocation()); responseMessage.setMessageType(Message.RESPONSE); return responseMessage; }
Example 3
Source File: LenientTokenResponseException.java From android-oauth-client with Apache License 2.0 | 5 votes |
/** * Returns a new instance of {@link LenientTokenResponseException}. * <p> * If there is a JSON error response, it is parsed using * {@link TokenErrorResponse}, which can be inspected using * {@link #getDetails()}. Otherwise, the full response content is read and * included in the exception message. * </p> * * @param jsonFactory JSON factory * @param readResponse an HTTP response that has already been read * @param responseContent the content String of the HTTP response * @return new instance of {@link TokenErrorResponse} */ public static LenientTokenResponseException from(JsonFactory jsonFactory, HttpResponse readResponse, String responseContent) { HttpResponseException.Builder builder = new HttpResponseException.Builder( readResponse.getStatusCode(), readResponse.getStatusMessage(), readResponse.getHeaders()); // details Preconditions.checkNotNull(jsonFactory); TokenErrorResponse details = null; String detailString = null; String contentType = readResponse.getContentType(); try { if (/* !response.isSuccessStatusCode() && */true && contentType != null && HttpMediaType.equalsIgnoreParameters(Json.MEDIA_TYPE, contentType)) { details = readResponse .getRequest() .getParser() .parseAndClose(new StringReader(responseContent), TokenErrorResponse.class); detailString = details.toPrettyString(); } else { detailString = responseContent; } } catch (IOException exception) { // it would be bad to throw an exception while throwing an exception exception.printStackTrace(); } // message StringBuilder message = HttpResponseException.computeMessageBuffer(readResponse); if (!com.google.api.client.util.Strings.isNullOrEmpty(detailString)) { message.append(StringUtils.LINE_SEPARATOR).append(detailString); builder.setContent(detailString); } builder.setMessage(message.toString()); return new LenientTokenResponseException(builder, details); }