掲示板

File download fails due to unsupported content types

12年前 に PortletFaces Community Member によって更新されました。

File download fails due to unsupported content types

Regular Member 投稿: 199 参加年月日: 12/04/03 最新の投稿
Hi Neil,
While working with JSF portlet bridge 2.0 in my JSF2.0 application I try to download some documents in portal mode.

When I try to download .txt file, it works fine. But lets say if I try to download .doc, .PPT files, I get below exception.

When I looked into the bridge code, I found that ExternalContextImpl has a method called getMimeType() which returns the content type to be set into reponse.

String mimeType = portletContext.getMimeType(fileName);

if ((mimeType == null) || (mimeType.length() == 0)) {
mimeType = FileNameUtil.getFileNameExtension(fileName);
}


here it first tries to get the Mime type from portlet context which is null in my case. Then it tries to call FileNameUtil.getFileNameExtension() method to get the MiMe types from a defined mime map.

here I see two issues.
1) Instead of getFileNameExtension, It should call getFileNameMimeType defined in the same class. Because calling the getFileNameExtension method returns the extension of the file and not the MiMe type hence it fails when it tries to set the content type by calling externalContextImpl's setResponseContentTpe method as shown below.

2) When I tried to fix the above problem i.e. now I am calling getFileNameMimeType to get the mime type of the file. But here we have only certain Mime Type defiend for some type of files. for e.g. it does not provide the Mime type for .doc files. Hence Still I will get the Mime type as null .

What should be the possible solution to fux this issue. Should I extend FileNameUtil's mimeTypeMap to support all possible File types to get correct MiMe type? Or how can I get MiMe type from portletContext.getMimeType(fileName); method?

Waiting for your valuable suggestions.

java.lang.IllegalArgumentException

org.portletfaces.bridge.BridgeException: java.lang.IllegalArgumentException
at BridgeImpl.java.doFacesRequest(BridgeImpl.java:304)
at org.portletfaces.bridge.GenericFacesPortlet.serveResource(GenericFacesPortlet.java:173)
at com.bea.portlet.container.PortletStub.doResource(PortletStub.java:973)
at com.bea.portlet.container.FilterChainGenerator.runFilterChain(FilterChainGenerator.java:138)
at com.bea.portlet.container.PortletStub.renderResource(PortletStub.java:719)
at com.bea.portlet.container.AppContainer.invokeResourceStub(AppContainer.java:1349)
at com.bea.portlet.container.AppContainer.invokeResource(AppContainer.java:1301)
at com.bea.wsrp.producer.adapter.javaportlet.JavaPortletAdapter.getResource(JavaPortletAdapter.java:736)
at com.bea.wsrp.producer.handlers.markup.MarkupServiceHandler.doGetResource(MarkupServiceHandler.java:713)
at com.bea.wsrp.producer.handlers.markup.MarkupServiceHandler.doService(MarkupServiceHandler.java:290)
at com.bea.wsrp.producer.handlers.AbstractServiceHandler.service(AbstractServiceHandler.java:68)
at com.bea.wsrp.producer.container.ProducerEndPoint.processNow(ProducerEndPoint.java:333)
at com.bea.wsrp.producer.wsee.ProducerEntryPoint.handleResponse(ProducerEntryPoint.java:90)
at weblogic.wsee.handler.HandlerIterator.handleResponse(HandlerIterator.java:287)
at weblogic.wsee.handler.HandlerIterator.handleResponse(HandlerIterator.java:271)
at weblogic.wsee.ws.dispatch.server.ServerDispatcher.callHandleResponse(ServerDispatcher.java:341)
at weblogic.wsee.ws.dispatch.server.ServerDispatcher.dispatch(ServerDispatcher.java:189)
at weblogic.wsee.ws.WsSkel.invoke(WsSkel.java:80)
at weblogic.wsee.server.servlet.SoapProcessor.handlePost(SoapProcessor.java:66)
at weblogic.wsee.server.servlet.SoapProcessor.process(SoapProcessor.java:44)
at weblogic.wsee.server.servlet.BaseWSServlet$AuthorizedInvoke.run(BaseWSServlet.java:285)
at weblogic.wsee.server.servlet.BaseWSServlet.service(BaseWSServlet.java:169)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at com.bea.content.manager.servlets.ContentServletFilter.doFilter(ContentServletFilter.java:178)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at com.bea.p13n.servlets.PortalServletFilter.doFilter(PortalServletFilter.java:336)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at com.bea.wsrp.producer.WsrpLocalProxyFilter.doFilter(WsrpLocalProxyFilter.java:72)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at com.csg.cs.corex.prefs.user.HttpUserPreferencesFilter.doFilter(HttpUserPreferencesFilter.java:60)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3592)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2202)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2108)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1432)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
Caused by: java.lang.IllegalArgumentException
at com.bea.wsrp.producer.container.ServletResponseImpl.setContentType(ServletResponseImpl.java:583)
at com.bea.portlet.container.HttpServletResponseImpl.setContentTypeInternal(HttpServletResponseImpl.java:208)
at com.bea.portlet.container.MimeResponseImpl.setContentTypeInternal(MimeResponseImpl.java:207)
at com.bea.portlet.container.ResourceResponseImpl.setContentType(ResourceResponseImpl.java:170)
at org.portletfaces.bridge.container.PortletContainerImpl.setMimeResponseContentType(PortletContainerImpl.java:388)
at org.portletfaces.bridge.context.ExternalContextImpl.setResponseContentType(ExternalContextImpl.java:1026)
at javax.faces.context.ExternalContextWrapper.setResponseContentType(ExternalContextWrapper.java:693)
at com.csg.jsf.application.ExportResourceHandler.handleResourceRequest(ExportResourceHandler.java:98)
at javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:125)
at org.apache.myfaces.custom.captcha.CAPTCHAResourceHandlerWrapper.handleResourceRequest(CAPTCHAResourceHandlerWrapper.java:208)
at com.csg.jsf.application.WebletsResourceHandler.handleResourceRequest(WebletsResourceHandler.java:144)
at BridgeImpl.java.doFacesRequest(BridgeImpl.java:278)
... 45 more
Caused by: javax.mail.internet.ParseException
at javax.mail.internet.ContentType.(ContentType.java:102)
at com.bea.wsrp.producer.container.ServletResponseImpl.setContentType(ServletResponseImpl.java:580)
... 56 more
thumbnail
12年前 に Neil Griffin によって更新されました。

