1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
52
53
54 public void init() throws PortletException {
55 }
56
57
58
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
82
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
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 }