i have a Backing Bean in which i read parameter which are not bound to a component. Seam offered that to read get parameter. I used
#RequestParameter private String param1;
this way he doesn't skip my action method on a validation error, like when i used
<param name="param1" value="#{myBean.param1}" />
, because i don't want to render HTML responses but reuse the business logic. Instead i grab the response stream and write the rendered response myself
FacesContext ctx = FacesContext.getCurrentInstance();
final HttpServletResponse resp = (HttpServletResponse)ctx.getExternalContext().getResponse();
resp.getOutputStream().write(xml.getBytes());
resp.getOutputStream().flush();
resp.getOutputStream().close();
ctx.responseComplete(); // render response phase done
I tried to read the file from the HttpServletRequest inputStream but that was already empty. Is there a way to still get the "file" from JSF. I could use a separate servlet and handle it there, but that would break a little bit how i build the rest of the interface.
Thx for any advice.
Edit: Just for completeness the code for the input stream
final HttpServletRequest request = (HttpServletRequest)ctx.getExternalContext().getRequest();
ServletInputStream stream = request.getInputStream();
int byt = stream.read(); // byt was -1
Edit Edit: Sorry for the long post. Now i made a separate servlet. There i also get an empty input Stream even though Firefug said i transmitted the file. How can that be empty?
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
ServletInputStream stream= req.getInputStream();
int test = stream.read(); // test == -1
That servlet code works fine in a separate project, so one of the JSF/Seam servlets in the filter chain has to remove the data. If someone has a hint which one or how to check.
The SeamFilter or more exactly the MultipartFilter was checking every request if it is a POST and multipart request. If so, they wrapped the request in a MultipartRequest and put the files in a separate parameter map.
final HttpServletRequestWrapper request = (HttpServletRequestWrapper)FacesContext.getCurrentInstance().getExternalContext().getRequest();
final ServletRequest innerRequest = request.getRequest();
if(innerRequest instanceof MultipartRequest){
byte[] imageData = ((MultipartRequest)innerRequest).getFileBytes("image");
Related
i got this problem when i try to export a datatable information to PDF, the method do all, but it doesn't download the PDF or even generate.
Netbeans version: 8.2
JSF: 2.2
Primefaces: 5.3
Libraries used:
Jasperreports 6.3
poi 3.14
commons-beanutils-1.9.0
commons-collection-3.2.2
commons-digester-2.1
commons-logging-1.1.1
groovy-all-2.4.0
itext-2.1.7.js5
jaxp-ri
jcommon-1.0.23
jfreechart-1.0.19
And my method is:
public void exportpdf(OrdenRetiro or) throws JRException, IOException {
conexion con = new conexion();
Map<String, Object> parametros = new HashMap<String, Object>();
FacesContext context = FacesContext.getCurrentInstance();
ServletContext servleContext = (ServletContext) context.getExternalContext().getContext();
parametros.put("RutaImagen", servleContext.getRealPath("/reportes/"));
parametros.put("cod_ordenretiro", or.getCod_ordenretiro());
String temperatura = or.getEs_temperado()==1?"ReporteFreezer.jasper":"ReporteNoFreezer.jasper";
String dirReporte = servleContext.getRealPath("/reportes/"+temperatura);
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
response.addHeader("Content-disposition", "attachment;filename=Orden de Retiro"+or.getCod_ordenretiro()+".pdf");
response.setContentType("application/pdf");
JasperPrint impres = JasperFillManager.fillReport(dirReporte, parametros, con.getConnection());
JasperExportManager.exportReportToPdfStream(impres, response.getOutputStream());
context.responseComplete();
}
Any idea?
Got the solution.
When the method works but doesn't export is because the ajax on the xhtml. After doing some research got the answer here
I make the puntual quote.
The first problem is that the <p:commandLink> sends by default an Ajax
request. This request is fired by JavaScript code. However, JavaScript
can't do anything with a response which contains a file download. Due
to security restrictions JavaScript can't spawn a Save As dialogue or
something. The response is basically totally ignored.
You need to add ajax="false" to <p:commandLink> to turn ajax off so
that the button fires a normal synchronous HTTP request, or you need
to replace it by standard <h:commandButton>
I'm trying to display image bytes which is saved in database as a StreamedContent in the <p:graphicImage> as follows:
<p:graphicImage value="#{item.imageF}" width="50" id="grpImage" height="80"/>
private StreamedContent content; // getter and setter
public StreamedContent getImageF() {
if (student.getImage() != null) {
InputStream is = new ByteArrayInputStream(student.getImage());
System.out.println("Byte :"+student.getImage());
content = new DefaultStreamedContent(is, "", student.getStuID());
System.out.println("ddd ------------------------------- " + content);
return content;
}
return content;
}
This returns a blank image. How is this caused and how can I solve it?
The stdout prints the following:
INFO: Byte :[B#a2fb48
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent#b0887b
INFO: Byte :[B#a2fb48
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent#1d06a92
INFO: Byte :[B#d52f0b
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent#39a60
INFO: Byte :[B#d52f0b
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent#8c3daa
INFO: Byte :[B#124728a
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent#1dbe05b
INFO: Byte :[B#124728a
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent#66a266
INFO: Byte :[B#a2fb48
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent#1293976
INFO: Byte :[B#a2fb48
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent#17b7399
INFO: Byte :[B#d52f0b
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent#1e245a5
INFO: Byte :[B#d52f0b
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent#4a7153
INFO: Byte :[B#124728a
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent#1561bfd
INFO: Byte :[B#124728a
INFO: ddd ------------------------------- org.primefaces.model.DefaultStreamedContent#47a8c2
The <p:graphicImage> requires a special getter method. It will namely be invoked twice per generated image, each in a completely different HTTP request.
The first HTTP request, which has requested the HTML result of a JSF page, will invoke the getter for the first time in order to generate the HTML <img> element with the right unique and auto-generated URL in the src attribute which contains information about which bean and getter exactly should be invoked whenever the webbrowser is about to request the image. Note that the getter does at this moment not need to return the image's contents. It would not be used in any way as that's not how HTML works (images are not "inlined" in HTML output, but they are instead requested separately).
Once the webbrowser retrieves the HTML result as HTTP response, it will parse the HTML source in order to present the result visually to the enduser. Once the webbrowser encounters an <img> element during parsing the HTML source, then it will send a brand new HTTP request on the URL as specified in its src attribute in order to download the content of that image and embed it in the visual presentation. This will invoke the getter method for the second time which in turn should return the actual image content.
In your particular case PrimeFaces was apparently either unable to identify and invoke the getter in order to retrieve the actual image content, or the getter didn't return the expected image content. The usage of #{item} variable name and the lot of calls in the log suggests that you were using it in an <ui:repeat> or a <h:dataTable>. Most likely the backing bean is request scoped and the datamodel isn't properly preserved during the request for the image and JSF won't be able to invoke the getter during the right iteration round. A view scoped bean would also not work as the JSF view state is nowhere available when the browser actually requests the image.
To solve this problem, your best bet is to rewrite the getter method as such so that it can be invoked on a per-request basis wherein you pass the unique image identifier as a <f:param> instead of relying on some backing bean properties which may go "out of sync" during subsequent HTTP requests. It would make completely sense to use a separate application scoped managed bean for this which doesn't have any state. Moreover, an InputStream can be read only once, not multiple times.
In other words: never declare StreamedContent nor any InputStream or even UploadedFile as a bean property; only create it brand-new in the getter of a stateless #ApplicationScoped bean when the webbrowser actually requests the image content.
E.g.
<p:dataTable value="#{bean.students}" var="student">
<p:column>
<p:graphicImage value="#{studentImages.image}">
<f:param name="studentId" value="#{student.id}" />
</p:graphicImage>
</p:column>
</p:dataTable>
Where the StudentImages backing bean can look like this:
#Named // Or #ManagedBean
#ApplicationScoped
public class StudentImages {
#EJB
private StudentService service;
public StreamedContent getImage() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
// So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL.
return new DefaultStreamedContent();
}
else {
// So, browser is requesting the image. Return a real StreamedContent with the image bytes.
String studentId = context.getExternalContext().getRequestParameterMap().get("studentId");
Student student = studentService.find(Long.valueOf(studentId));
return new DefaultStreamedContent(new ByteArrayInputStream(student.getImage()));
}
}
}
Please note that this is a very special case wherein performing business logic in a getter method is completely legit, considering how the <p:graphicImage> works under the covers. Invoking business logic in getters is namely usually frowned upon, see also Why JSF calls getters multiple times. Don't use this special case as excuse for other standard (non-special) cases. Please also note that you can't make use of EL 2.2 feature of passing method arguments like so #{studentImages.image(student.id)} because this argument won't end up in the image URL. Thus you really need to pass them as <f:param>.
If you happen to use OmniFaces 2.0 or newer, then consider using its <o:graphicImage> instead which can be used more intuitively, with an application scoped getter method directly delegating to the service method and supporting EL 2.2 method arguments.
Thus so:
<p:dataTable value="#{bean.students}" var="student">
<p:column>
<o:graphicImage value="#{studentImages.getImage(student.id)}" />
</p:column>
</p:dataTable>
With
#Named // Or #ManagedBean
#ApplicationScoped
public class StudentImages {
#EJB
private StudentService service;
public byte[] getImage(Long studentId) {
return studentService.find(studentId).getImage();
}
}
See also the blog on the subject.
Try including a mime type. In your posted example, you have it as "". The blank image may be because it doesn't recognize the stream as a image file since you made that field an empty string. So add a mime type of image/png or image/jpg and see if that works:
String mimeType = "image/jpg";
StreamedContent file = new DefaultStreamedContent(bytes, mimeType, filename);
There's a couple possibilities here (and please post the entire class if this isn't it).
1) You're not initializing the image properly
2) Your stream is empty so you're getting nothing
I'm assuming student.getImage() has a signature of byte[] so first make sure that that data is actually intact and represents an image. Secondly--you're not specifying a content-type which should be "image/jpg" or whatever you're using.
Here's some boilerplate code to check it with, I'm using Primefaces 2 for this.
/** 'test' package with 'test/test.png' on the path */
#RequestScoped
#ManagedBean(name="imageBean")
public class ImageBean
{
private DefaultStreamedContent content;
public StreamedContent getContent()
{
if(content == null)
{
/* use your database call here */
BufferedInputStream in = new BufferedInputStream(ImageBean.class.getClassLoader().getResourceAsStream("test/test.png"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
int val = -1;
/* this is a simple test method to double check values from the stream */
try
{
while((val = in.read()) != -1)
out.write(val);
}
catch(IOException e)
{
e.printStackTrace();
}
byte[] bytes = out.toByteArray();
System.out.println("Bytes -> " + bytes.length);
content = new DefaultStreamedContent(new ByteArrayInputStream(bytes), "image/png", "test.png");
}
return content;
}
}
and some markup...
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui"
>
<h:head>
</h:head>
<h:body>
<p:graphicImage value="#{imageBean.content}" />
</h:body>
</html>
If that code works then you're set up properly. Despite the fact it is garbage code for the streams (don't use it in production) it should give you a point to troubleshoot from. My guess is that you might have something happening in your JPA or other Database framework where you're byte[] is empty or it is formatted wrong. Alternatively you could just have a content-type problem.
Lastly, I would clone the data from the bean so that student.getImage() would only be copied into a new array and then used. This way if you have something unknown going on (something else moving the object or changing the byte[] you're not messing with your streams.
Do something like:
byte[] data = new byte[student.getImage().length]
for(int i = 0; i < data.length; i++)
data[i] = student.getImage()[i];
so that your bean has a copy (or Arrays.copy()--whatever floats your boat). I can't stress enough how something simple like this/content type is usually what's wrong. Good luck with it.
The answer from BalusC is (as usual) the correct one.
But keep one thing (as already stated by him) in mind. The final request is done from the browser to get the URL from the constructed <img> tag. This is not done in a 'jsf context'.
So if you try to e.g. access the phaseId (logging or whatever reason)
context.getCurrentPhaseId().getName()
This will result in a NullPointerException and the somehow misleading error message you will get is:
org.primefaces.application.resource.StreamedContentHandler () - Error in streaming dynamic resource. Error reading 'image' on type a.b.SomeBean
It took me quite some time to figure out what was the problem.
A week ago, I have studied about ViewExpiredException, and I've read several things about it.
viewExpiredException JSF
How to control web page caching, across all browsers?
Session timeout and ViewExpiredException handling on JSF/PrimeFaces ajax request
My problem, which is some cases, I would like to ignore the ViewExpiredException. These are situations that do not need a "session", and my Beans are #RequestScoped. As an example, the pages login.xhtml, register.xhtml and passwordRecovery.xhtml.
In these cases, it is very strange display an error to the user saying that your session has expired. So if you open the login page and stand still for a while, when he inform your data and click Login, it would be forwarded to an error page. I would just ignore it and let transparent to the user.
So, my solution so far is create a ExceptionHandler to ignore these exceptions:
#Override
public void handle() throws FacesException {
for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
ExceptionQueuedEvent event = i.next();
ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
Throwable t = context.getException();
// just remove the exception from queue
if (t instanceof ViewExpiredException) {
i.remove();
}
}
getWrapped().handle();
}
Then, I created a filter to check whether the user is logged in, if not then redirected to login page (This filter applies only pages that require authentication):
#Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
if (!loginManagedBean.isLogged()) {
String pathLogin = request.getContextPath() + "/" + LOGIN_VIEW;
if (isAJAXRequest(request)) {
response.setContentType("text/xml");
response.getWriter()
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.printf("<partial-response><redirect url=\"%s\"></redirect></partial-response>", pathLogin);
return;
}
pathLogin += "?source=" + request.getServletPath();
response.sendRedirect(pathLogin);
return;
}
chain.doFilter(request, response);
}
So when the session expires, does not affect the user experience in the login and registration pages. And on pages I wish session, are handled by the filter.
That would be a good solution? Are there any security risk to ignore ViewExpiredException in a ExceptionHandler?
Ignoring them is not technically bad in this specific case, but it indicates a bad design. It's as if you're using the wrong tool for the job. I.e. those views should actually never expire.
Just make specifically those views stateless.
<f:view transient="true">
...
</f:view>
This can be placed anywhere in the page, even duplicated, but most self-documenting is making it the top level tag of the page, composition or definition.
See also:
What is the usefulness of statelessness in JSF?
I am trying to implement a "generic" view where (part of) the content displayed depends on the URL. E.g.
If /somepath/somepage.xhtml points to a non existing file, instead of going straight to a 404 error I want to try to retrieve /somepath/somepage.xhtml's content from the database using a generic view, /genericview.xhtml, where I have something like:
<h:outputText value="#{genericViewBean.content_lg}"
escape="false" />
which, if found by the backing bean, would output the content of the database entry from a tgenericcontent table, depending on the originally requested viewId:
webpath | content
/somepath/somepage.xhtml | <p>This is a test</p>
/someotherpath/someotherpage.xhtml | <p>A different test</p>
If the view content is not found in that table then the standard 404 error would be returned.
The closest I got wast to clone /genericview.xhtml changing only the file path (for example, to /somepath/somepage.xhtml). But that gets me one exact copy of the file per view, it is quite messy, and it doesn't allow me to create a new url just by adding an entry to my database.
How can I get the same result without cloning /genericview.xhtml?
(P.S: I have read about prettyfaces, but isn't there a simpler solution?)
For that, normally a servlet filter is being used. PrettyFaces, UrlRewriteFilter and FacesViews also do it that way.
You can get the request URI by HttpServletRequest#getRequestURI(). You can check the existence of a web resource by ServletContext#getResource() which will return null on non-existent resources. If the resource exists, just continue the request by FilterChain#doFilter(), else forward the request to the generic view by RequestDispatcher#forward().
All in all, this is how the filter could look like:
#WebFilter("/*")
public class GenericViewFilter implements Filter {
#Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
String relativeRequestURI = request.getRequestURI().substring(request.getContextPath().length());
boolean resourceExists = request.getServletContext().getResource(relativeRequestURI) != null;
boolean facesResourceRequest = request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER));
if (resourceExists || facesResourceRequest) {
chain.doFilter(request, response);
}
else {
request.getRequestDispatcher("/genericview.xhtml").forward(request, response);
}
}
// ...
}
In the /genericview.xhtml, the original request URI is available as request attribute keyed with RequestDispatcher#FORWARD_REQUEST_URI. You could use it in #PostConstruct of backing bean associated with the view in order to pull the right content from the DB.
String originalRequestURI = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI);
// ...
I must have utf-8 characters in url.
For example there is a string that is to be put in url:
"Hayranlık"
I thought to encode it:
try{
selected=URLEncoder.encode(selected,"UTF-8");
}catch(Exception e){
e.printStackTrace();
}
try {
FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + serverUrl +"/myjsfpage.jsf?param=" + selected );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I debug it and I see an expected string : Hayranl%C4%B1k%24
In another controller, I must handle it, so I get the url by
HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
String selected = (String)req.getParameter("param");
if(selected ==null){
//show no result message
return;
}
After that i try to decode it, but "before" decoding, my string that I get from url is something like "Hayranlık$".
try{
selected=URLDecoder.decode(selected,"UTF-8");
}catch(Exception e){
e.printStackTrace();
}
Does JSF redirection cause the problem, or is the browser URL handling the problem?
It's the servletcontainer itself who decodes HTTP request parameters. You don't and shouldn't need URLDecoder for this. The character encoding used for HTTP request parameter decoding needs for GET requests to be configured in the servletcontainer configuration.
It's unclear which one you're using, but based on your question history, it's Tomcat. In that case, you need to set the URIEncoding attribute of <Connector> element in Tomcat's /conf/server.xml to UTF-8.
<Connector ... URIEncoding="UTF-8">
See also:
Unicode - How to get the characters right?
Unrelated to the concrete problem, the way how you pulled the request parameter in JSF is somewhat clumsy. The following is simpler and doesn't introduce a Servlet API dependency in your JSF managed bean:
String selected = externalContext.getRequestParameterMap().get("param");
Or, if you're in a request scoped bean, just inject it by #ManagedProperty.
#ManagedProperty("#{param.param}")
private String param;
(note that #{param} is an implicit EL object referring the request parameter map and that #{param.param} merely returns map.get("param"); you might want to change the parameter name to make it more clear, e.g. "?selected=" + selected and then #{param.selected})
Or if you're in a view scoped bean, just set it by <f:viewParam>.
<f:viewParam name="param" value="#{bean.param}" />
See also:
ViewParam vs #ManagedProperty(value = "#{param.id}")