RE: File download fails due to unsupported content types

Liferay Legend 投稿: 2655 参加年月日: 05/07/27 最新の投稿
For an example of how to download a binary file from a JSF2 portlet, please check out this example:
http://www.liferay.com/community/liferay-projects/liferay-faces/demos#jsf2-export-pdf-portlet
12年前 に PortletFaces Community Member によって更新されました。

RE: File download fails due to unsupported content types

Regular Member 投稿: 199 参加年月日: 12/04/03 最新の投稿
Thanks for your reply Neil.

I had a look at the sample. Here in CustomerExportResource, we explicitly define the content type as "application/pdf" for PDF download.

But in my case, I can download any file for example a word document or a PPT file. It is not always a PDF.

I am using similar mechanism for downloading the files as you mentioned.

my problem is Do I need to explicitly mention the content type of the file which needs to be download? In my case, file download is dynamic i.e. I use the same mechanism to download any kind of file for e.g. .txt file, .doc file, .PPT file etc. Hence I cannot hardcode it to use some specific content type.

Either my application provide all supported MIMe types in web.xml or Should I extend FileNameUtil's MimeTypesMap to support various other MIME types?
thumbnail
12年前 に Neil Griffin によって更新されました。

RE: File download fails due to unsupported content types

Liferay Legend 投稿: 2655 参加年月日: 05/07/27 最新の投稿
Yes, please checkout the bridge trunk and try these changes:

1. In ExternalContextImp.java, change this:

if ((mimeType == null) || (mimeType.length() == 0)) {
mimeType = FileNameUtil.getFileNameExtension(fileName);
}

to call getFileNameMimeType instead

2. In FileNameUtil.java add your different mime types to the mimeTypeMap

Let me know if that works for you and maybe we can fix the bridge.

Thanks!
12年前 に PortletFaces Community Member によって更新されました。

RE: File download fails due to unsupported content types

Regular Member 投稿: 199 参加年月日: 12/04/03 最新の投稿
Thanks Neil.

I will give it a try and let you know.

One more question, when would the below code (in bold )i.e.portletContext.getMimeType() will return MiME type value. In my case, It always returns null


