Why BasicResponseHandler doesn't consume response entity after successfully get the response string - httpclient

I face a ConnectionPoolTimeOutException recently, and I am wondering if it relates to my response handler. If response entity is resource and should be freed as soon as it is not needed, why the Apache BasicResponseHandler does not consume entity before return response string?
#Immutable
public class BasicResponseHandler implements ResponseHandler<String> {
/**
* Returns the response body as a String if the response was successful (a
* 2xx status code). If no response body exists, this returns null. If the
* response was unsuccessful (>= 300 status code), throws an
* {#link HttpResponseException}.
*/
public String handleResponse(final HttpResponse response)
throws HttpResponseException, IOException {
StatusLine statusLine = response.getStatusLine();
HttpEntity entity = response.getEntity();
if (statusLine.getStatusCode() >= 300) {
EntityUtils.consume(entity);
throw new HttpResponseException(statusLine.getStatusCode(),
statusLine.getReasonPhrase());
}
//Why not this:
//EntityUtils.consume(entity);
//String responseStr = entity == null ? null : EntityUtils.toString(entity);
//return responseStr;
return entity == null ? null : EntityUtils.toString(entity);
}
}

Because it is taken care of by CloseableHttpClient

BasicResponseHandler:
if status is greater than 300
invokes EntityUtils.consume();
else
invokes EntityUtils.toString();
If you look at those two methods, both of them closes the Inputstream as shown below.
public final class EntityUtils {
public static void consume(HttpEntity entity) throws IOException {
// ..
InputStream instream = entity.getContent();
// ..
instream.close();
//..
}
public static String toString(HttpEntity entity, String defaultCharset) throws IOException, ParseException {
// ..
InputStream instream = entity.getContent();
// ..
instream.close();
return var9;
}
}

Related

how to set response value in springcloud gateway with GlobalFilter

