001: /**
002: * $Id: MarkupConfigManagerImpl.java,v 1.9 2004/02/27 20:06:34 mjain Exp $
003: * Copyright 2003 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wsrp.consumer.markup.impl;
014:
015: import com.sun.portal.container.ContainerRequest;
016: import com.sun.portal.container.ContainerException;
017: import com.sun.portal.container.ContentException;
018: import com.sun.portal.providers.context.ProviderContext;
019: import com.sun.portal.providers.context.ProviderContextException;
020:
021: import com.sun.portal.wsrp.consumer.common.WSRPConsumerException;
022: import com.sun.portal.wsrp.consumer.common.WSRPProviderConstants;
023: import com.sun.portal.wsrp.consumer.markup.MarkupConfig;
024: import com.sun.portal.wsrp.consumer.markup.MarkupConfigManager;
025: import com.sun.portal.wsrp.consumer.markup.MarkupContentException;
026: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntity;
027: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntityStatus;
028: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntityManager;
029: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntityManagerFactory;
030: import com.sun.portal.wsrp.consumer.common.RemoteServiceStubManager;
031: import com.sun.portal.wsrp.consumer.common.RemoteServiceStubManagerFactory;
032: import com.sun.portal.wsrp.consumer.common.WSRPConsumerErrorCode;
033:
034: import com.sun.portal.wsrp.common.stubs.PortletDescription;
035: import com.sun.portal.wsrp.common.stubs.ServiceDescription;
036: import com.sun.portal.wsrp.common.stubs.PortletContext;
037:
038: import com.sun.portal.desktop.encode.TypeEncoder;
039: import com.sun.portal.desktop.encode.Encoder;
040: import com.sun.portal.desktop.encode.EncoderClassNames;
041: import com.sun.portal.desktop.encode.EncoderException;
042:
043: import javax.servlet.http.HttpServletRequest;
044:
045: import java.io.UnsupportedEncodingException;
046:
047: /**
048: * Implementation of MarkupConfigManager
049: */
050: public class MarkupConfigManagerImpl implements MarkupConfigManager {
051:
052: private TypeEncoder _namespaceEncoder;
053:
054: public MarkupConfigManagerImpl() throws WSRPConsumerException {
055: try {
056: _namespaceEncoder = Encoder
057: .get(EncoderClassNames.ENCODER_FORMNAME);
058: } catch (EncoderException ex) {
059: throw new WSRPConsumerException(
060: "Failed encoder initialization:", ex);
061: }
062: }
063:
064: /**
065: * Check the description in the interface.
066: *
067: * @param request
068: * @exception com.sun.portal.container.ContainerException
069: */
070: public MarkupConfig getMarkupConfig(ContainerRequest request)
071: throws ContainerException, ContentException {
072:
073: return new MarkupConfig(request.getEntityID(),
074: getProducerEntityId(request), getPortletId(request),
075: getConsumerId(request), getProducerEntity(request),
076: getProducerEntityManager(request),
077: getRemoteServiceStubManager(request),
078: getServiceDescription(request),
079: getPortletDescription(request),
080: getPortletContext(request),
081: getPortletInstanceKey(request),
082: getNamespacePrefix(request));
083: }
084:
085: protected String getChannelName(ContainerRequest request)
086: throws ContainerException {
087: return request.getEntityID();
088: }
089:
090: protected String getProducerEntityId(ContainerRequest request)
091: throws ContainerException {
092: return getConfiguredProperty(request,
093: WSRPProviderConstants.PRODUCER_ENTITY_ID);
094: }
095:
096: protected String getPortletId(ContainerRequest request)
097: throws ContainerException {
098: return getConfiguredProperty(request,
099: WSRPProviderConstants.PORTLET_ID);
100: }
101:
102: protected String getConsumerId(ContainerRequest request)
103: throws ContainerException {
104: return getConfiguredProperty(request,
105: WSRPProviderConstants.CONSUMER_ID);
106: }
107:
108: protected PortletContext getPortletContext(ContainerRequest request)
109: throws ContainerException {
110:
111: PortletContext portletContext = new PortletContext();
112:
113: String portletHandle = getConfiguredProperty(request,
114: WSRPProviderConstants.PORTLET_HANDLE);
115:
116: portletContext.setPortletHandle(portletHandle);
117:
118: String portletState = getConfiguredProperty(request,
119: WSRPProviderConstants.PORTLET_STATE);
120:
121: if (portletState != null && portletState.length() > 0) {
122: try {
123: portletContext.setPortletState(portletState
124: .getBytes("UTF-8"));
125: } catch (UnsupportedEncodingException ex) {
126: throw new ContainerException(
127: "Failed to decode portletState ", ex);
128: }
129:
130: }
131:
132: return portletContext;
133:
134: }
135:
136: public void processPortletContext(ContainerRequest request,
137: PortletContext portletContext) throws ContainerException,
138: ContentException {
139:
140: //
141: // input parameter sanity check
142: //
143:
144: if (portletContext == null) {
145: return;
146: }
147:
148: byte[] portletState = portletContext.getPortletState();
149: String newPortletHandle = portletContext.getPortletHandle();
150: String oldPortletHandle = getConfiguredProperty(request,
151: WSRPProviderConstants.PORTLET_HANDLE);
152:
153: //
154: // Check for anonymous that shouldn't be able to set any state
155: //
156: if (MarkupUtil.isAuthless(request)
157: && ((portletState != null) || !newPortletHandle
158: .equals(oldPortletHandle))) {
159:
160: throw new MarkupContentException(
161: WSRPConsumerErrorCode.ANONYMOUS_NOTALLOWED_STATECHANGE,
162: "Anonymous user not allowed state change.");
163: }
164:
165: //
166: // set new portlet handle and state
167: //
168:
169: if (!newPortletHandle.equals(oldPortletHandle)) {
170: setConfiguredProperty(request,
171: WSRPProviderConstants.PORTLET_HANDLE,
172: newPortletHandle);
173:
174: }
175:
176: if (portletState != null) {
177: try {
178: String portletStateStr = new String(portletState,
179: "UTF-8");
180: setConfiguredProperty(request,
181: WSRPProviderConstants.PORTLET_STATE,
182: portletStateStr);
183: } catch (UnsupportedEncodingException ex) {
184: throw new ContainerException(
185: "Failed to encode portletState ", ex);
186: }
187: }
188:
189: }
190:
191: protected String getPortletInstanceKey(ContainerRequest request)
192: throws ContainerException {
193: return "P_" + _namespaceEncoder.encode(request.getEntityID());
194: }
195:
196: protected String getNamespacePrefix(ContainerRequest request)
197: throws ContainerException {
198:
199: return "N_" + _namespaceEncoder.encode(request.getEntityID());
200: }
201:
202: protected PortletDescription getPortletDescription(
203: ContainerRequest request) throws ContainerException,
204: ContentException {
205:
206: return getProducerEntity(request).getPortletDescription(
207: getPortletId(request));
208:
209: }
210:
211: protected ServiceDescription getServiceDescription(
212: ContainerRequest request) throws ContainerException,
213: ContentException {
214:
215: return getProducerEntity(request).getServiceDescription();
216: }
217:
218: protected ProducerEntity getProducerEntity(ContainerRequest request)
219: throws ContainerException, ContentException {
220:
221: ProducerEntity producerEntity;
222: try {
223: producerEntity = getProducerEntityManager(request)
224: .getProducerEntity(getProducerEntityId(request));
225: if (!producerEntity.getStatus().equals(
226: ProducerEntityStatus.OK)) {
227: throw new MarkupContentException(
228: WSRPConsumerErrorCode.PRODUCER_STATUS_NOT_OK,
229: "Producer status not ok :"
230: + producerEntity.getStatus());
231: }
232:
233: } catch (WSRPConsumerException ex) {
234: throw new ContainerException(
235: "Problem reading producerEntityId", ex);
236:
237: }
238: return producerEntity;
239: }
240:
241: protected String getConfiguredProperty(ContainerRequest request,
242: String key) throws ContainerException {
243:
244: try {
245: ProviderContext context = MarkupUtil
246: .getProviderContext(request);
247:
248: return context.getStringProperty(getChannelName(request),
249: key);
250: } catch (ProviderContextException e) {
251: throw new ContainerException(
252: "From ProviderContext, failed to get value of "
253: + key, e);
254: }
255: }
256:
257: protected void setConfiguredProperty(ContainerRequest request,
258: String key, String value) throws ContainerException {
259:
260: try {
261: MarkupUtil.getProviderContext(request).setStringProperty(
262: getChannelName(request), key, value);
263: } catch (ProviderContextException e) {
264: throw new ContainerException(
265: "From ProviderContext, failed to set value of "
266: + key, e);
267: }
268: }
269:
270: protected ProducerEntityManager getProducerEntityManager(
271: ContainerRequest request) throws ContainerException {
272:
273: try {
274: ProducerEntityManagerFactory pemf = ProducerEntityManagerFactory
275: .getInstance();
276:
277: //
278: // no access checking done on PEM here.
279: // it's assumed that WSRPWindowProvider has already done this.
280: //
281: return pemf.getProducerEntityManager(
282: getConsumerId(request), request
283: .getHttpServletRequest());
284: } catch (WSRPConsumerException ex) {
285: throw new ContainerException("Error in getting PEM:", ex);
286: }
287:
288: }
289:
290: protected RemoteServiceStubManager getRemoteServiceStubManager(
291: ContainerRequest request) throws ContainerException {
292: return RemoteServiceStubManagerFactory
293: .getRemoteServiceStubManager();
294: }
295:
296: }
|