public String getMimeType(String fileName) {
String mimeType = portletContext.getMimeType(fileName);

if ((mimeType == null) || (mimeType.length() == 0)) {
mimeType = FileNameUtil.getFileNameMimeType(fileName);
}

return mimeType;
}


Thanks.
thumbnail
12年前 に Neil Griffin によって更新されました。

RE: File download fails due to unsupported content types

Liferay Legend 投稿: 2655 参加年月日: 05/07/27 最新の投稿
The PortletContext#getMimeType(String) method is part of the Portlet 2.0 API, and is implemented by the underlying portlet container. I think in your case you're using BEA WebLogic Portal right? If so then the return value of the method is determined by the BEA WebLogic Portlet Container implementation. That code is proprietary, so we don't have access to the Java code for that, and can't answer your question about the return value.
12年前 に PortletFaces Community Member によって更新されました。

RE: File download fails due to unsupported content types

Regular Member 投稿: 199 参加年月日: 12/04/03 最新の投稿
Many Thanks for this information.
I will try extending the bridge code and let you know.
12年前 に PortletFaces Community Member によって更新されました。

RE: File download fails due to unsupported content types

Regular Member 投稿: 199 参加年月日: 12/04/03 最新の投稿
Hi Neil,

I tried as you suggested and it works.

I modified ExternalContextImpl's getMimeType method to call FileNameUtil.getFileNameMimeType(fileName);

Added content type in FileNameUtil's mimeTypeMap

static {
mimeTypeMap = new HashMap<String, String>();
mimeTypeMap.put("css", "text/css");
mimeTypeMap.put("js", "text/javascript");
mimeTypeMap.put("groovy", "application/x-groovy");
mimeTypeMap.put("properties", "text/plain");
mimeTypeMap.put("txt", "text/plain");
mimeTypeMap.put("doc", "application/msword"); } // Added by me to support word document file.

Should we extend this map to support for various other file types?
thumbnail
12年前 に Neil Griffin によって更新されました。

RE: File download fails due to unsupported content types

Liferay Legend 投稿: 2655 参加年月日: 05/07/27 最新の投稿
This has been fixed in FACES-255 and FACES-256 and will be in version 2.0.1 of the bridge.
12年前 に PortletFaces Community Member によって更新されました。

RE: File download fails due to unsupported content types

Regular Member 投稿: 199 参加年月日: 12/04/03 最新の投稿
Thanks you very much for this update.
9年前 に Koffi AGHOSTO によって更新されました。

RE: File download fails due to unsupported content types

Junior Member 投稿: 67 参加年月日: 14/05/27 最新の投稿
Hello Neil,

I meet the same problem with Liferay 6.2 and JSF 2.0. I try to send Csv file to the browser but I can't. I read
the example of pdf portlet to implement handler but I don't understand the mechanism, so I want to ask if
you can help to resolve this problem.

Thank in advanced,
Koffi
9年前 に Koffi AGHOSTO によって更新されました。

RE: File download fails due to unsupported content types

Junior Member 投稿: 67 参加年月日: 14/05/27 最新の投稿
Hello,

I have found solution of my problem and I want to share with you, maybe it will be usefull for
somebody.

To send my file to browser :

In my backing bean :

//Get the HttpServletRequest
PortletRequest portletRequest = (PortletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
HttpServletRequest httpServletRequest = com.liferay.portal.util.PortalUtil.getHttpServletRequest(portletRequest);

//Get the httpServletResponse
PortletResponse portletResponse = (PortletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
HttpServletResponse httpServletResponse = com.liferay.portal.util.PortalUtil.getHttpServletResponse(portletResponse);

//Send the file to browser
ServletResponseUtil.sendFile(httpServletRequest, httpServletResponse, file.getName(), new FileInputStream(file),ContentTypes.APPLICATION_VND_MS_EXCEL);


And in JSF View :

<p:commandButton ajax="false" value="Export to csv" action="#{myBackingBean.export}" icon="ui-icon-document" rendered="true" />


Thanks,
Best regards
thumbnail
8年前 に Kyle Joseph Stiemann によって更新されました。

Thread Split

Liferay Master 投稿: 760 参加年月日: 13/01/14 最新の投稿
Hi Mouad,

Please follow Forum Posting Guideline #11:
11. Zombie Threads. Don't revive old, dead threads with only remotely relevant information. Instead, start a new thread with a reference to the old/dead thread (if applicable).

I've moved your post to this new thread.

- Kyle