View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.desktop.impl;
18  
19  import java.io.IOException;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.jetspeed.desktop.JetspeedDesktop;
24  import org.apache.jetspeed.pipeline.PipelineException;
25  import org.apache.jetspeed.pipeline.valve.AbstractValve;
26  import org.apache.jetspeed.pipeline.valve.ValveContext;
27  import org.apache.jetspeed.request.RequestContext;
28  
29  /***
30   * DesktopEncoderRedirect Valve
31   * 
32   * if request parameter encoder=desktop is NOT defined,
33   *    redirect to same url with /desktop pipeline,
34   * otherwise,
35   *    just invoke next valve
36   * 
37   * Used by the /render pipeline (desktop-render-pipeline) to allow
38   * render requests that are not initiated via desktop javascript code to result
39   * in a page level navigation to the /desktop pipeline with the correct portlet rendering
40   * indicated in the original url. The encoder=desktop request parameter
41   * is used by desktop javascript code to indicate that the request is an "official"
42   * desktop ajax request. 
43   *
44   * @author <a href="mailto:smilek@apache.org">Steve Milek</a>
45   * @version $Id: $
46   */
47  public class DesktopEncoderRedirectValveImpl extends AbstractValve
48  {
49      protected Log log = LogFactory.getLog(DesktopEncoderRedirectValveImpl.class);
50      
51      private String desktopPipelinePath = null;
52      private String desktopRenderPipelinePath = null;
53      
54      public DesktopEncoderRedirectValveImpl( String desktopPipelinePath, String desktopRenderPipelinePath )
55      {
56          if ( desktopPipelinePath == null || desktopPipelinePath.length() == 0 )
57              desktopPipelinePath = JetspeedDesktop.DEFAULT_DESKTOP_PIPELINE_PATH;
58          if ( desktopPipelinePath.charAt( 0 ) != '/' )
59              desktopPipelinePath = "/" + desktopPipelinePath;
60          if ( desktopPipelinePath.charAt( desktopPipelinePath.length() -1 ) != '/' )
61              desktopPipelinePath = desktopPipelinePath + "/";
62  
63          if ( desktopRenderPipelinePath == null || desktopRenderPipelinePath.length() == 0 )
64              desktopRenderPipelinePath = JetspeedDesktop.DEFAULT_DESKTOP_RENDER_PIPELINE_PATH;
65          if ( desktopRenderPipelinePath.charAt( 0 ) != '/' )
66              desktopRenderPipelinePath = "/" + desktopRenderPipelinePath;
67          if ( desktopRenderPipelinePath.charAt( desktopRenderPipelinePath.length() -1 ) != '/' )
68              desktopRenderPipelinePath = desktopRenderPipelinePath + "/";
69          
70          this.desktopPipelinePath = desktopPipelinePath;
71          this.desktopRenderPipelinePath = desktopRenderPipelinePath;
72      }
73          
74      public void invoke( RequestContext request, ValveContext context )
75          throws PipelineException
76      {
77          try
78          {  
79              if ( request.getPortalURL() == null )
80              {
81                  String encoding = request.getRequestParameter(JetspeedDesktop.DESKTOP_ENCODER_REQUEST_PARAMETER);
82                  if (encoding == null || ! encoding.equals(JetspeedDesktop.DESKTOP_ENCODER_REQUEST_PARAMETER_VALUE))
83                  {
84                      // redirect to page url with render encoding
85                      try 
86                      {
87                          String queryString = request.getRequest().getQueryString();
88                          String location = request.getRequest().getRequestURI();
89                          if ( queryString != null && queryString.length() > 0 )
90                              location += "?" + queryString;
91                          location = location.replaceAll( this.desktopRenderPipelinePath, this.desktopPipelinePath );
92                          //log.info( "DesktopEncoderRedirectValveImpl redirecting request-uri=" + request.getRequest().getRequestURI() + " location=" + location );
93                          request.getResponse().sendRedirect( location );
94                      }
95                      catch (IOException ioe){}
96                      return;
97                  }                
98              }
99          }
100         catch (Exception e)
101         {
102             throw new PipelineException(e);
103         }
104         // Pass control to the next Valve in the Pipeline
105         context.invokeNext( request );
106     }
107 
108     public String toString()
109     {
110         return "DesktopValve";
111     }
112 }