|
Home > Archive > WebSphere Portal Server > April 2005 > WCM and saved files
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
WCM and saved files
|
|
|
| Hi everybody,
I created some contents in WCM and I need to store some files with them.
Actually, I have an authoring template, with a text field containing the DocumentId of a LibraryFileComponent.
Manipulating a content, I'm able to get the DocumentId of the LibraryFileComponent. With this DocumentId, I'm able to get the data (a byte array) and the file name.
But I'm not able to get an URL to this component or to display the file on the screen (if it's an image for example)
I tried this kind of thing
[code]Map parametermap = new HashMap();
RenderingContext context = workspace.createRenderingContext(request, response, parametermap);
context.setWcmServletPath("/connect"); // from PortalServer/wcm/config/aptrixjpe.properties - wcm.servlet.path
context.setWcmWebAppPath("/wps/wcm"); // from PortalServer/wcm/config/aptrixjpe.properties - wcm.context.path
context.setRenderedContent(myFile);
System.err.println("ws.generateURL(context, fichier) : "+ws.generateURL(context, myFile));
System.err.println("ws.render(context) : "+ws.render(context));[/code]
And I get this :
for generateURL : a bad URL with "null" in the middle of it
for render : File Not Found
I don't manage to get something better.
So I want to do another way.
As the real files are hidden, but not their data, when I use the method
[i]JavaDoc wrote: getFile()
Returns the contents of the file contained within this component as a byte array.[/i]
I obtain a byte[] containing the data... That's what I want to use.
Method :
Open a new navigator window
put the good headers
put the data
That's what the page, which url is obtained by the render method, do. And that's what I want to reproduce
So I did this
[code]public class GenereFileOneAction extends Action {
public ActionForward execute
(ActionMapping mapping, ActionForm form,HttpServletRequest request,HttpServletResponse response)
throws Exception {
String documentIdString=request.getParameter("id");
Workspace ws=Outils.getWorkspace(request);
try{
DocumentId id=ws.createDocumentId(documentIdString);
if (documentIdString.indexOf("LibraryImageComponent")>-1){
LibraryImageComponent document=(LibraryImageComponent) ws.getById(id);
if (document!=null){
genereFile(document, response);
}
} else if (documentIdString.indexOf("LibraryFileComponent")>-1){
LibraryFileComponent document=(LibraryFileComponent) ws.getById(id);
if (document!=null){
genereFile(document, response);
}
}
} catch (Throwable t){
//t.printStackTrace();
}
return null;
}
public void genereFile(byte[] data, String fileName, HttpServletResponse response){
System.err.println("genereFile(..., "+fileName+", "+response+")");
String extension=null;
String contentType=null;
if (data!=null && fileName!=null){
int posPoint=fileName.lastIndexOf('.');
extension=fileName.substring(posPoint+1).toLowerCase();
if (extension.equals("asf")) contentType="video/x-ms-asf";
else if (extension.equals("avi")) contentType="video/avi";
else if (extension.equals("doc")) contentType="application/msword";
else if (extension.equals("zip")) contentType="application/zip";
else if (extension.equals("xls")) contentType="application/vnd.ms-excel";
else if (extension.equals("gif")) contentType="image/gif";
else if (extension.equals("jpg")) contentType="image/jpeg";
else if (extension.equals("jpeg")) contentType="image/jpeg";
else if (extension.equals("wav")) contentType="audio/wav";
else if (extension.equals("mp3")) contentType="audio/mpeg3";
else if (extension.equals("mpg")) contentType="video/mpeg";
else if (extension.equals("mpeg")) contentType="video/mpeg";
else if (extension.equals("rtf")) contentType="application/rtf";
else if (extension.equals("htm")) contentType="text/html";
else if (extension.equals("html")) contentType="text/html";
else if (extension.equals("asp")) contentType="text/asp";
else contentType="application/octet-stream";
response.reset();
response.setContentType(contentType);
response.setContentLength(data.length);
OutputStream os;
try {
os = response.getOutputStream();
System.err.print("response="+response+", os = "+os+", data=");
System.err.println(data+", extension="+extension+", contentType="+contentType);
if (os!=null){
os.write(data);
os.flush();
os.close();
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
public void genereFile(LibraryFileComponent file, HttpServletResponse response){
System.err.println("genereFile("+file+", "+response+")");
byte[] data=null;
String fileName=null;
if (file!=null){
try {
System.err.println("file!=null");
data = file.getFile();
fileName=file.getFileName();
genereFile(data, fileName, response);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
public void genereFile(LibraryImageComponent file, HttpServletResponse response){
System.err.println("genereFile("+file+", "+response+")");
byte[] data=null;
String fileName=null;
if (file!=null){
System.err.println("file!=null");
try {
data = file.getImage();
fileName=file.getImageFileName();
genereFile(data, fileName, response);
} catch (Throwable t) {
t.printStackTrace();
}
}
}[/code]
But the result is not the one I want.
Here is my log
[i]genereFile(com.ibm.workplace.wcm.api.WCM_LibraryImageComponent@71373fd7, com.ibm.wps.struts.base. WpsStrutsPortletResponseWrapper@2a9ffd6)
file!=null
genereFile(..., 2004-06-24-02.jpg, com.ibm.wps.struts.base. WpsStrutsPortletResponseWrapper@2a9ffd6)
response=com.ibm.wps.struts.base. WpsStrutsPortletResponseWrapper@2a9ffd6,
os = null, data=[B@b26ffd0, extension=jpg, contentType=image/jpeg[/i]
My questions are :
- does any one knows why response.getOutputStream (either response.getWriter) returns null
- when I click on a link to the action, the page on the screen is the last (just where I come from) but the Struts JavaDoc says about the execute methode
[i]Process the specified HTTP request, and create the corresponding HTTP response (or forward to another web component that will create it), with provision for handling exceptions thrown by the business logic. Return an ActionForward instance describing w
here and how control should be forwarded, or null if the response has already been completed.[/i]
So do you think I might have a return in my genereFile methods ?
Thank you
| |
| George Niece 2005-04-05, 5:54 pm |
| Long question.
Short answer.
Portal works in a two phase process, do business logic, then generate
display
Struts does both in in essentially one phase (please do not hammer me with
opinions about struts please)
This means that you have access to the response object just about anywhere
you are in Struts, however, in Struts portlets you only have that in the
appropriate phase, and hence no return output stream from the method because
of where you are trying to generate the output.
Good Luck,
geo
<asobrero@sysdeo.com> wrote in message
news:868813801.1112695900102.JavaMail.wassrvr@ltsgwas007.sby.ibm.com...
> Hi everybody,
> I created some contents in WCM and I need to store some files with them.
>
> Actually, I have an authoring template, with a text field containing the
DocumentId of a LibraryFileComponent.
>
> Manipulating a content, I'm able to get the DocumentId of the
LibraryFileComponent. With this DocumentId, I'm able to get the data (a byte
array) and the file name.
>
> But I'm not able to get an URL to this component or to display the file on
the screen (if it's an image for example)
>
> I tried this kind of thing
> [code]Map parametermap = new HashMap();
> RenderingContext context = workspace.createRenderingContext(request,
response, parametermap);
> context.setWcmServletPath("/connect"); // from
PortalServer/wcm/config/aptrixjpe.properties - wcm.servlet.path
> context.setWcmWebAppPath("/wps/wcm"); // from
PortalServer/wcm/config/aptrixjpe.properties - wcm.context.path
>
> context.setRenderedContent(myFile);
> System.err.println("ws.generateURL(context, fichier) :
"+ws.generateURL(context, myFile));
> System.err.println("ws.render(context) : "+ws.render(context));[/code]
>
> And I get this :
> for generateURL : a bad URL with "null" in the middle of it
> for render : File Not Found
>
> I don't manage to get something better.
>
> So I want to do another way.
>
> As the real files are hidden, but not their data, when I use the method
> [i]JavaDoc wrote: getFile()
> Returns the contents of the file contained within this component as a byte
array.[/i]
>
> I obtain a byte[] containing the data... That's what I want to use.
>
> Method :
> Open a new navigator window
> put the good headers
> put the data
>
> That's what the page, which url is obtained by the render method, do. And
that's what I want to reproduce
>
> So I did this
>
> [code]public class GenereFileOneAction extends Action {
> public ActionForward execute
> (ActionMapping mapping, ActionForm form,HttpServletRequest
request,HttpServletResponse response)
> throws Exception {
> String documentIdString=request.getParameter("id");
> Workspace ws=Outils.getWorkspace(request);
> try{
> DocumentId id=ws.createDocumentId(documentIdString);
> if (documentIdString.indexOf("LibraryImageComponent")>-1){
> LibraryImageComponent document=(LibraryImageComponent)
ws.getById(id);
> if (document!=null){
> genereFile(document, response);
> }
> } else if (documentIdString.indexOf("LibraryFileComponent")>-1){
> LibraryFileComponent document=(LibraryFileComponent)
ws.getById(id);
> if (document!=null){
> genereFile(document, response);
> }
> }
> } catch (Throwable t){
> //t.printStackTrace();
> }
> return null;
> }
>
> public void genereFile(byte[] data, String fileName,
HttpServletResponse response){
> System.err.println("genereFile(..., "+fileName+", "+response+")");
> String extension=null;
> String contentType=null;
> if (data!=null && fileName!=null){
> int posPoint=fileName.lastIndexOf('.');
> extension=fileName.substring(posPoint+1).toLowerCase();
>
> if (extension.equals("asf")) contentType="video/x-ms-asf";
> else if (extension.equals("avi")) contentType="video/avi";
> else if (extension.equals("doc"))
contentType="application/msword";
> else if (extension.equals("zip")) contentType="application/zip";
> else if (extension.equals("xls"))
contentType="application/vnd.ms-excel";
> else if (extension.equals("gif")) contentType="image/gif";
> else if (extension.equals("jpg")) contentType="image/jpeg";
> else if (extension.equals("jpeg")) contentType="image/jpeg";
> else if (extension.equals("wav")) contentType="audio/wav";
> else if (extension.equals("mp3")) contentType="audio/mpeg3";
> else if (extension.equals("mpg")) contentType="video/mpeg";
> else if (extension.equals("mpeg")) contentType="video/mpeg";
> else if (extension.equals("rtf")) contentType="application/rtf";
> else if (extension.equals("htm")) contentType="text/html";
> else if (extension.equals("html")) contentType="text/html";
> else if (extension.equals("asp")) contentType="text/asp";
> else contentType="application/octet-stream";
>
> response.reset();
> response.setContentType(contentType);
> response.setContentLength(data.length);
> OutputStream os;
> try {
> os = response.getOutputStream();
> System.err.print("response="+response+", os = "+os+", data=");
> System.err.println(data+", extension="+extension+",
contentType="+contentType);
> if (os!=null){
> os.write(data);
> os.flush();
> os.close();
> }
> } catch (Throwable t) {
> t.printStackTrace();
> }
> }
> }
>
> public void genereFile(LibraryFileComponent file, HttpServletResponse
response){
> System.err.println("genereFile("+file+", "+response+")");
> byte[] data=null;
> String fileName=null;
> if (file!=null){
> try {
> System.err.println("file!=null");
> data = file.getFile();
> fileName=file.getFileName();
> genereFile(data, fileName, response);
> } catch (Throwable t) {
> t.printStackTrace();
> }
> }
> }
>
> public void genereFile(LibraryImageComponent file, HttpServletResponse
response){
> System.err.println("genereFile("+file+", "+response+")");
> byte[] data=null;
> String fileName=null;
> if (file!=null){
> System.err.println("file!=null");
> try {
> data = file.getImage();
> fileName=file.getImageFileName();
> genereFile(data, fileName, response);
> } catch (Throwable t) {
> t.printStackTrace();
> }
> }
> }[/code]
>
> But the result is not the one I want.
>
> Here is my log
>
[i]genereFile(com.ibm.workplace.wcm.api.WCM_LibraryImageComponent@71373fd7,
com.ibm.wps.struts.base. WpsStrutsPortletResponseWrapper@2a9ffd6)
> file!=null
> genereFile(..., 2004-06-24-02.jpg,
com.ibm.wps.struts.base. WpsStrutsPortletResponseWrapper@2a9ffd6)
> response=com.ibm.wps.struts.base. WpsStrutsPortletResponseWrapper@2a9ffd6,
os = null, data=[B@b26ffd0, extension=jpg, contentType=image/jpeg[/i]
>
>
> My questions are :
> - does any one knows why response.getOutputStream (either
response.getWriter) returns null
> - when I click on a link to the action, the page on the screen is the last
(just where I come from) but the Struts JavaDoc says about the execute
methode
>
> [i]Process the specified HTTP request, and create the corresponding HTTP
response (or forward to another web component that will create it), with
provision for handling exceptions thrown by the business logic. Return an
ActionForward instance describing where and how control should be forwarded,
or null if the response has already been completed.[/i]
>
> So do you think I might have a return in my genereFile methods ?
>
> Thank you
|
|
|
|
|