Servlet: doGet not returning - servlets

I receive an image in doPost method from a client application. I'm not supposed to store this image in any folder path, so i use the following code to store this image in session attribute as data byte.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String fileName = null;
fileName = request.getParameter("filename");
System.out.println("filename: " + fileName);
DataInputStream din = new DataInputStream(request.getInputStream());
byte[] data = new byte[0];
byte[] buffer = new byte[512];
int bytesRead;
while ((bytesRead = din.read(buffer)) > 0) {
// construct an array large enough to hold the data we currently have
byte[] newData = new byte[data.length + bytesRead];
// copy data that was previously read into newData
System.arraycopy(data, 0, newData, 0, data.length);
// append new data from buffer into newData
System.arraycopy(buffer, 0, newData, data.length, bytesRead);
// set data equal to newData in prep for next block of data
data = newData;
}
request.getSession().setAttribute("imageData", data);
}
I want to retrieve this from doGet method after its received. So, i am trying with the following doGet code to retrieve it.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
byte[] data = (byte[]) request.getSession().getAttribute("imageData");
int len = data.length;
byte[] imageData = new byte[len];
for(int i=0; i < len; i++) {
imageData[i] = data[i];
}
response.setContentType("image/jpg");
response.getOutputStream().write(imageData);
response.getOutputStream().flush();
response.getOutputStream().close();
}
But, its NOT returning this image in doGet, when i call this servlet from an another client after some time.
Could someone please advise me, what I'm doing wrong here for not getting image in doGet?

I'm not supposed to store this image in any folder path, so i use the following code to store this image in session attribute as data byte.
...
But, its NOT returning this image in doGet, when i call this servlet from an other client after some time.
Session attributes are associated with exactly one client, so one client can not get the session attributes from an other client
You could store the image in the servlet context, like:
ServletContext context = request.getSession().getServletContext();
context.setAttribute("imageData", data);
Later, you can read the attribute from the servlet context.
Or, another possibility is to store the image in a static variable.
However, the image will be stored, although it is in memory. Maybe some Servlet Containers also store Servlet Context attributes on the hard disk.

Related

Using Open CSV to Send files from ServletOutputStream

I have parsed a JSON Object that was sent to my TOMCAT server using POST and turned it into a CSV file. I would like to send the CSV file back to the user. I am confused about how to send the file back because all the examples I have seen reference an actual file that is stored on the server - my file is built in memory and doesn't actually reference a file/file descriptor as far as I can tell.
//The doPost method has already generated the String[] - I then call generateCSV
protected void generateCSV(ArrayList<String[]> data, ServletOutputStream out,
HttpServletResponse response)
{
try{
BufferedWriter buff = new BufferedWriter(new OutputStreamWriter(out));
CSVWriter writer = new CSVWriter(buff);
writer.writeAll(data);
writer.close();
sendFile(response, out);
}catch(IOException | ServletException e){
e.printStackTrace();
}
}
protected void sendFile(HttpServletResponse response, ServletOutputStream outstream)throws ServletException, IOException
{
response.setContentType("application/octet-stream");
outstream.flush();
outstream.close();
}
What do you have to do to send a file to the user from a servlet?
Set the content-type.
Write out the bytes.
Seems to me you are doing this, but in the wrong order. Also, rather than using application/octet-stream you might want to actually tell the browser this is CSV. You might also want to give it a filename.
response.setContentType("text/csv");
response.setHeader("Content-Disposition","filename=whatever.csv");
BufferedWriter buff = new BufferedWriter(new OutputStreamWriter(out));
CSVWriter writer = new CSVWriter(buff);
writer.writeAll(data);
writer.close();

Mocking HttpClient.execute issues: Mockito

