I m working with httpCore in order to create my own basic web server. Upon receiving a certain request from the user a file transfer operation has to be initiated. I m handling the request with HttpRequestHandler. My code looks like this
private HttpRequestHandler mRequestHandler = new HttpRequestHandler()
{
#Override
public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException
{
try{
HttpEntity entity=null;
String contentType="text/html";
entity = new EntityTemplate(new ContentProducer()
{
public void writeTo(final OutputStream outstream)throws IOException
{
OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
String resp = "Server is up and running";
writer.write(resp);
writer.flush();
}
});
((EntityTemplate)entity).setContentType(contentType);
response.setEntity(entity);
}catch(Exception e)
{
e.printStackTrace();
}
}
};
This is a very basic response , however What I m looking to transfer a file from the server to the client machine. How can I send a file in response ? Thanks
I figured it out myself. If someone stumbles across the same problem, here is how I got it working.
private HttpRequestHandler mRequestHandler = new HttpRequestHandler()
{
#Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
throws HttpException, IOException
{
try{
File file = new File("/mnt/sdcard/Music/song.mp3");
FileEntity body = new FileEntity(file, "audio/mpeg");
response.setHeader("Content-Type", "application/force-download");
response.setHeader("Content-Disposition","attachment; filename=song.mp3");
response.setEntity(body);
}catch(Exception e)
{
e.printStackTrace();
}
}
};
Related
I've came up with the following problem. In my application I need to post an XML file to a server and wait for it's reply. I've achieved the above by using Apache HttpClient:
create the xml
using HttpClient and Post to post it to the server
The server initially responded with a 302 (Object moved), so I used LaxRedirectStrategy to be able to follow the redirect. I wrap the response in a StringBuffer and then push it into a new tab of the browser. Everything works fine, the tab shows the complete response from that server but every action in that page is not associated to the URL of the server response but to my main application. So, for example, if my application is on https://myapplication.com and the server is on https://theotherserver.com (+ /redirect for the redirect), each action on the page (i.e. /action) leads to https://myapplication.com/action and not to https://theotherserver.com/action.
In my page I have a button where this process starts. When you click it, it executes:
jQuery('input#button').on
(
'click',
function ()
{
jQuery.ajax
(
{
url: 'myURL.do',
data: {data: mydata},
method: 'POST',
success: function(data)
{
if (data.success == 'true')
{
var w = window.open('tab', windowname');
w.document.write(data.result);
w.document.close();
w.focus();
}
else
{
alert(data.error);
}
}
});
});
the .do executes the following:
public ResponseEntity<Map<String, String>> myMethod(HttpServletRequest request, HttpServletResponse response) throws ParserConfigurationException
{
Map<String, String> respData = new HashMap<String, String>();
try
{
String myXML = prepareMyXMLFile();
String result = sendMyXMLFile(myXML);
respData.put("success", String.valueOf(true));
respData.put("result", result);
return new ResponseEntity<Map<String, String>>(respData, HttpStatus.OK);
}
catch (Exception e)
{
respData.put("success", String.valueOf(false));
respData.put("error", String.valueOf(e));
return new ResponseEntity<Map<String, String>>(respData, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
and finally sendMyXMLFile():
private String sendMyXML(String myXML)
{
try
{
HttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
HttpPost post = new HttpPost("myURL");
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("xml", myXML));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new
InputStreamReader(response.getEntity().getContent()));
StringBuffer responseResult = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null)
{
responseResult.append(line);
}
return responseResult.toString();
}
catch (IOException | TransformerException e)
{
//log error
}
return null;
}
What do I need to do to use the response URL as the base URL in the other tab?
Any help is appreciated. Thanks
I'm adding message level encryption (MLE) to an existing code base for outgoing requests. To do this, I simply wrote an interceptor that will catch outgoing requests, encrypt their bodies, and then send the request out. The response we get is also encrypted, and must be decrypted. This all is working fine for me. The only problem I'm having is that I must replace the ClientHttpResponse encrypted body with the now decrypted JSON. How can I do this? I don't see any methods that will let me alter the response body. Thanks in advance.
#Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
ClientHttpResponse response;
String bodyStr = new String(body);
// Encrypt the body and send
bodyStr = encrypt(bodyStr);
try {
response = execution.execute(request, bodyStr.getBytes());
} catch (Exception e) {
throw e;
}
// Decrypt the response body
String decryptedResponseBody = decrypt(response.getBody());
// Set the response body to the decrypted data (JSON)
// response.setBody(decryptedResponseBody)?????????
return response;
}
You will need to create an implementation of ClientHttpResponse which is not too hard since there are only a few methods to override, I added an example of how you would fix this. I hope this helps. I would suggest adding a named bean for this type of request, you don't want to have all your resttemplates being encrypted/decrypted.
restTemplate.getInterceptors().add( (ClientHttpRequestInterceptor)
(request, body, execution) -> {
ClientHttpResponse response;
String bodyStr = new String(body);
// Encrypt the body and send
bodyStr = encrypt(bodyStr);
try {
response = execution.execute(request, bodyStr.getBytes());
String text = IOUtils.toString(response.getBody(), StandardCharsets.UTF_8.name());
// Decrypt the response body
String decryptedResponseBody = decrypt(text);
} catch (Exception e) {
throw e;
}
InputStream inputStream = inputStream = new ByteArrayInputStream(decryptedResponseBody.getBytes());
return new ClientHttpResponse() {
#Override
public HttpHeaders getHeaders() {
return response.getHeaders();
}
#Override
public InputStream getBody() throws IOException {
return inputStream;
}
#Override
public HttpStatus getStatusCode() throws IOException {
return response.getStatusCode();
}
#Override
public int getRawStatusCode() throws IOException {
return response.getRawStatusCode();
}
#Override
public String getStatusText() throws IOException {
return response.getStatusText();
}
#Override
public void close() {
response.close();
}
};
}))
I am trying to log all the outgoing Http requests in my spring based web application. Is there is interceptor for this purpose? I want to log all outgoing the contents and headers before it leaves the application. I am using spring-ws to send SOAP requests. So basically, I want to log not only the SOAP request xml (as mentioned here How can I make Spring WebServices log all SOAP requests?) but the http request as a whole.
Intercept the request/response using a ClientInterceptor on the WebServiceGatewaySupport:
// soapClient extends WebServiceGatewaySupport
soapClient.setInterceptors(new ClientInterceptor[]{new ClientInterceptor() {
#Override
public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
messageContext.getRequest().writeTo(os);
} catch (IOException e) {
throw new WebServiceIOException(e.getMessage(), e);
}
String request = new String(os.toByteArray());
logger.trace("Request Envelope: " + request);
return true;
}
#Override
public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
messageContext.getResponse().writeTo(os);
} catch (IOException e) {
throw new WebServiceIOException(e.getMessage(), e);
}
String response = new String(os.toByteArray());
logger.trace("Response Envelope: " + response);
return true;
}
...
To get the headers as well you need an instance of TransportOutputStream.
Unfortunately the class is abstract, so you need to subclass is. Here's how it might look:
class ByteArrayTransportOutputStream extends TransportOutputStream {
private ByteArrayOutputStream outputStream;
#Override
public void addHeader(String name, String value) throws IOException {
createOutputStream();
String header = name + ": " + value + "\n";
outputStream.write(header.getBytes());
}
public byte[] toByteArray() {
return outputStream.toByteArray();
}
#Override
protected OutputStream createOutputStream() throws IOException {
if (outputStream == null) {
outputStream = new ByteArrayOutputStream();
}
return outputStream;
}
}
I'm trying to download a XML file that is gzip compressed from a server for that I use the following code:
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
HttpGet response = new HttpGet(urlData);
client.addRequestInterceptor(new HttpRequestInterceptor() {
#Override
public void process(HttpRequest request, HttpContext context) {
// Add header to accept gzip content
if (!request.containsHeader("Accept-Encoding")) {
request.addHeader("Accept-Encoding", "gzip");
}
}
});
client.addResponseInterceptor(new HttpResponseInterceptor() {
#Override
public void process(HttpResponse response, HttpContext context) {
// Inflate any responses compressed with gzip
final HttpEntity entity = response.getEntity();
final Header encoding = entity.getContentEncoding();
if (encoding != null) {
for (HeaderElement element : encoding.getElements()) {
if (element.getName().equalsIgnoreCase("gzip")) {
response.setEntity(new InflatingEntity(response.getEntity()));
break;
}
}
}
}
});
ResponseHandler<String> responseHandler = new BasicResponseHandler();
return client.execute(response, responseHandler);
InflatingEntity method:
private static class InflatingEntity extends HttpEntityWrapper {
public InflatingEntity(HttpEntity wrapped) {
super(wrapped);
}
#Override
public InputStream getContent() throws IOException {
return new GZIPInputStream(wrappedEntity.getContent());
}
#Override
public long getContentLength() {
return -1;
}
}
If I remove everything related to Gzip compression and replace the compressed XML file from the server with a normal XML everything works fine, but after I implement the Gzip compression I get the compressed string:
Does anyone knows what is missing in my code to get the decompressed XML?
I have solved the problem, my response didn't have an entity so the code was not decompressing the response since that part of the code was not being reached, here is the modification in the responseinterceptor:
client.addResponseInterceptor(new HttpResponseInterceptor() {
#Override
public void process(HttpResponse response, HttpContext context) {
response.setEntity(new InflatingEntity(response.getEntity()));
}
});
In my app, I need to send all sorts of POST requests to a server. some of those requests have responses and others don't.
this is the code I'm using to send the requests:
private static final String TAG = "Server";
private static final String PATH = "http://10.0.0.2:8001/data_connection";
private static HttpResponse response = null;
private static StringEntity se = null;
private static HttpClient client;
private static HttpPost post = null;
public static String actionKey = null;
public static JSONObject sendRequest(JSONObject req) {
try {
client = new DefaultHttpClient();
actionKey = req.getString("actionKey");
se = new StringEntity(req.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_ENCODING, "application/json"));
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post = new HttpPost(PATH);
post.setEntity(se);
Log.d(TAG, "http request is being sent");
response = client.execute(post);
Log.d(TAG, "http request was sent");
if (response != null) {
InputStream in = response.getEntity().getContent();
String a = convertFromInputStream(in);
in.close();
return new JSONObject(a);
}
} catch (UnsupportedEncodingException e) {
Log.d(TAG, "encoding request to String entity faild!");
e.printStackTrace();
} catch (ClientProtocolException e) {
Log.d(TAG, "executing the http POST didn't work");
e.printStackTrace();
} catch (IOException e) {
Log.d(TAG, "executing the http POST didn't work");
e.printStackTrace();
} catch (JSONException e) {
Log.d(TAG, "no ActionKey");
e.printStackTrace();
}
return null;
}
private static String convertFromInputStream(InputStream in)
throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
return (sb.toString());
}
This is the code for the AsyncTask class that sends the request:
class ServerRequest extends AsyncTask<JSONObject, Void, JSONObject> {
#Override
protected JSONObject doInBackground(JSONObject... params) {
JSONObject req = params[0];
JSONObject response = Server.sendRequest(req);
return response;
}
#Override
protected void onPostExecute(JSONObject result) {
// HANDLE RESULT
super.onPostExecute(result);
}
}
my problem starts when the server doesn't return a response. the AsyncTask thread stays open even after the work is done because the HTTPClient never closes the connection.
Is there a way to not wait for a response? this is something that will definitely add a lot of overhead to the server since all the Android apps trying to connect to it will keep the connection alive, and will probably cause many problems on the app itself.
Basically, what I'm looking for is a method that will allow me to send to POST message and kill the connection right after the sending of the request since there is no response coming my way.
Just, Set ConnectionTimeOut with HttpClient Object, (Code is for your understanding in your case it may be different)
int TIMEOUT_MILLISEC = 30000;
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient httpclient = new DefaultHttpClient(httpParams);
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Content-Type", "application/json");
Now, It will terminate the Connection after TimeoOut you defined. But be sure this will throw TimeOutException so You have to handle this exception in your HttpRequest.. (Use Try -catch)
EDIT: Or you can use HttpRequestExecutor class.
From class HttpRequestExecutor of package org.apache.http.protocol
protected boolean canResponseHaveBody (HttpRequest request, HttpResponse response)
Decide whether a response comes with an entity. The implementation in this class is based on RFC 2616. Unknown methods and response codes are supposed to indicate responses with an entity.
Derived executors can override this method to handle methods and response codes not specified in RFC 2616.