You are viewing a plain text version of this content. The canonical link for it is here.
Posted to wsrp4j-dev@portals.apache.org by dl...@apache.org on 2005/08/24 19:36:34 UTC
cvs commit: ws-wsrp4j/sandbox/wsrp4j/consumer/src/java/org/apache/wsrp4j/consumer/proxyportlet/impl ResourceProxy.java WSRPRequestImpl.java URLGeneratorImpl.java
dlouzan 2005/08/24 10:36:34
Added: sandbox/wsrp4j/consumer/src/java/org/apache/wsrp4j/consumer/proxyportlet/impl
ResourceProxy.java WSRPRequestImpl.java
URLGeneratorImpl.java
Log:
Initial commit.
Revision Changes Path
1.1 ws-wsrp4j/sandbox/wsrp4j/consumer/src/java/org/apache/wsrp4j/consumer/proxyportlet/impl/ResourceProxy.java
Index: ResourceProxy.java
===================================================================
/*
* Copyright 2003-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.wsrp4j.consumer.proxyportlet.impl;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.wsrp4j.commons.util.Constants;
/**
* This servlet acts as proxy for WSRP resources.
*
* A WSRP resource link should point to this servlet.
* The URL of the actual resource is taken from the request parameter
* and an URL connection is opened to the target URL. The input stream
* of this connection is read and copied to the servlet response stream.
* @version $Id: ResourceProxy.java,v 1.1 2005/08/24 17:36:33 dlouzan Exp $
*/
public class ResourceProxy extends HttpServlet {
public void service(HttpServletRequest request,
HttpServletResponse response) {
String targetURL = request.getParameter(Constants.URL);
if (targetURL != null) {
try {
URL url = new URL(URLDecoder.decode(targetURL, "utf-8"));
URLConnection conn = url.openConnection();
conn.connect();
response.setContentType(conn.getContentType());
response.setContentLength(conn.getContentLength());
InputStream in = conn.getInputStream();
OutputStream out = response.getOutputStream();
int next;
while ((next = in.read()) != -1) {
out.write(next);
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
1.1 ws-wsrp4j/sandbox/wsrp4j/consumer/src/java/org/apache/wsrp4j/consumer/proxyportlet/impl/WSRPRequestImpl.java
Index: WSRPRequestImpl.java
===================================================================
/*
* Copyright 2003-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.wsrp4j.consumer.proxyportlet.impl;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequest;
import oasis.names.tc.wsrp.v1.types.ClientData;
import oasis.names.tc.wsrp.v1.types.MarkupContext;
import oasis.names.tc.wsrp.v1.types.NamedString;
import oasis.names.tc.wsrp.v1.types.SessionContext;
import org.apache.pluto.core.CoreUtils;
import org.apache.pluto.om.portlet.ContentType;
import org.apache.pluto.om.portlet.ContentTypeSet;
import org.apache.pluto.om.window.PortletWindow;
import org.apache.pluto.util.NamespaceMapperAccess;
import org.apache.wsrp4j.commons.consumer.interfaces.request.InteractionRequest;
import org.apache.wsrp4j.commons.consumer.interfaces.request.MarkupRequest;
import org.apache.wsrp4j.commons.consumer.interfaces.session.
PortletWindowSession;
import org.apache.wsrp4j.commons.consumer.driver.GenericWSRPBaseRequestImpl;
import org.apache.wsrp4j.commons.log.LogManager;
import org.apache.wsrp4j.commons.log.Logger;
import org.apache.wsrp4j.commons.util.AuthenticationInfoHelper;
import org.apache.wsrp4j.commons.util.Constants;
import org.apache.wsrp4j.commons.util.LocaleHelper;
import org.apache.wsrp4j.commons.util.Modes;
import org.apache.wsrp4j.commons.util.WindowStates;
/**
* @version $Id: WSRPRequestImpl.java,v 1.1 2005/08/24 17:36:33 dlouzan Exp $
*/
public class WSRPRequestImpl extends GenericWSRPBaseRequestImpl
implements InteractionRequest, MarkupRequest {
private final PortletRequest portletRequest;
private final PortletWindowSession windowSession;
private final PortletWindow portletWindow;
private final String nameSpace;
private final String userAuth;
private NamedString[] formParameters;
private String interactionState;
private String currentMode;
private String currentState;
private String naviState;
// just for performance reasons we cache this info
private String[] modes;
private String[] locales;
protected Logger logger =
LogManager.getLogManager().getLogger(WSRPRequestImpl.class);
public WSRPRequestImpl(PortletWindowSession windowSession,
javax.portlet.PortletRequest portletRequest) {
this.windowSession = windowSession;
this.portletRequest = portletRequest;
this.portletWindow =
CoreUtils.getInternalRequest(portletRequest).
getInternalPortletWindow();
this.nameSpace =
NamespaceMapperAccess.getNamespaceMapper().
encode(portletWindow.getId(), "");
this.userAuth =
AuthenticationInfoHelper.
getWsrpFromPortlet(portletRequest.getAuthType());
integrateParameter();
}
public String getInteractionState() {
return interactionState;
}
public NamedString[] getFormParameters() {
return formParameters;
}
public MarkupContext getCachedMarkup() {
if (windowSession == null)
return null;
return windowSession.getCachedMarkup();
}
public String getSessionID() {
if (this.windowSession != null) {
SessionContext sessionCtx =
this.windowSession.getPortletSession().getSessionContext();
if (sessionCtx != null) {
return sessionCtx.getSessionID();
}
}
return null;
}
public String getPortletInstanceKey() {
return this.portletWindow.getId().toString();
}
public String getNavigationalState() {
return naviState;
}
public String getWindowState() {
if (currentState == null) {
//map portlet window states to wsrp:window states
javax.portlet.WindowState portletWindowState =
portletRequest.getWindowState();
currentState =
WindowStates.
getWsrpStateFromJsrPortletState(portletWindowState).
toString();
}
return currentState;
}
public String getMode() {
if (currentMode == null) {
//map jsr-168 modes to wsrp:modes
PortletMode portletMode = portletRequest.getPortletMode();
currentMode =
Modes.getWsrpModeFromJsrPortletMode(portletMode).toString();
}
return currentMode;
}
public ClientData getClientData() {
// TODO: need to find out the client data here
return null;
}
public String[] getLocales() {
if (this.locales == null) {
Enumeration eLocales = portletRequest.getLocales();
List wsrpLocales = new ArrayList();
while (eLocales.hasMoreElements()) {
Locale locale = (Locale)eLocales.nextElement();
wsrpLocales.add(LocaleHelper.getWsrpLocale(locale));
}
locales = (String[])wsrpLocales.toArray(new String[0]);
}
return locales;
}
public String[] getModes() {
final String MN = "getModes()";
if (logger.isLogging(Logger.TRACE_HIGH)) {
logger.entry(Logger.TRACE_HIGH, MN);
}
if (this.modes != null) {
if (logger.isLogging(Logger.TRACE_HIGH)) {
logger.exit(Logger.TRACE_HIGH, MN);
}
return this.modes;
}
Set result = new HashSet();
String[] mimeTypes = getMimeTypes();
if (mimeTypes != null) {
ContentTypeSet contentTypes =
this.portletWindow.getPortletEntity().
getPortletDefinition().getContentTypeSet();
for (int i = 0; i < mimeTypes.length; i++) {
if (mimeTypes[i].equals("*")) {
Iterator it = contentTypes.iterator();
while (it.hasNext()) {
ContentType type = (ContentType)it.next();
Iterator modesIt = type.getPortletModes();
while (modesIt.hasNext()) {
PortletMode mode = (PortletMode)modesIt.next();
result.add(
Modes.getWsrpModeFromJsrPortletMode(mode).
toString());
}
}
break;
}
ContentType type = contentTypes.get(mimeTypes[i]);
if (type != null) {
Iterator modesIt = type.getPortletModes();
while (modesIt.hasNext()) {
PortletMode mode = (PortletMode)modesIt.next();
result.add(
Modes.getWsrpModeFromJsrPortletMode(mode).
toString());
}
}
}
}
if (!result.isEmpty()) {
this.modes = (String[])result.toArray(new String[0]);
}
if (logger.isLogging(Logger.TRACE_HIGH)) {
logger.exit(Logger.TRACE_HIGH, MN);
}
return this.modes;
}
public String[] getWindowStates() {
// TODO: for now we simply return what we know
// we should return what our environment supports
return WindowStates.getWindowStatesAsStringArray();
}
public String[] getMimeTypes() {
// TODO: return whatever our environment supports
return null;
}
public String[] getCharacterEncodingSet() {
// TODO: return whatever our environment supports
return null;
}
public boolean isModeSupported(String wsrpMode) {
if (wsrpMode == null)
throw new IllegalArgumentException("A mode must not be null");
return portletRequest.isPortletModeAllowed(
Modes.getJsrPortletModeFromWsrpMode(
Modes.fromString(wsrpMode)));
}
public boolean isWindowStateSupported(String wsrpWindowState) {
if (wsrpWindowState == null)
throw new IllegalArgumentException("A window state must not be " +
"null");
return portletRequest.isWindowStateAllowed(
WindowStates.getJsrPortletStateFromWsrpState(
WindowStates.fromString(wsrpWindowState)));
}
public String getUserAuthentication() {
return this.userAuth;
}
private void integrateParameter() {
final String MN = "integrateParameter()";
if (logger.isLogging(Logger.TRACE_HIGH)) {
logger.entry(Logger.TRACE_HIGH, MN);
}
ArrayList formParams = new ArrayList();
Set paramKeysSet = portletRequest.getParameterMap().keySet();
// interaction state
this.interactionState = portletRequest.getParameter(
Constants.INTERACTION_STATE);
// check for navistate
// if navistate is stored as url parameter take this
// otherwise look for render param
this.naviState = portletRequest.getParameter(
Constants.NAVIGATIONAL_STATE);
if (this.naviState == null) {
this.naviState = portletRequest.getParameter(
ProxyPortlet.NAVIGATIONAL_STATE);
}
Iterator paramKeys = paramKeysSet.iterator();
while (paramKeys.hasNext()) {
String key = (String)paramKeys.next();
if (!Constants.isWsrpURLParam(key) && !key.equals(
ProxyPortlet.NAVIGATIONAL_STATE)) {
// the rest are form parameter
String[] values = portletRequest.getParameterValues(key);
if (values != null) {
for (int i = 0; i < values.length; i++) {
NamedString paramPair = new NamedString();
paramPair.setName(key);
paramPair.setValue(values[i]);
formParams.add(paramPair);
}
}
}
}
if (!formParams.isEmpty()) {
formParameters = new NamedString[formParams.size()];
formParams.toArray(formParameters);
}
if (logger.isLogging(Logger.TRACE_HIGH)) {
logger.exit(Logger.TRACE_HIGH, MN);
}
}
}
1.1 ws-wsrp4j/sandbox/wsrp4j/consumer/src/java/org/apache/wsrp4j/consumer/proxyportlet/impl/URLGeneratorImpl.java
Index: URLGeneratorImpl.java
===================================================================
/*
* Copyright 2003-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.wsrp4j.consumer.proxyportlet.impl;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import javax.portlet.PortletConfig;
import javax.portlet.PortletMode;
import javax.portlet.PortletURL;
import javax.portlet.RenderResponse;
import javax.portlet.WindowState;
import org.apache.wsrp4j.commons.consumer.interfaces.urlgenerator.URLGenerator;
import org.apache.wsrp4j.commons.util.Constants;
import org.apache.wsrp4j.commons.util.Modes;
import org.apache.wsrp4j.commons.util.WindowStates;
/**
* This class implements the URLGenerator-interface providing methods
* to query the consumer's urls.
* @version $Id: URLGeneratorImpl.java,v 1.1 2005/08/24 17:36:33 dlouzan Exp $
*/
public class URLGeneratorImpl implements URLGenerator {
private static URLGeneratorImpl instance;
private RenderResponse renderResponse;
private Map consumerParameters;
// URL to resource proxy
private static String rpURL;
private static String RESOURCE_PROXY = "/ResourceProxy";
public static URLGenerator getInstance(RenderResponse response,
PortletConfig config) {
if (response == null) {
throw new IllegalArgumentException("response must not be null");
}
if (config == null) {
throw new IllegalArgumentException("config must not be null");
}
if (instance == null) {
instance = new URLGeneratorImpl(response);
} else {
instance.setRenderResponse(response);
}
setResourceProxyURL(config);
return instance;
}
/**
* @param config
*/
private static void setResourceProxyURL(PortletConfig config) {
String fullPath = config.getPortletContext().getRealPath("");
int i = fullPath.lastIndexOf(File.separatorChar);
rpURL = "/";
rpURL = rpURL.concat(fullPath.substring(i+1));
rpURL = rpURL.concat(RESOURCE_PROXY);
}
private URLGeneratorImpl(RenderResponse response) {
this.renderResponse = response;
}
public void setRenderResponse(RenderResponse response) {
if (response != null) {
this.renderResponse = response;
}
}
public void setConsumerParameters(Map consumerParameters) {
if (consumerParameters != null) {
this.consumerParameters = consumerParameters;
}
}
public String getBlockingActionURL(Map params) {
PortletURL url = renderResponse.createActionURL();
if (params != null) {
Iterator iter = params.keySet().iterator();
String paramName = "";
String paramValue = "";
while (iter.hasNext()) {
paramName = (String)iter.next();
if (paramName.equalsIgnoreCase(Constants.WINDOW_STATE)) {
if ((paramValue = (String)params.get(paramName)) != null) {
setWindowState(url, paramValue);
}
} else if (paramName.equalsIgnoreCase(Constants.PORTLET_MODE)) {
if ((paramValue = (String)params.get(paramName)) != null) {
setPortletMode(url, paramValue);
}
} else {
if ((paramValue = (String)params.get(paramName)) != null) {
url.setParameter(paramName, paramValue);
}
}
}
}
if (consumerParameters != null) {
Iterator iter2 = consumerParameters.keySet().iterator();
String name = null;
String value = null;
while (iter2.hasNext()) {
name = (String) iter2.next();
if ((value = (String)consumerParameters.get(name)) != null) {
url.setParameter(name, value);
}
}
}
return url.toString();
}
public String getRenderURL(Map params) {
PortletURL url = renderResponse.createRenderURL();
if (params != null) {
Iterator iter = params.keySet().iterator();
String paramName = "";
String paramValue = "";
while (iter.hasNext()) {
paramName = (String)iter.next();
if (paramName.equalsIgnoreCase(Constants.WINDOW_STATE)) {
if ((paramValue = (String)params.get(paramName)) != null) {
setWindowState(url, paramValue);
}
} else if (paramName.equalsIgnoreCase(Constants.PORTLET_MODE)) {
if ((paramValue = (String)params.get(paramName)) != null) {
setPortletMode(url, paramValue);
}
} else {
if (!paramName.equalsIgnoreCase(Constants.URL_TYPE)
&& (paramValue = (String)params.get(paramName)) != null) {
url.setParameter(paramName, paramValue);
}
}
}
}
if (consumerParameters != null) {
Iterator iter2 = consumerParameters.keySet().iterator();
String name = null;
String value = null;
while (iter2.hasNext()) {
name = (String) iter2.next();
if ((value = (String)consumerParameters.get(name)) != null) {
url.setParameter(name, value);
}
}
}
return url.toString();
}
public String getResourceURL(Map params) {
String resourceUrl = this.renderResponse.encodeURL(rpURL);
// TODO
if (resourceUrl == null)
return "Error";
StringBuffer url = new StringBuffer(resourceUrl);
if (params != null) {
String paramValue = null;
if ((paramValue = (String)params.get(Constants.URL)) != null) {
url.append(Constants.PARAMS_START);
url.append(Constants.URL);
url.append(Constants.EQUALS);
url.append(paramValue);
}
}
return url.toString();
}
public String getNamespacedToken(String token) {
return renderResponse.getNamespace();
}
/**
* Maps wsrp-windowStates to pluto-windowStates.
*/
private void setWindowState(PortletURL url, String windowState) {
try {
if (windowState.equalsIgnoreCase(WindowStates._maximized)) {
url.setWindowState(WindowState.MAXIMIZED);
} else if (windowState.equalsIgnoreCase(WindowStates._minimized)) {
url.setWindowState(WindowState.MINIMIZED);
} else if (windowState.equalsIgnoreCase(WindowStates._normal)) {
url.setWindowState(WindowState.NORMAL);
} else if (windowState.equalsIgnoreCase(WindowStates._solo)) {
url.setWindowState(WindowState.MAXIMIZED);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Maps wsrp-portletModes to pluto-portletModes.
**/
private void setPortletMode(PortletURL url, String mode) {
try {
if (mode.equalsIgnoreCase(Modes._edit)) {
url.setPortletMode(PortletMode.EDIT);
} else if (mode.equalsIgnoreCase(Modes._view)) {
url.setPortletMode(PortletMode.VIEW);
} else if (mode.equalsIgnoreCase(Modes._help)) {
url.setPortletMode(PortletMode.HELP);
} else if (mode.equalsIgnoreCase(Modes._preview)) {
url.setPortletMode(PortletMode.VIEW);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}