I am trying to test this method.
#Override
public JSON connectResource() throws IOException {
//get the location and credentials for the certificates
System.setProperty("javax.net.ssl.trustStore", "C:/Program Files/Java/jdk1.7.0_40/jre/lib/security/cacerts");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
HttpRequest httpRequest = new HttpGet(url);
System.out.println("hello");
httpRequest.addHeader("Accept", "application/json");
HttpResponse response = httpClient.execute((HttpUriRequest) httpRequest);
System.out.println("hello1");
HttpEntity httpEntity = response.getEntity();
String data = this.getData(httpEntity);
return JSONSerializer.toJSON(data.toString());
}
My set up method is:
#Before
public void setUp() throws Exception{
mockHttpClient = mock(DefaultHttpClient.class);
mockHttpRequest = mock(HttpUriRequest.class);
mockHttpResponse = mock(BasicHttpResponse.class);
mockHttpEntity = mock(HttpEntity.class);
mockInputStream = mock(InputStream.class);
mockInputStreamReader = mock(InputStreamReader.class);
mockBufferedReader = mock(BufferedReader.class);
mockHttpGet = mock(HttpGet.class);
mockHttpRequestBase = mock(HttpRequestBase.class);
//when(mockHttpClient.execute(Mockito.isA(HttpUriRequest.class))).thenReturn(mockHttpResponse);
//when(mockHttpClient.execute(mockHttpRequest)).thenReturn(mockHttpResponse);
//when(mockHttpClient.execute(mockHttpRequestBase)).thenReturn(mockHttpResponse);
//when(mockHttpClient.execute(mockHttpGet)).thenReturn(mockHttpResponse);
when(mockHttpResponse.getEntity()).thenReturn(mockHttpEntity);
when(mockHttpEntity.getContent()).thenReturn(mockInputStream);
PowerMockito.whenNew(InputStreamReader.class)
.withArguments(mockInputStream).thenReturn(mockInputStreamReader);
PowerMockito.whenNew(BufferedReader.class)
.withArguments(mockInputStreamReader).thenReturn(mockBufferedReader);
PowerMockito.when(mockBufferedReader.readLine())
.thenReturn(JSON_STRING)
.thenReturn(null);
PowerMockito.whenNew(HttpGet.class).withArguments(VALID_URL)
.thenReturn(mockHttpGet);
}
And my test case is :
#Test
public void testConnectResource() throws IOException {
when(mockHttpClient.execute(mockHttpGet)).thenReturn(mockHttpResponse);
HttpConnectGithub connHandle = new HttpConnectGithub(VALID_URL);
JSON jsonObject = connHandle.connectResource();
System.out.println(jsonObject);
//assertThat(jsonObject, instanceOf(JSON.class));
}
However, the execution stops at
HttpResponse response = httpClient.execute((HttpUriRequest) httpRequest);
you can see all that I tried in the comments of my set up method.
Does anyone find an issue with anything? I debugged through my test case and all mock objects are properly initialized.
I have tried exchanging HttpUriRequest and HttpRequest, HttpResponse and BasicHttpResponse etc but without much luck.
Please guide on how to tackle this issue.
Part of the problem you're running into is matching the arguments:
#Test
public void testConnectResource() throws IOException {
when(mockHttpClient.execute(mockHttpGet)).thenReturn(mockHttpResponse);
HttpConnectGithub connHandle = new HttpConnectGithub(VALID_URL);
JSON jsonObject = connHandle.connectResource();
System.out.println(jsonObject);
//assertThat(jsonObject, instanceOf(JSON.class));
}
With the line you've specified above
when(mockHttpClient.execute(mockHttpGet)).thenReturn(mockHttpResponse);
The mocking will only trigger when the instance of mockHttpGet you've defined is passed.
Your method under test on the other hand is creating a new HttpGet instance which is not going to be the same as the mockHttpGet instance. You will need to alter the 'when' statement so that you have something like
when(mockHttpClient.execute(Matchers.any(HttpGet.class))).thenReturn(mockHttpResponse);
I'm doing this exclusively from memory so the Matchers.any() may be incorrect, but you should be able to make headway based on what I've given you above.
The problem is with mockHttpClient. It is not able to mock it automatically for some reason. The fix is to pass httpclient as a parameter through some method (constructor in my case)

How to avoid java.io.IOException: Attempted read on closed stream

