1. I am able to create an byte array that contains valid PNG. I can write that byte array to an PNG image file, read that file using the following and display image.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//write image to baos
JFreeChart chart = ...<get the chart>..;
ChartUtilities.writeChartAsPNG(baos, chart, 350, 200); //this is a JFreeChart method
File file = File.createTempFile("image-1234", ".png");
FileOutputStream fout = new FileOutputStream(file);
fout.write(baos.toByteArray());
Image img = new Image(file.getAbsolutePath());
2. I created and configured servlet that returns contents of that byte array when specific url is requested (let name it 'reqUrl'). Here is the call to create the Image.
reqUrl = "http://localhost:8080/APP1/DynamicImageServlet?imgId=1234";
Image img = new Image(reqUrl);
3. An exception occurs on call of ii.check() inside ImageInfo, where ii represents ImageParser. I don't think it is contacting the servlet that provides the image byte array.
Exception: UnsupportedOperationException with "Unknown image file format for file."
4. When I type-in the reqUrl in browser -- the servelet is contacted (debug messages are given) -- since the servlet is expecting to reference a global image hashtable the actual processing fails, BUT it does get contacted.
5. Application.getResourceAsStrem(reqUrl) returns an InputStream object. Does not call the DynamicImageServlet. Its reference is (java.io.BufferedInputStream@1ef7de4)
I HOPE A DEVELOPER OR SOMEONE ELSE CAN PROVIDE INSIGHT....
IF YOU NEED MORE PLEASE LET ME KNOW.
THANKS!!!
Here is my reference to DynamicImageServlet.java in web.xml:
<servlet>
<servlet-name>DynamicImageServlet</servlet-name>
<servlet-class>my.path.DynamicImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DynamicImageServlet</servlet-name>
<url-pattern>/DynamicImageServlet/</url-pattern>
</servlet-mapping>
Logged In: YES
user_id=1588044
Originator: YES
Here is some test code that I think will show the issue. You will need JFreeChart jar files (jfreechart-1.0.0-rc1.jar and jcommon-1.0.10.jar).
File Added: TestDynamicImageServlet.jar
Zip file of TestDynamicImageServlet eclipse package
Logged In: YES
user_id=1874369
Originator: NO
Don't think this a bug in thinwire as I do this with a delegate server that forwards thinwire requests to the thinwire servlet and file requests to another servlet in the same instance. To debug you may want to print the keyset(in the delegate server) of you hashmap to see if the image is actually there(to see whether the static hasmap is shared between both servlets in this setup). If it is in the same instance post your image delegate servlet class here and we can go from there.
Logged In: YES
user_id=1874369
Originator: NO
Oh, by the way, you may want to put an extension on the file name if you don't do it in the delgate.
e.g.
http://localhost:8080/APP1/DynamicImageServlet?imgId=1234.png
Logged In: YES
user_id=1618512
Originator: NO
I have spent 15 minutes fiddling with this code - the byte array that is returned by Application.getResourceAsStream() is not the same that is written to temp file. Former has 1940 bytes and latter 6300 (values are approximately, I do not have access to exact values right now). Reason I nve not figured out yet.
Logged In: YES
user_id=1874369
Originator: NO
You delegate server service method should look something like this:
//private WebServlet delegate = new WebServlet();//need to init(cfg) in overriden method
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String method = request.getMethod();
if (method.equals("GET")) {
String imageID = request.getParameter("imageId");
if (resource != null) {
//get imageID bytes from hashmap
byte[] imageBytes = STATIC_IMAGE_HASHMAP.get(imagedID);
//might want to check the image exists and send resource not found error if not
response.setContentType(mimeType);
response.setHeader("Content-Disposition","filename="+imageId);
//write the bytes to the output stream
response.getOutputStream().write(imageBytes);
response.flushBuffer();
return;
}
else{
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
}
//if you have set up a delegate server you could then send the request to the thinwire servlet
//delegate.service(request, response);
}
Pass the url into the image and it will work. Try opening the resource as a hyperlink.openlocation and see what is returned in a web page.
Logged In: YES
user_id=1588044
Originator: YES
Where does this service method go..
public void service(HttpServletRequest request, HttpServletResponse
response) throws IOException, ServletException { .... }
I was told to create a DynamicImageServlet class...
public class DynamicImageServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
Integer imageid = (Integer)request.getAttribute("imageid");
if (imageid == null)
{
throw new ServletException("No image identifier exists");
}
byte[] image = TestDynamicImageServlet.images.get(imageid);
response.setContentType("image/png");
response.setContentLength(image.length);
response.getOutputStream().write(image);
response.getOutputStream().close();
}
}
WHAT ELSE IS NEEDED....??
I Was told to reference/call this DynamicImageServlet as stated in original bug request....
I've given an example ... what else do I need to give?
THANKS
Logged In: YES
user_id=1618512
Originator: NO
> Where does this service() method go..
It's easy. You have inherited your DynamicImageServlet from HttpServlet that in turn inherits from GenericServlet. Latter defines service() method. Former provides implementation of this method that separates GETs from POSTs and handle them with separate doGet() and doPost() methods.
When developer inherits from HttpServlet he typically overrides one (or both) of these doXXX() methods. If for some reasons developes deicides to inherit not form HttpServlet, but form GenericServlet he overrides service() method instead.
Sorry, but it is much better to ask (and answer) such questions in the forum, and not here.
Regards,
Oleksandr
Logged In: YES
user_id=1588044
Originator: YES
I'm being told to do certain things associated with this "bug" so, I am trying to respond to your questions. It's hard to tell if the developers think there is a "resolution" to the "bug".
So, what I have created with the DynamicImageServlet is correct? If so then have I referenced/called that servlet correctly??
thanks for all your help..
Logged In: YES
user_id=1618512
Originator: NO
As I have already written in one of the comments to this bug your servlet returns wrong byte stream. Reason I have not find yet. I plan to examine it today evening, when I come to home. It seems to me that problem is not in ThinWire, but I have to check it once more.
BTW, attached code contains one more bug - you wrongly construct urlReq (it contains urlReq.append("TestDynamicServlet") but shall contain urlReq.append("TestDynamicImageServlet");
Concerning you - just wait patiently.
Logged In: YES
user_id=1618512
Originator: NO
So I have spent some more time, now code works (even with ThinWire r359).
code attached to this support request was heavily bugged. Save the bug that I have mentioned earlier, there were 2 more major bugs
1. DynamicImageServlet has used getAttribute() instead of getParameter(), this is a reason why imageid inside it alway was null and caused "No image identifier exists" exception.
2. (The most important bug) Servlet mapping for DynamicImageServlet in web.xml was wrong:
<servlet-mapping>
<servlet-name>DynamicImageServlet</servlet-name>
<url-pattern>/DynamicImageServlet/</url-pattern>
</servlet-mapping>
Proper mapping is
<servlet-mapping>
<servlet-name>DynamicImageServlet</servlet-name>
<url-pattern>/DynamicImageServlet/*</url-pattern>
</servlet-mapping>
Note * in url-pattern .
For this reason request from Application.getResourceAsStream has not reached DynamicImage servlet, but was
answered by standard ThinWire servlet, obviously this response did not contained image :)
After fixing of these 2 errors code works smoothly. Hope, this request may be closed.
Regards,
Oleksandr