T2.3 contains a DirectResponseLayout class which provides the same
functionality. It is documented on the turbine wiki. 

Peter

On Wed, 2005-01-19 at 18:35 +0100, Martin Lidgard wrote:
> Direct response in Turbine 2.3 can be done as described below:
> 
> The class below handles requests for the file favicon.ico and returns the
> file of that name in the default skin directory. It can easily be adapted to
> handle returning of any binary file as a direct response, depending on the
> request.  We use a version of this class for returning different favicon.ico
> files depending on which server name is requested as we host several domains
> on the same servlet.
> 
> You need to replace the VelocityOnlyLayout by placing your
> CustomVelocityOnlyLayout (extends Layout) in the modules\layouts\ directory
> of your servlet. Copy the content of the VelocityOnlyLayout and replace the
> last line in the doBuild() method as follows:
> 
>         // remove this:
>         data.getOut().print(TurbineVelocity.handleRequest(context, prefix
> + templateName));
> 
>         // replace with this:
>         if (!data.isOutSet()) {
>             data.getOut().print(TurbineVelocity.handleRequest(context,
> prefix + templateName));
>         } else {
>             log.debug("doBuild(): data.declareDirectResponse() has been
> called, not doing anything");
>         }
> 
> This is to avoid the layout attempting to return a response after the direct
> response has been set.
> 
> Good luck!
> 
> /Martin
>  
> 
> Martin Lidgard
> Arkatay Consulting
> 
> ________________________________
> 
> Arkatay Consulting
> Magle Stora Kyrkogata 6
> SE 223 50 Lund
> Sweden        
> Cell: +46 739 373815
> Fax: +46 46 157716
> Mail: [EMAIL PROTECTED]
> Web: www.arkatay.com  
> 
> 
> 
> 
> <begin class Default.java>
> 
> package com.arkatay.tibet.modules.screens.test;
> 
> import org.apache.turbine.TurbineConstants;
> import org.apache.turbine.Turbine;
> import org.apache.turbine.modules.screens.VelocitySecureScreen;
> import org.apache.turbine.util.RunData;
> import org.apache.turbine.services.mimetype.MimeTypeService;
> import org.apache.turbine.services.TurbineServices;
> import org.apache.turbine.services.servlet.TurbineServlet;
> import org.apache.velocity.context.Context;
> 
> import javax.servlet.ServletOutputStream;
> import javax.servlet.http.HttpServletResponse;
> import javax.servlet.http.HttpServletRequest;
> import java.io.IOException;
> import java.io.FileInputStream;
> import java.io.FileNotFoundException;
> 
> /**
>  * Screen class for return files as direct responses 
>  * <p>
>  * The <code>isAuthorized()</code> method should be overridden to implement
> security checks
>  * @author  <a href="mailto:[EMAIL PROTECTED]">Martin Lidgard</a>
>  */
> public abstract class Default extends VelocitySecureScreen {
> 
> 
>     /**
>      * This method should be overridden to implement security checks
>      * @param   data        the RunData object for the request
>      * @return
>      */
>     protected boolean isAuthorized(RunData data) {
>         return true;
>     }
> 
> 
>     /**
>      * If the request is in the form
> <tt>http://www.mysite.com/favicon.ico</tt>,
>      * a direct response is returned with an appropriate favicon.ico file.
>      * <p>
>      * If successful, status code <tt>OK</tt> (200) is returned in the http
> header, 
>      * otherwise <tt>NOT_FOUND</tt> (404) is returned.
>      * @param   data        the RunData object for the request
>      * @param   context     the context to return to the screen template
>      */
>     public void doBuildTemplate(RunData data, Context context) {
>         String errorPageTemplate =
> Turbine.getConfiguration().getString(TurbineConstants.TEMPLATE_ERROR_KEY);
>         String errorPageScreen =
> Turbine.getConfiguration().getString(TurbineConstants.SCREEN_ERROR_KEY);
> 
>         HttpServletRequest request = data.getRequest();
>         String url = request.getRequestURL().toString();
>         int reqInfoLen = request.getScheme().length() + "://".length() +
> request.getServerName().length();
>         int requestPathStartPos = url.substring(reqInfoLen).indexOf("/") +
> reqInfoLen + 1;
>         String requestPath = url.substring(requestPathStartPos);
> 
>         if (requestPath.startsWith("favicon.ico")) {
>             String fileName = "favicon.ico";
>             // fix path using default skin
>             String relPath = "/resources/ui/skins/images/" + fileName;
>             try {
>                 boolean success = returnDirectResponseFile(fileName,
> relPath, data);
>                 if (success) {
>                     return;
>                 } else {
>                     // continue processing to error screen,
>                     // the file could not be written to the response output
> stream
>                 }
>             } catch (IOException e) {
>                 e.printStackTrace();
>                 log.error("doBuildTemplate(): failed writing direct response
> file " + relPath);
>                 // continue processing to error screen
>             }
>         }
> 
> 
>         // we were not able to determine any redirect page from the request,
> redirect to error screen
>         try {
>             data.setStatusCode(HttpServletResponse.SC_NOT_FOUND);
>             data.getResponse().setStatus(HttpServletResponse.SC_NOT_FOUND);
>             doRedirect(data, errorPageScreen , errorPageTemplate);
>         } catch (Exception e) {
>             e.printStackTrace();
>             log.error("doBuildTemplate(): failed redirecting to screen " +
> errorPageScreen + ", template " + errorPageTemplate + ", and requestPath " +
> requestPath);
>         }
>     }
> 
> 
>     /**
>      * Returns a direct response with the given file for downloading
>      * @param   fileName                the fileName of the file to return
>      * @param   relPath                 the relative path including the
> fileName of the file to return
>      * @param   data                    the RunData object with the
> HtttServletResponse to return the file with
>      * @return                          whether the writing to response
> output stream was successful or not
>      * @throws  FileNotFoundException   failed creating FileInputStream for
> file <var>filePath</var>
>      * @throws  IOException             failed opening OutputStream for file
> <var>filePath</var>
>      * @throws  IOException             failed writing to OutputStream for
> file <var>filePath</var>
>      * @throws  IOException             failed closing InputStream and
> OutputStream for file <var>filePath</var>
>      */
>     public boolean returnDirectResponseFile(String fileName, String relPath,
> RunData data) throws FileNotFoundException, IOException {
>         MimeTypeService mimeService = (MimeTypeService)
> TurbineServices.getInstance().getService(MimeTypeService.SERVICE_NAME);
> 
>         // get the full path to the input file to indicate what file is to
> be downloaded
>         String filePath = TurbineServlet.getRealPath(relPath);
> 
>         // open the file for reading
>         FileInputStream input = new FileInputStream(filePath); 
> 
>         // check the size of the file in bytes
>         int fileLen = input.available();
> 
>         // if the file was successfully read, initiate response procedure
>         if (fileLen > 0) {
>             data.declareDirectResponse();
>             HttpServletResponse response = data.getResponse();
> 
>             // set layout and screen classes and template to avoid getting
> warnings from velocity.
>             // the layouts and screens are never rendered as
> VelocityOnlyLayout
>             // notices that data.isOutSet() is set to true
>             this.setLayout(data, "DirectResponse");
>             data.setLayout("DirectResponse");
>             data.setLayoutTemplate("DirectResponse.vm");
>             data.setScreenTemplate("DirectResponse.vm");
>             data.getTemplateInfo().setLayoutTemplate("DirectResponse.vm");
> 
> 
>             int readCount;
>             byte fileBuffer[] = new byte[fileLen];
> 
>             // Setting content type and headers
>             // tell the browser what the file name is so that the "save as"
> dialog does the
>             // right thing (use the file name only, not the full path to the
> file!)
>             response.setHeader("Content-Disposition", "inline; filename=" +
> fileName);
> 
>             //response.setHeader("Content-Disposition", "attachment;filename
> = " + aFileName);
>             response.setContentType(mimeService.getContentType(fileName));
>             response.setContentLength(fileBuffer.length);
>             data.setStatusCode(HttpServletResponse.SC_OK);
>             response.setStatus(HttpServletResponse.SC_OK);
> 
>             // open the response stream
>             ServletOutputStream output = response.getOutputStream();
> 
>             // copy the file content to the response stream
>             // copy the file content from the input stream to the output
> stream
>             while ((readCount = input.read(fileBuffer)) != -1) {
>                 output.write(fileBuffer, 0, readCount);
>             }
> 
>             // close everything
>             output.flush();
>             output.close();
>             input.close();
>             log.debug("returnDirectReponseFile(): finished writing reponse
> file");
>             return true;
> 
>         } else {
> 
>             // file was empty
>             log.error("returnDirectReponseFile(): failed writing response
> file, there was nothing to read");
>             return false;
>         }
>     }
> }
> 
> 
> <end class Default.java>
> 
> 
> 
> -----Ursprungligt meddelande-----
> Fr�n: Henning P. Schmiedehausen [mailto:[EMAIL PROTECTED] 
> Skickat: den 19 januari 2005 16:11
> Till: [email protected]
> �mne: Re: RawScreen Problem
> 
> "Jeffrey D. Brekke" <[EMAIL PROTECTED]> writes:
> 
> >Point taken, thanks.  Maybe it is something else, but back when we ran 
> >into the problem (sep/oct), we noticed that RawScreen didn't work at 
> >all in 2.3 unless the presence of the Default.class page was around.  
> >There is a DefaultPage class but something is requiring a Default.class 
> >in the pages package, which I believe was the pre-2.3 situation.
> 
> RawScreen works fine without a Default.class page in 2.3.1 and probably also
> in 2.3. I do have an example (sorry, I know I've already promised this once;
> if you are willing to cope with an unpolished version, I can simply send you
> a tarball) to show how to do this.
> 
> >I was going to get around to writing up a test for RawScreen to show 
> >this but never did.  At the time there were no tests of the RawScreen 
> >classes in the tree, I haven't looked lately though.  I may be able to 
> >try to get it setup today again...
> 
> It would be great to get a test for this.
> 
>       Regards
>               Henning
> 
> 
> 
> >Courcoux Peter, Slough wrote:
> >> But be careful about this... Turbine can throw this exception when 
> >> some seemingly unrelated issue occurs, such as a service not starting 
> >> properly. I have seen this when the TurbinePull service has failed.
> >> 
> >> Regards,
> >> 
> >> Peter
> >> 
> >> -----Original Message-----
> >> From: Jeffrey D. Brekke [mailto:[EMAIL PROTECTED]
> >> Sent: 19 January 2005 14:07
> >> To: Turbine Users List
> >> Subject: Re: RawScreen Problem
> >> 
> >> If you're getting an exception about not finding the Default page 
> >> class, (we were) you could try to create one.  This worked for us:
> >> 
> >> ----
> >> 
> >> package [YOUR PACKAGE].modules.pages;
> >> 
> >> import org.apache.turbine.modules.pages.DefaultPage;
> >> 
> >> public class Default extends DefaultPage {}
> >> 
> >> ----
> >> 
> >> Nagendra Singh wrote:
> >> 
> >>>  
> >>>Hi !!
> >>>   I am not able to use rawscreen api in turbine 2.3. It shows class 
> >>>not
> >> 
> >> found exception. I am waiting for the solution.
> >> 
> >>>
> >>>
> >>>Nagendra Kumar Singh
> >>>Software Engineer
> >>>EE/ACES, IIT Kanpur
> >>>PH- +91-512-2597841
> >> 
> >> 
> >> 
> 
> 
> >--
> >=====================================================================
> >Jeffrey D. Brekke                                   [EMAIL PROTECTED]
> >Wisconsin,  USA                                     [EMAIL PROTECTED]
> >                                                     [EMAIL PROTECTED]
> >                                                     [EMAIL PROTECTED]
> 
> 
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
> 
> -- 
> Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
> [EMAIL PROTECTED]        +49 9131 50 654 0   http://www.intermeta.de/
> 
> RedHat Certified Engineer -- Jakarta Turbine Development  -- hero for hire
>    Linux, Java, perl, Solaris -- Consulting, Training, Development
> 
> What is more important to you...
>    [ ] Product Security
> or [ ] Quality of Sales and Marketing Support
>               -- actual question from a Microsoft customer survey
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
-- 
Peter Courcoux <[EMAIL PROTECTED]>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to