I'm trying to find a way to avoid the IOException related to the fact that I read on a closed stream.
I'm calling a webservice method that returns a Stream:
InputStream stream = callRestWebService();
try {
parkingState = objectMapper.readValue(stream, ParkingState.class);
} catch (IOException e) {
throw new ParkingMeasurementProviderException("Could not retrieve data.", e);
}
Then, I have my Web Service method where I close the get connection:
public InputStream callRestWebService() {
int parkingId = 2803;
String endpointURL = REST_ENDPOINT + URI_INFO_PATH + parkingId + "/parkingState";
InputStream inputStream = null;
// Create an instance of HttpClient.
HttpClient httpclient = new HttpClient();
// Create a method instance.
GetMethod getMethod = new GetMethod(endpointURL);
getMethod.addRequestHeader("accept", "application/json");
try {
// Execute the method.
int statusCode = httpclient.executeMethod(getMethod);
inputStream = getMethod.getResponseBodyAsStream();
} catch (IOException e) {
e.printStackTrace();
} finally {
// Release the connection.
getMethod.releaseConnection();
}
return inputStream;
}
Is there a way to avoid having this exception without removing the : getMethod.releaseConnection();
The stack trace:
Disconnected from the target VM, address: '127.0.0.1:62152', transport: 'socket'
at be.ixor.itg.server.service.parking.hermesWS.HermesWSParkingControllerMeasurementProvider.getHermesMechelenData(HermesWSParkingControllerMeasurementProvider.java:126)
at be.ixor.itg.server.service.parking.hermesWS.Main.main(Main.java:14)
Caused by: java.io.IOException: Attempted read on closed stream.
at org.apache.commons.httpclient.AutoCloseInputStream.isReadAllowed(AutoCloseInputStream.java:183)
at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:86)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(XMLEntityManager.java:2977)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:702)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:232)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
at be.ixor.itg.server.service.parking.hermesWS.HermesWSParkingControllerMeasurementProvider.getHermesMechelenData(HermesWSParkingControllerMeasurementProvider.java:116)
... 1 more
Because you are calling releaseConnection() in your finally block, the input stream will no longer be available.
If you do not expect the content to be large, you could read the data from the input stream into a buffer and return the buffer instead of the input stream. Otherwise, you will need to change your code to allow the called to process the data from the input stream before releasing the connection.
BufferedReader br = new BufferedReader(new
InputStreamReader((response.getEntity().getContent())));
String response = br.readLine();
System.out.println("response" + response );
This code is working for me.

c# how to get a stream processed by httpResponse.BinaryWrite

I have the following method that writes a stream in a HttpResponse object.
public HttpResponse ShowPDF(Stream stream)
{
MemoryStream memoryStream = (MemoryStream) stream;
httpResponse.Clear();
httpResponse.Buffer = true;
httpResponse.ContentType = "application/pdf";
httpResponse.BinaryWrite(memoryStream.ToArray());
httpResponse.End();
return httpResponse;
}
In order to test it, I need to recover the processed stream.
Is there someway to read the stream from the httpResponse object?
I have two ideas... one to mock the HttpResponse, and the other is to simulate a web server.
1. Mocking HttpResponse
I wrote this before I knew which mocking framework you used. Here's how you could test your method using TypeMock.
This assumes that you pass your httpResponse variable to the method, changing the method as follows:
public void ShowPDF(Stream stream, HttpResponse httpResponse)
Of course you would change this to passing it to a property on your Page object instead, if it is a member of your Page class.
And here's an example of how you could test using a fake HttpResponse:
internal void TestPDF()
{
FileStream fileStream = new FileStream("C:\\deleteme\\The Mischievous Nerd's Guide to World Domination.pdf", FileMode.Open);
MemoryStream memoryStream = new MemoryStream();
try
{
memoryStream.SetLength(fileStream.Length);
fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
memoryStream.Flush();
fileStream.Close();
byte[] buffer = null;
var fakeHttpResponse = Isolate.Fake.Instance<HttpResponse>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => fakeHttpResponse.BinaryWrite(null)).DoInstead((context) => { buffer = (byte[])context.Parameters[0]; });
ShowPDF(memoryStream, fakeHttpResponse);
if (buffer == null)
throw new Exception("It didn't write!");
}
finally
{
memoryStream.Close();
}
}
2. Simulate a Web Server
Perhaps you can do this by simulating a web server. It might sound crazy, but it doesn't look like it's that much code. Here are a couple of links about running Web Forms outside of IIS.
Can I run a ASPX and grep the result without making HTTP request?
http://msdn.microsoft.com/en-us/magazine/cc163879.aspx

Sending and receiving binary data in Servlets