how to set response value in springcloud gateway with GlobalFilter?
I don't know use which method in ServerHttpResponse.
public class AuthGlobalFilterFilter1 implements GlobalFilter, Ordered {
#Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
Object key = exchange.getRequest().getQueryParams().get("key");
if(key==null){
ServerHttpResponse serverHttpResponse = exchange.getResponse();
serverHttpResponse.setStatusCode(HttpStatus.UNAUTHORIZED);
//in here,I want set the value of response body.such as json string "{"status":401}"
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
I have solved it.
ServerHttpResponse serverHttpResponse = exchange.getResponse();
serverHttpResponse.setStatusCode(HttpStatus.OK);
byte[] response = "{\"status\":\"erroe\",\"message\":\"error happen\"}".getBytes(StandardCharsets.UTF_8);;
DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(response);
return exchange.getResponse().writeWith(Flux.just(buffer));

Apache HttpClient - REST API: Issue in converting response to customized object which is put as SerializableEntity

I am using Apache HttpClient to put/get customized object using REST APIs. Below is the sample code. My putObject() method works fine and I could serialize Person object and put properly. However, while getting the object, I got below error:
Exception in thread "main" java.lang.ClassCastException: [B cannot be cast to Person at MyTest.demoGetRESTAPI(MyTest.java:88) at MyTest.main(MyTest.java:21)
Seems the code to build Person object out of response entity is not correct
HttpEntity httpEntity = response.getEntity();
byte[] resultByteArray = EntityUtils.toByteArray(httpEntity);
Person person = (Person)SerializationUtils.deserialize(resultByteArray);
Am I doing somthing wrong while getting byte[] array and converting to Person object. Please help me out to solve this issue.
Complete Example Program:
import java.io.Serializable;
import org.apache.commons.lang.SerializationUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.SerializableEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class MyTest {
public static void main(String[] args) throws Exception {
putObject();
getObject();
}
public static void putObject() throws Exception
{
HttpClient httpClient = new DefaultHttpClient();
Person person = new Person();
person.setName("Narendra");
person.setId("1");
try
{
//Define a postRequest request
HttpPut putRequest = new HttpPut("http://localhost:9084/ehcache-server/rest/screeningInstance/2221");
//Set the API media type in http content-type header
putRequest.addHeader("content-type", "application/x-java-serialized-object");
//Set the request put body
SerializableEntity personSEntity = new SerializableEntity(SerializationUtils.serialize(person));
putRequest.setEntity(personSEntity);
//Send the request; It will immediately return the response in HttpResponse object if any
HttpResponse response = httpClient.execute(putRequest);
//verify the valid error code first
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 201)
{
throw new RuntimeException("Failed with HTTP error code : " + statusCode);
}
}
finally
{
//Important: Close the connect
httpClient.getConnectionManager().shutdown();
}
}
public static void getObject() throws Exception
{
DefaultHttpClient httpClient = new DefaultHttpClient();
try
{
//Define a HttpGet request; You can choose between HttpPost, HttpDelete or HttpPut also.
//Choice depends on type of method you will be invoking.
HttpGet getRequest = new HttpGet("http://localhost:9084/ehcache-server/rest/screeningInstance/2221");
//Set the API media type in http accept header
getRequest.addHeader("accept", "application/x-java-serialized-object");
//Send the request; It will immediately return the response in HttpResponse object
HttpResponse response = httpClient.execute(getRequest);
//verify the valid error code first
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200)
{
throw new RuntimeException("Failed with HTTP error code : " + statusCode);
}
//Now pull back the response object
HttpEntity httpEntity = response.getEntity();
byte[] resultByteArray = EntityUtils.toByteArray(httpEntity);
Person person = (Person)SerializationUtils.deserialize(resultByteArray);
}
finally
{
//Important: Close the connect
httpClient.getConnectionManager().shutdown();
}
}
}
class Person implements Serializable{
String name;
String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#Override
public String toString() {
return "Person [name=" + name + ", id=" + id + "]";
}
}
I got the solution. It was mistake in my code:
While putting object, I have written below code. That was doing two time serialization. First from Person object to byte[] and second from byte[] to byte[].
SerializableEntity personSEntity = new SerializableEntity(SerializationUtils.serialize(person));
putRequest.setEntity(personSEntity);
This is the right approach:
SerializableEntity personSEntity = new SerializableEntity(person);
putRequest.setEntity(personSEntity);
After getting binary from REST, code should be like below to get Object:
HttpEntity httpEntity = response.getEntity();
InputStream inputStream = null;
try {
inputStream = httpEntity.getContent();
Person p = (Person) SerializationUtils.deserialize(inputStream);
System.out.println("Person:" + p.getName());
}
finally {
inputStream.close();
}
This worked like CHARM !!

Game port to unity: Web posting

I am porting a game from Java Native to Unity. While the game is working correctly, I am having trouble posting the score using the same web services.
Java Code:
public static String gameConfigURL = "http://192.168.0.140/services/scoreupload.svc/json/GetGameConfigurationLite";
public static String scoreUploadURL = "http://192.168.0.140/services/scoreupload.svc/json/Upload";
public static final String MagicKey = "0GmWDa6j";
private static int timeoutConnection = 60000;
public static enum RequestSource
{
Unknown,
System,
Person;
}
public static Response sendRequestForResult(Request request, String Url,
Activity activity, Response response) throws JSONException,
ClientProtocolException, IOException,ConnectTimeoutException {
/** Code to create a JSON request from requestObject **/
JSONObject object = request.getJSON();
JSONObject requestObject = new JSONObject();
requestObject.put("request", object);
Log.v("","REQUEST:"+requestObject.toString());
/** Add code to create a HttpPostRequest **/
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Url);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
HttpResponse httpResponse = null;
String jsonValueString = null;
StringEntity se = null;
try {
se = new StringEntity(requestObject.toString());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "text/json");
/**
* add code to attach the JSON object received from request to the
* HttpPostRequest Add Code to execute HttpRequest
**/
httpResponse = client.execute(httpPost);
/** Get string from the HttpRespnse **/
jsonValueString = EntityUtils.toString(httpResponse.getEntity());
Log.v("","RESPONSE:"+jsonValueString);
/** Create JSON object from incoming String **/
JSONObject repliedObject = new JSONObject(jsonValueString);
response.fromJSON(repliedObject);
return response;
How Do I convert this to unity C#.
So far I have this:
JSONObject j = new JSONObject ();
j.AddField ("Id", "1234567890");
j.AddField ("MagicKey", ApplicationServices.magicKey);
j.AddField ("RequestedBy", "09996f84-1a06-e211-a518-001aa020d699");
j.AddField ("Timestamp", "/Date(1547535370953)/");
j.AddField ("RequestSource", "Person");
j.AddField ("RequestedGameId", "375b43c0-91be-e011-a505-001aa020d699");
j.AddField ("RequestedPersonId", "09996f84-1a06-e211-a518-001aa020d699");
string json = j.ToString ();
Dictionary<string, string> header = new Dictionary<string, string>();
header.Add ("Accept", "application/json");
header.Add ("Content-Type", "text/json");
byte[] encode = Encoding.ASCII.GetBytes (json.ToCharArray ());
WWW getConfig = new WWW (ApplicationServices.gameConfigURL, encode, header);
yield return getConfig;
if (getConfig.error != null) {
Debug.Log (getConfig.error);
} else {
Debug.Log (getConfig.text);
}
This does not seem to work.
For "POST" you should use WWWForm instead of WWW.
Take a look here

Any way to get the path parameters in httpservlet request

I have rest service implemented.
I am trying to get the path parameters of the the request in filter.
My request is
/api/test/{id1}/{status}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException
{
//Way to get the path parameters id1 and status
}
You can autowire HttpServletRequest in your filter and use it to get information.
#Autowire
HttpServletRequest httpRequest
httpRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)
will give you map of path params.
Example:
If your request is like url/{requestId} then above map will return
0 = {LinkedHashMap$Entry#12596} "requestId" -> "a5185067-612a-422e-bac6-1f3d3fd20809"
key = "requestId"
value = "a5185067-612a-422e-bac6-1f3d3fd20809"
There's no other way to do it in a ServletFilter other than trying to parse the URI yourself, but you can access the path parameters if you decide to use a JAX-RS request filter:
#Provider
public class PathParamterFilter implements ContainerRequestFilter {
#Override
public void filter(ContainerRequestContext request) throws IOException {
MultivaluedMap<String, String> pathParameters = request.getUriInfo().getPathParameters();
pathParameters.get("status");
....
}
}
String pathInfo = request.getPathInfo();
if (pathInfo != null) {
String[] parts = pathInfo.split("/");
int indexOfName = Arrays.asList(parts).indexOf("test");
if (indexOfName != -1) {
Optional<String> testId1 = Optional.of(parts[indexOfName + 1]);
Optional<String> status= Optional.of(parts[indexOfName + 2]);
}
}
Your Servlet Mapping should be till /api/*
eg. #WebServlet(urlPatterns = {"/api/*"})

How to handle an invalid content type in Rest Template?

I am querying a REST API, for negative cases in response I am getting a 200 code and some weird Content-type in headers. Because of this I am unable to store the response, as it throws an exception while parsing.
Below image shows the headers from the response:
ResponseErrorHandler:
#Component
public class AutomationResponseErrorHandler implements ResponseErrorHandler{
private static final Logger logger = LoggerFactory.getLogger(AutomationResponseErrorHandler.class);
#Override
public boolean hasError(ClientHttpResponse response) throws IOException {
// TODO Auto-generated method stub
return response.getStatusCode() != HttpStatus.OK;
}
#Override
public void handleError(ClientHttpResponse response) throws IOException {
logger.error("Response Error: {} {} {}", response.getStatusCode(), response.getStatusText(), response.getBody());
}
}
Custom RestTemplate:
#Component
public class CustomRestTemplate {
#Autowired
AutomationResponseErrorHandler responseErrorHandler;
public RestTemplate getRestTemplate(boolean isHttpsRequired)
throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
// if https is not required,
if (!isHttpsRequired) {
return new RestTemplate();
}
// else below code adds key ignoring logic for https calls
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
restTemplate.setErrorHandler(responseErrorHandler);
return restTemplate;
}
}
Below is the code for saving response:
ResponseEntity<String> response = restTemplate.getForEntity(outBound, String.class);
Below is the Exception occured:
at org.springframework.http.MediaType.parseMediaType(MediaType.java:534)
at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:869)
at org.springframework.web.client.HttpMessageConverterExtractor.getContentType(HttpMessageConverterExtractor.java:124)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:88)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:991)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:974)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:725)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:359)
at com.att.aotsm.msnautomationscheduler.TicketCloseAutomation.queryTicketCloseAPI(TicketCloseAutomation.java:54)
at com.att.aotsm.msnautomationscheduler.AutomationInvokeWebService.queryTicketCloseAPI(AutomationInvokeWebService.java:71)
at com.att.aotsm.msnautomationscheduler.AutomationThreadProcess.run(AutomationThreadProcess.java:138)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.util.InvalidMimeTypeException: Invalid mime type "`colnames<-`(`*tmp*`, value = c("MSN/Port", "Count"))": does not contain '/'
at org.springframework.util.MimeTypeUtils.parseMimeType(MimeTypeUtils.java:194)
at org.springframework.http.MediaType.parseMediaType(MediaType.java:531)
... 12 more
I want the to save the response body, no matter whatever the content-type is.