6 Replies Latest reply on May 1, 2006 5:51 AM by chrholm

    Mapping URL's to a seam actions.

    markfrench

      Hi,

      I am attempting to migrate an existing web app which currently uses cgi to serve up image content and also return server side created javascript to the client. What I was wondering if it was possible to map a url to a seam action to do this.

      Cheers

      Mark

        • 1. Re: Mapping URL's to a seam actions.
          gavin.king

          What exactly do you mean? That was not enough information...

          • 2. Re: Mapping URL's to a seam actions.
            markfrench

            Basically the current ui makes a request to a cgi script for images and passes in some data. The script then returns image stream can this be replicated in seam. For example open url : localhost/images.cgi?id=1233. Hope this makes sense as I'm trying to avoid writing a servlet just for this purpose.

            • 3. Re: Mapping URL's to a seam actions.
              eekboom

              I am not sure if this is what you are after.
              Anyway here's a seam action I wrote to serve pdf files and avoid writing a servlet.
              I guess you should be able to simply change to other mime types like images.

              @Stateless(name = "ReportAction")
              @Name("_reportAction")
              @Interceptors(SeamInterceptor.class)
              @Local(ReportAction.class)
              public class ReportActionImpl implements ReportAction {
               @In("facesContext")
               private transient FacesContext _facesContext;
              
               @In(create = true)
               private ReportGenerator _reportGenerator;
              
               public String generateUserReport() {
               return generatePdfReport("http://foo.jrxml", "foo.pdf");
               }
              
               private String generatePdfReport(String urlText, String fileName) {
               try {
               ExternalContext external = _facesContext.getExternalContext();
               HttpServletResponse response = (HttpServletResponse) external.getResponse();
              
               URL url = new URL(urlText);
               byte[] data = _reportGenerator.generatePdfReport(url);
              
               configureResponse(response, fileName, data.length);
              
               ServletOutputStream out = response.getOutputStream();
               out.write(data);
               out.flush();
               _facesContext.responseComplete();
              
               return null;
               }
               catch(IOException e) {
               throw new RuntimeException("Cannot generate report: ", e);
               }
               }
              
               private void configureResponse(HttpServletResponse response, String fileName, int length) {
               response.setHeader("Expires", "0");
               response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
               response.setHeader("Pragma", "public");
               response.setContentType("application/pdf");
               response.addHeader("Content-disposition", "attachment; filename=\"" + fileName +"\"");
               response.setContentLength(length);
               }
              }


              • 4. Re: Mapping URL's to a seam actions.
                markfrench

                Hi,

                Thanks for that thats part of the puzzle I'm just not sure map a specific URL
                e.g. /ui/loadImage.cgi to always hit the action. Is possible using the faces-config.xml file or web.xml?


                Cheers

                Mark

                • 5. Re: Mapping URL's to a seam actions.
                  gavin.king

                  Use a Seam page action (this functionality is only in CVS).

                  <pages>
                   <page view-id="/ui/loadImage.jsp" action="#{my.action}"/>
                  </pages>


                  • 6. Re: Mapping URL's to a seam actions.
                    chrholm

                    Would it be possible to enhance SEAM to use annotations to achieve this? It would be very nice if you could annotate your ejb method with something like

                    @Page("/home.faces")