I want to make a Cross-Domain request to download a file from my java web application. I am using the CORS filter and have included the configuration in my deployment descriptor(web.xml):
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
I have the cross domain URL in the href attribute:
download
Now when I click the link, the whole page is redirected and the file is downloaded. Can I know what how can I achive this.
Thanks
I faced a similar problem sometime ago. You will not be able to download the file using a GET request using href attribute as u mentioned in the question.
Instead you can submit a form embedded in an iframe as suggested here and submit the form with your file id as a hidden element and it should trigger the download. So your anchor tag shall be:
download
Now pass your fileId as parameter to the onclick JS function and define it like this:
function downloadFile(fileId) {
let $iframe, iframeDoc, iframeHtml;
if(($iframe = $('#download_frame')).length === 0) {
$iframe = $('<iframe id="download_frame" +
' style="display: none;" src="about:blank"></iframe>'
).appendTo('body');
}
iframeDoc= $iframe[0].contentWindow;
if (iframeDoc.document) {
iframeDoc= iframeDoc.document;
}
iframeHtml= '<html><head></head><body><form method="POST" action="http:localhost:8080/MyProject/downloadAction.action"><input type="hidden" name="fileId" value="'+fileId+'"></form></body></html>';
iframeDoc.open();
iframeDoc.write(iframeHtml);
$(iframeDoc).find('form').submit();
}
You can follow similar approach for single or multiple files.
Also to add you can include this in ur web.xml in the filter configuration:
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,X-Test-Header,Cache-Control
</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials
</param-value>
</init-param>
Related
I've got the following problem on every single page I am trying to access
WARNING [http-nio-8080-exec-7]
org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping
found for HTTP request with URI
[/cart/%3Cc:url%20value='/resources/css/bootstrap.css'%20/%3E] in
DispatcherServlet with name 'springmvc-dispatcher
There are warnings, but those resources are visible (both css and scripts are working, still gives errors), and I have nothing underlined in jsp files.
<link href="<c:url value='/resources/css/bootstrap.css' />" rel="stylesheet"/>
<script src="<c:url value="/resources/js/controllers.js"/>"></script>
I have above paths to css and js in every jsp file.
Project-Structure
And my filter mapping:
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
You haven't used DispatcherServlet in your mapping. Just try this.
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
I have a JSF page which posts data to an external page.
The data is loaded from a JSF managed bean which generates a unique ID in the post data.
I have an issue where a user clicks on a checkout button then navigates back to the same page and presses the checkout button again. The post data has not updated. Moreover, the bean is not invoked at all. Is there anyway to force JSF to reload the page and the form data?
<form action="#{checkoutBean.externalUrl}" method="post"
id="payForm" name="payForm">
<input type="hidden" value="#{checkoutBean.uniqueID}" />
<input type="submit" value="Proceed to Checkout" />
</form>
That page is likely being loaded from browser cache. This is essentially harmless, but indeed confusing to the enduser, because s/he incorrectly thinks that it's really coming from the server. You can easily confirm this by looking at the HTTP traffic monitor in browser's web developer toolset (press F12 in Chrome/FireFox23+/IE9+ and check "Network" section).
You basically need to tell the browser to not cache (dynamic) JSF pages. This way the browser will actually request the server for the page (and hereby triggering proper creation/initialization of managed beans and so forth) instead of showing the previously requested one from its cache.
Generally, this is to be done with a simple servlet filter like follows:
#WebFilter("/app/*")
public class NoCacheFilter implements Filter {
#Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
if (!request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) { // Skip JSF resources (CSS/JS/Images/etc)
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0); // Proxies.
}
chain.doFilter(req, res);
}
// ...
}
Where /app/* is the example URL pattern on which you'd like to turn off the browser cache. You can if necessary map it on /*, *.xhtml or even on servletNames={"Faces Servlet"}.
If you happen to use JSF utility library OmniFaces, then you can use its builtin CacheControlFilter by just adding the following entry to web.xml (which demonstrates a direct mapping on FacesServlet, meaning that every dynamic JSF page won't be cached):
<filter>
<filter-name>noCache</filter-name>
<filter-class>org.omnifaces.filter.CacheControlFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>noCache</filter-name>
<servlet-name>facesServlet</servlet-name>
</filter-mapping>
See also the showcase.
I found a solution that works for JSF without having to create a servlet-filter. Just put the line below to your .xhtml page.
<f:event type="preRenderView" listener="#{facesContext.externalContext.response.setHeader('Cache-Control', 'no-cache, no-store')}" />
I am using primefaces 3.3.1 and liferay 6.
In my portlet I want to add fileUpload for primefaces. But each time whenever my page loads this primefaces component is not visible through firebug. I need to make its display to block.
Also, any components below the fileUpload which consists of javascript method or ajax is not working.
I have noticed that in firebug whenever my page loads it shows me one error such as
this.form.fileUpload is not a function
I guess due to this error my fileUpload is not working. But I am not able to to fix it.
<p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}" mode="advanced" fileLimit="3"/>
Upgrade your primefaces to version 3.5 and add commons-fileupload to your project
I think it caused by you didn't add the commons-fileupload and commons-io in your project lib. Also, don't forget do define FileUploadFilter in your web.xml.
Let me show you how i did file upload in my primefaces before, hope this example can solve your problem.
My xhtml page :
<p:fileUpload allowTypes="/(\.|\/)(csv)$/" fileUploadListener="#{fileBean.handleFileUpload}"/>
My FileBean.java :
public class FileBean {
private UploadedFile file;
public void handleFileUpload(FileUploadEvent event) throws Exception {
file = event.getFile();
InputStream input = file.getInputstream();
OutputStream output = new FileOutputStream(new File("D:/UploadedFile/", file.getFileName()));
IOUtils.copy(input, output);
}
Add this to your web.xml :
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>
org.primefaces.webapp.filter.FileUploadFilter
</filter-class>
</filter>
And as I said before, add commons-fileupload and commons-io to your project lib.
Fixed this issue in Liferay Portal 6.0.x.
Can you add this code to web.xml and try it?
<!-- Workaround for PrimeFaces 3.2: -->
<context-param>
<param-name>com.liferay.faces.bridge.primeFileUploadForceResourceURL</param-name>
<param-value>true</param-value>
</context-param>
Here is link for more information.
After trying everything else concerning web.xml,pom.xml,... I came back to the obvious things :-)
Maybe you are just forgetting to wrap an form around your upload elements:
<h:form enctype="multipart/form-data">
<p:fileUpload mode="simple" value="#{mgr.file}"/>
<p:commandButton value="Hochladen" ajax="false" onclick="start();"
action="#{mgr.uploadBescheid(bescheidTyp)}" update="growl"/>
</h:form>
Without that point it won't work...
I use a HttpServlet to generate an html image dependent on multiple IDs like this:
<img src="./someServlet?ids=123,124,125,126[...]" alt=""/>
someServlet extends from javax.servlet.http.HttpServlet overwriting the doGet()and doPost() methods. It sets the response contenttype to img/png and uses the response outputstream to commit the generated image to the view.
The servlet mapping is done in web.xml:
<servlet>
<servlet-name>SomeServlet</servlet-name>
<servlet-class>my.package.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SomeServlet</servlet-name>
<url-pattern>/someServlet</url-pattern>
</servlet-mapping>
My question: How do I send the request above via POST instead of GET? I tried surrounding it by the <form> tag setting the method to POST, but as expected, it didn't work.
EDIT: I need this because my request (with 5-10 UUIDS) exceeds the limit of a GET request
You cannot change an <img> element to send a POST request instead of GET. This makes no sense.
As per the comment on the question the query string length limit seems to be the main reason:
I want to use POST, because the get parameter length is limited. I commit 5-10 UUIds as a parameter, which exceeds the length of a GET request.
Just pass it as part of URL instead, as path info. So, instead of
<img src="someServlet?id1=123&id2=234&id3=345&id4=456&id5=567" alt=""/>
use
<img src="someServlet/123/234/345/456/567" alt=""/>
You only need to change your servlet's URL pattern to
<url-pattern>/someServlet/*</url-pattern>
and change the way to obtain the IDs as follows
String[] ids = request.getPathInfo().substring(1).split("/");
// ...
Ok, I've run across my first StackOverflowError since joining this site, I figured this is a must post :-). My environment is Seam 2.0.1.GA, JBoss 4.2.2.GA and I'm using JSF. I am in the process of converting from a facelets view to JSP to take advantage of some existing JSP tags used on our existing site. I changed the faces-config.xml and the web.xml configuration files and started to receive the following error when trying to render a jsp page. Anyone have any thoughts?
2008-09-17 09:45:17,537 DEBUG
[org.jboss.seam.contexts.FacesLifecycle]
Begin JSF request for /form_home.jsp
2008-09-17 09:45:17,587 ERROR
[org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/].[Faces
Servlet]] Servlet.service() for
servlet Faces Servlet threw exception
java.lang.StackOverflowError
at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:210)
at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:222)
at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:222)
at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:222)
...
My faces-config.xml file is now empty with no FaceletsViewHandler:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
</faces-config>
And my Web.xml file:
<?xml version="1.0"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Ajax4jsf -->
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>blueSky</param-value>
</context-param>
<!-- Seam -->
<listener>
<listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
</listener>
<filter>
<filter-name>Seam Filter</filter-name>
<filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Seam Filter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>Seam Resource Servlet</servlet-name>
<servlet-class>org.jboss.seam.servlet.SeamResourceServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Seam Resource Servlet</servlet-name>
<url-pattern>/seam/resource/*</url-pattern>
</servlet-mapping>
<!-- Seam end -->
<!-- JSF -->
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.jsp</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
I was able to figure out this problem. Apparently you can not configure web.xml to have the same param-value of .jsp for Javax.faces.DEFAULT_SUFFIX as the Faces Servlet url-pattern (*.jsp). If you change your url-pattern to .jspx or to /whateverdirnameyouwant/ the application starts up with no stack overflow errors. (note: the key is that DEFAULT_SUFFIX and Faces Servlet url-pattern cannot be the same regardless of what they are.) Hope this helps anyone else that experiences this specific problem.
Stack overflows in java are almost always caused by infinite recursion / method calls. In your case given the stack trace, it appears 'getAttribute()' is being called repeatedly until crash. While I'm not intimately familiar with the particular environments you are using, I would suggest checking your .jsp code for any of this type of behaviour (for example two methods that call each other)
So, I had a similar error. For me, it was that I had a JSF project and I was messing around with the file extensions. To start with, I had all my web files with extension .jsp. This was working, but then I wanted them to be all .jsf, then after that I went all in on using .xhtml. In the process, my web.xml file changed to accomodate xhtml and jsf. Changing the web.xml file was fine. What got me the StackOverflowError was that I had index.xhtml with a ui.include tag pointing to header.jsf. So I had a xhtml file pointing to a jsf file. I had thought that web.xml would be able to handle this, but it did not, I got the StackOverflowError. So, to fix this, now all my JSF files have extension .xhtml, and nested ui:include tags point to .xhtml files.
On the flip side, though, the browser url can handle the index.jsp, index.jsf, index.xhtml just fine. So the web.xml (with servlet mappings for jsp, jsf and xhtml) handles the browser url just fine, but not for what my problem above highlighted.