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.aggregator.impl;
18  
19  import java.io.BufferedReader;
20  import java.io.StringReader;
21  import java.util.Map;
22  
23  import javax.portlet.PortletException;
24  import javax.servlet.RequestDispatcher;
25  import javax.servlet.ServletContext;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.jetspeed.aggregator.PortletContent;
30  import org.apache.jetspeed.headerresource.HeaderResource;
31  import org.apache.jetspeed.portlet.PortletHeaderRequest;
32  import org.apache.jetspeed.portlet.PortletHeaderResponse;
33  import org.apache.jetspeed.request.RequestContext;
34  
35  
36  public class PortletHeaderResponseImpl implements PortletHeaderResponse
37  {
38      private RequestContext requestContext;
39      private HeaderResource hr;
40      private String tempContent;
41      
42      private boolean isDesktop;
43      
44      private Map headerConfiguration;
45      private Map headerResourceRegistry;
46      
47      public PortletHeaderResponseImpl( RequestContext requestContext, HeaderResource hr, boolean isDesktop, Map headerConfiguration, Map headerResourceRegistry )
48      {
49          this.requestContext = requestContext;
50          this.hr = hr;
51          this.isDesktop = isDesktop;
52          
53          this.headerConfiguration = headerConfiguration;
54          this.headerResourceRegistry = headerResourceRegistry;
55      }    
56  
57      public void include(PortletHeaderRequest request, PortletHeaderResponse response, String headerResource)
58      throws PortletException
59      {
60          try
61          {
62              HttpServletRequest servletRequest = requestContext.getRequest();
63              HttpServletResponse servletResponse = requestContext.getResponse();
64              PortletContent content = new PortletContentImpl();
65              HttpBufferedResponse bufferedResponse = 
66                  new HttpBufferedResponse(servletResponse, content.getWriter());
67              ServletContext crossContext = requestContext.getConfig().getServletContext().getContext(request.getPortletApplicationContextPath());            
68              RequestDispatcher dispatcher = crossContext.getRequestDispatcher(headerResource);
69              if (dispatcher != null)
70                  dispatcher.include(servletRequest, bufferedResponse);            
71              bufferedResponse.flushBuffer();
72              BufferedReader reader = new BufferedReader(new StringReader(content.getContent()));
73              String buffer;
74              StringBuffer headerText = new StringBuffer();
75              while ((buffer = reader.readLine()) != null)
76              {
77                  headerText.append( buffer ).append( "\r\n" );
78              }
79              tempContent = headerText.toString();            
80          }
81          catch (Exception e)
82          {
83              throw new PortletException(e);
84          }
85      }
86      
87      protected RequestContext getRequestContext()
88      {
89          return this.requestContext;
90      }
91      
92      public HeaderResource getHeaderResource()
93      {
94          return this.hr;
95      }
96      
97      public boolean isDesktop()
98      {
99          return this.isDesktop;
100     }
101     
102     public Map getHeaderConfiguration()
103     {
104         return this.headerConfiguration;
105     }
106     
107     public Map getHeaderResourceRegistry()
108     {
109         return this.headerResourceRegistry;
110     }
111     
112     public String getContent()
113     {
114         return tempContent; 
115     }
116 }