View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.portletbridge.portlet;
17  
18  import java.io.IOException;
19  import java.io.StringReader;
20  import java.net.URI;
21  import java.net.URISyntaxException;
22  
23  import javax.portlet.ActionRequest;
24  import javax.portlet.ActionResponse;
25  import javax.portlet.GenericPortlet;
26  import javax.portlet.PortletException;
27  import javax.portlet.PortletPreferences;
28  import javax.portlet.ReadOnlyException;
29  import javax.portlet.RenderRequest;
30  import javax.portlet.RenderResponse;
31  import javax.xml.transform.Templates;
32  import javax.xml.transform.Transformer;
33  import javax.xml.transform.TransformerConfigurationException;
34  import javax.xml.transform.TransformerException;
35  import javax.xml.transform.stream.StreamResult;
36  import javax.xml.transform.stream.StreamSource;
37  
38  /***
39   * Portlet that handles editing of preferences
40   * 
41   * @author JMcCrindle
42   */
43  public class BridgeEditPortlet extends GenericPortlet {
44  
45      private Templates templates = null;
46      
47      public BridgeEditPortlet() {
48          super();
49      }
50      
51      /* (non-Javadoc)
52       * @see javax.portlet.GenericPortlet#init()
53       */
54      public void init() throws PortletException {
55      }
56      
57      /* (non-Javadoc)
58       * @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
59       */
60      protected void doEdit(RenderRequest request, RenderResponse response)
61              throws PortletException, IOException {
62          PortletPreferences preferences = request.getPreferences();
63          String secureEdit = preferences.getValue("secureEdit", "false");
64          if("true".equalsIgnoreCase(secureEdit) && !request.isUserInRole("portletbridge")) {
65              throw new PortletException(getPortletConfig().getResourceBundle(request.getLocale()).getString("error.invalid.role"));
66          }
67          response.setContentType("text/html");
68          try {
69              Transformer transformer = templates.newTransformer();
70              transformer.setParameter("portlet", new PortletFunctions(request, response));
71              transformer.transform(new StreamSource(new StringReader("<xml/>")), new StreamResult(response.getWriter()));
72          } catch (TransformerConfigurationException e) {
73              throw new PortletException(e);
74          } catch (TransformerException e) {
75              throw new PortletException(e);
76          } catch (IOException e) {
77              throw new PortletException(e);
78          }
79      }
80      
81      /* (non-Javadoc)
82       * @see javax.portlet.GenericPortlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
83       */
84      public void processAction(ActionRequest request, ActionResponse response)
85              throws PortletException, IOException {
86          PortletPreferences preferences = request.getPreferences();
87          String initUrlParameter = request.getParameter("initUrl");
88          if(initUrlParameter != null && initUrlParameter.trim().length() > 0) {
89              try {
90                  URI initUrl = new URI(initUrlParameter);
91                  if(initUrl.getPath() == null || "".equals(initUrl.getPath())) {
92                      initUrl = new URI(initUrlParameter + "/");
93                  }
94                  preferences.setValue("initUrl", initUrl.toString());
95              } catch (URISyntaxException e) {
96                  // TODO: validation
97                  throw new PortletException(e);
98              }
99          }
100         setIfNotNull(request, preferences, "scope");
101         setIfNotNull(request, preferences, "proxyHost");
102         setIfNotNull(request, preferences, "proxyPort");
103         setIfNotNull(request, preferences, "proxyAuthentication");
104         setIfNotNull(request, preferences, "proxyAuthenticationUsername");
105         setIfNotNull(request, preferences, "proxyAuthenticationPassword");
106         setIfNotNull(request, preferences, "proxyAuthenticationHost");
107         setIfNotNull(request, preferences, "proxyAuthenticationDomain");
108         setIfNotNull(request, preferences, "stylesheet");
109         
110         preferences.store();
111     }
112     
113     protected void setIfNotNull(ActionRequest request, PortletPreferences preferences, String parameterName) throws ReadOnlyException {
114         String parameterValue = request.getParameter(parameterName);
115         if(parameterValue != null) {
116             preferences.setValue(parameterName, parameterValue);
117         }
118     }
119 
120     public void setTemplates(Templates templates) {
121         this.templates = templates;
122     }
123 }