I'm attempting to write a Java Servlet to receive binary data requests and reply to them, using HttpServletRequest.getOutputStream() and HttpServletResponse.getInputStream(). This is for a project which involves having a request sent by a Silverlight client to which this servlet responds to through an HTTP POST connection. For the time being, to test the Servlet I'm implementing a client in Java which I'm more familiar with than Silverlight.
The problem is that in my test project I send the data from a Servlet client as a byte array and expect to receive a byte array with the same length -- only it doesn't, and instead I'm getting a single byte. Therefore I'm posting here the relevant code snippets in the hopes that you might point me where I'm doing wrong and hopefully provide relevant bibliography to help me further.
So here goes.
The client servlet handles POST requests from a very simple HTML page with a form which I use as front-end. I'm not too worried about using JSP etc, instead I'm focused on making the inter-Servlet communication work.
// client HttpServlet invokes this method from doPost(request,response)
private void process(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String firstName = (String) request.getParameter("firstname");
String lastName = (String) request.getParameter("lastname");
String xmlRequest = "<MyRequest><Person><Name Firstname=\""+firstName+"\" Lastname=\""+lastName+"\" /></Person></MyRequest>";
OutputStream writer = null;
InputStream reader = null;
try {
URL url = new URL("http://localhost:8080/project/Server");
URLConnection conn = url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
writer = conn.getOutputStream();
byte[] baXml = xmlRequest.getBytes("UTF-8");
writer.write(baXml, 0,baXml.length);
writer.flush();
// perhaps I should be waiting here? how?
reader = conn.getInputStream();
int available = reader.available();
byte[] data = new byte[available];
reader.read(data,0,available);
String xmlResponse = new String(data,"UTF-8");
PrintWriter print = response.getWriter();
print.write("<html><body>Response:<br/><pre>");
print.write(xmlResponse);
print.write("</pre></body></html>");
print.close();
} finally {
if(writer!=null)
writer.close();
if(reader!=null)
reader.close();
}
}
The server servlet handles HTTP POST requests. This is done by receiving requests the requests from a client Servlet for testing purposes above, but in the future I intend to use it for clients in other languages (specifically, Silverlight).
// server HttpServlet invokes this method from doPost(request,response)
private void process(HttpServletRequest request, HttpServetResponse response)
throws ServletException, IOException {
ServletInputStream sis = null;
try {
sis = request.getInputStream();
// maybe I should be using a BufferedInputStream
// instead of the InputStream directly?
int available = sis.available();
byte[] input = new byte[available];
int readBytes = sis.read(input,0,available);
if(readBytes!=available) {
throw new ServletException("Oops! readBytes!=availableBytes");
}
// I ONLY GET 1 BYTE OF DATA !!!
// It's the first byte of the client message, a '<'.
String msg = "Read "+readBytes+" bytes of "
+available+" available from request InputStream.";
System.err.println("Server.process(HttpServletRequest,HttpServletResponse): "+msg);
String xmlReply = "<Reply><Message>"+msg+"</Message></Reply>";
byte[] data = xmlReply.getBytes("UTF-8");
ServletOutputStream sos = response.getOutputStream();
sos.write(data, 0,data.length);
sos.flush();
sos.close();
} finally {
if(sis!=null)
sis.close();
}
}
I have been sticking to byte arrays instead of using BufferInputStreams so far because I've not decided yet if I'll be using e.g. Base64-encoded strings to transmit data or if I'll be sending binary data as-is.
Thank you in advance.
To copy input stream to output stream use the standard way:
InputStream is=request.getInputStream();
OutputStream os=response.getOutputStream();
byte[] buf = new byte[1000];
for (int nChunk = is.read(buf); nChunk!=-1; nChunk = is.read(buf))
{
os.write(buf, 0, nChunk);
}
The one thing I can think of is that you are reading only request.getInputStream().available() bytes, then deciding that you have had everything. According to the documentation, available() will return the number of bytes that can be read without blocking, but I don't see any mention of whether this is actually guaranteed to be the entire content of the input stream, so am inclined to assume that no such guarantees are made.
I'm not sure how to best find out when there is no more data (maybe Content-Length in the request can help?) without risking blocking indefinitely at EOF, but I would try looping until having read all the data from the input stream. To test that theory, you could always scan the input for a known pattern that occurs further into the stream, maybe a > matching the initial < that you are getting.

Resources