You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by we...@apache.org on 2005/06/13 18:44:47 UTC
svn commit: r190422 - in
/portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax: ./
AJAXFilter.java AJAXRequestImpl.java AJAXResponseImpl.java
AJAXServiceImpl.java AJAXValve.java
Author: weaver
Date: Mon Jun 13 09:44:46 2005
New Revision: 190422
URL: http://svn.apache.org/viewcvs?rev=190422&view=rev
Log:
JS2-283: These are the initial implementations of the Ajax service interfaces
Added:
portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/
portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXFilter.java
portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXRequestImpl.java
portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXResponseImpl.java
portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXServiceImpl.java
portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXValve.java
Added: portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXFilter.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXFilter.java?rev=190422&view=auto
==============================================================================
--- portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXFilter.java (added)
+++ portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXFilter.java Mon Jun 13 09:44:46 2005
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2000-2001,2004 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.jetspeed.ajax;
+
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.web.context.WebApplicationContext;
+
+/**
+ * Simple ServletFilter for invoking AJAX services.
+ *
+ *
+ * @author <href a="mailto:weaver@apache.org">Scott T. Weaver</a>
+ *
+ */
+public class AJAXFilter implements Filter
+{
+ private ApplicationContext ctx;
+ private AJAXService ajaxService;
+ private FilterConfig config;
+
+ public void init(FilterConfig config) throws ServletException
+ {
+ this.config = config;
+ }
+
+ public void doFilter(ServletRequest request, ServletResponse response,
+ FilterChain arg2) throws IOException, ServletException
+ {
+ try
+ {
+ response.setContentType("text/xml");
+ if(ctx == null)
+ {
+ ctx = (ApplicationContext)config.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
+ ajaxService = (AJAXService) ctx.getBean("AJAXService");
+ }
+
+ AJAXRequest ajaxRequest = new AJAXRequestImpl((HttpServletRequest) request, (HttpServletResponse) response, config.getServletContext());
+ AJAXResponse ajaxReponse = ajaxService.processRequest(ajaxRequest);
+ ajaxReponse.complete();
+ }
+ catch (AJAXException e)
+ {
+ ((HttpServletResponse) response).sendError(500, e.getMessage());
+ }
+ catch(Exception e)
+ {
+ throw new ServletException(e.getMessage(), e);
+ }
+ }
+
+ public void destroy()
+ {
+ // do nothing
+
+ }
+
+}
Added: portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXRequestImpl.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXRequestImpl.java?rev=190422&view=auto
==============================================================================
--- portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXRequestImpl.java (added)
+++ portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXRequestImpl.java Mon Jun 13 09:44:46 2005
@@ -0,0 +1,180 @@
+/*
+ * Copyright 2000-2001,2004 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.jetspeed.ajax;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Request used for AJAX services.
+ *
+ * @author <href a="mailto:weaver@apache.org">Scott T. Weaver</a>
+ *
+ */
+public class AJAXRequestImpl implements AJAXRequest
+{
+ public static final String AJAX_SERVICE = "ajax_service";
+ public static final String AJAX_PARAM_PREFIX = "ajax_param_";
+
+ private final HttpServletRequest request;
+ private List ajaxParams;
+ private final String serviceName;
+ private final String methodName;
+ private HttpServletResponse response;
+ private ServletContext context;
+
+ public AJAXRequestImpl(HttpServletRequest request, HttpServletResponse response, ServletContext context) throws AJAXException
+ {
+ this.request = request;
+ this.response = response;
+ this.context = context;
+ String serviceRequest = request.getParameter(AJAX_SERVICE);
+ if(serviceRequest == null )
+ {
+ throw new AJAXException("No '"+AJAX_SERVICE+"' parameter could be found in the request or it was not in the '{service_name}.{method_name}' format.");
+ }
+ final String split = serviceRequest.split("\\.")[0];
+ serviceName = split;
+ methodName = serviceRequest.split("\\.")[1];
+
+ parseRequestArguments();
+
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.jetspeed.ajax.AJAXRequest#getParameters()
+ */
+ public List getParameters()
+ {
+ return ajaxParams;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.jetspeed.ajax.AJAXRequest#getServiceName()
+ */
+ public String getServiceName()
+ {
+ return serviceName;
+ }
+
+ protected List parseRequestArguments() throws AJAXException
+ {
+ try
+ {
+ ajaxParams = new ArrayList();
+ Map rawParams = request.getParameterMap();
+ Iterator entryItr = rawParams.entrySet().iterator();
+ while(entryItr.hasNext())
+ {
+ Map.Entry entry = (Map.Entry) entryItr.next();
+ String key = entry.getKey().toString();
+
+ if(key.startsWith(AJAX_PARAM_PREFIX))
+ {
+ String[] paramInfo = key.split("_");
+ int index = Integer.parseInt(paramInfo[2]);
+ String type = paramInfo[3];
+ AJAXParameter ajaxParam = new AJAXParameter(type, (String[])entry.getValue());
+ ajaxParams.add(index, ajaxParam);
+ }
+ }
+ return ajaxParams;
+ }
+ catch (Throwable e)
+ {
+ throw new AJAXException("Errors were encountered parsing request parameters for the AJAX service "+serviceName+": "+e.getMessage(), e);
+ }
+ }
+
+ public class AJAXParameter
+ {
+ private Object value;
+
+ public AJAXParameter(String typeName, String[] paramValues)
+ {
+ if(typeName.equals("int"))
+ {
+ if(paramValues.length > 1)
+ {
+ int[] intValues = new int[paramValues.length];
+ for(int i=0; i<paramValues.length; i++)
+ {
+ intValues[i] = Integer.parseInt(paramValues[i]);
+ }
+ }
+ else
+ {
+ value = new Integer(paramValues[0]);
+ }
+ }
+ else if(typeName.equals("str"))
+ {
+ if(paramValues.length > 1)
+ {
+ value = paramValues;
+ }
+ else
+ {
+ value = paramValues[0];
+ }
+ }
+ }
+
+ public Object getValue()
+ {
+ return value;
+ }
+ }
+
+
+ /* (non-Javadoc)
+ * @see org.apache.jetspeed.ajax.AJAXRequest#getMethodName()
+ */
+ public String getMethodName()
+ {
+ return methodName;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.jetspeed.ajax.AJAXRequest#getContext()
+ */
+ public ServletContext getContext()
+ {
+ return context;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.jetspeed.ajax.AJAXRequest#getServletRequest()
+ */
+ public HttpServletRequest getServletRequest()
+ {
+ return request;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.jetspeed.ajax.AJAXRequest#getServletResponse()
+ */
+ public HttpServletResponse getServletResponse()
+ {
+ return response;
+ }
+}
Added: portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXResponseImpl.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXResponseImpl.java?rev=190422&view=auto
==============================================================================
--- portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXResponseImpl.java (added)
+++ portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXResponseImpl.java Mon Jun 13 09:44:46 2005
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2000-2001,2004 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.jetspeed.ajax;
+
+import java.io.Reader;
+import java.io.Writer;
+
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.context.Context;
+
+/**
+ * Response object used for AJAX services.
+ *
+ * @author <href a="mailto:weaver@apache.org">Scott T. Weaver</a>
+ *
+ */
+public class AJAXResponseImpl implements AJAXResponse
+{
+
+ private Context context;
+ private VelocityEngine engine;
+ private Reader template;
+ private Writer output;
+
+ public AJAXResponseImpl(Context context, VelocityEngine engine, Reader template, Writer output)
+ {
+ this.context = context;
+ this.engine = engine;
+ this.template = template;
+ this.output = output;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.jetspeed.ajax.AJAXResponse#complete()
+ */
+ public void complete() throws AJAXException
+ {
+ try
+ {
+ engine.evaluate(context, output, "AJAX processor", template);
+ }
+ catch (Exception e)
+ {
+ throw new AJAXException("Failed to render velocity xml template: "+e.getMessage(), e);
+ }
+
+ }
+
+
+
+}
Added: portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXServiceImpl.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXServiceImpl.java?rev=190422&view=auto
==============================================================================
--- portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXServiceImpl.java (added)
+++ portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXServiceImpl.java Mon Jun 13 09:44:46 2005
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2000-2001,2004 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.jetspeed.ajax;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.lang.reflect.Method;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.context.Context;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.BeanFactoryAware;
+
+/**
+ * Performs invocation of the actual AJAX request and returns
+ * a result object to converted into XML.
+ *
+ * @author <href a="mailto:weaver@apache.org">Scott T. Weaver</a>
+ *
+ */
+public class AJAXServiceImpl implements AJAXService, BeanFactoryAware
+{
+
+ private Map serviceToBeans;
+
+ private BeanFactory beanFactory;
+ private VelocityEngine engine;
+
+ public AJAXServiceImpl(Map serviceToBeans)
+ {
+ this.serviceToBeans = serviceToBeans;
+
+ }
+
+ public AJAXResponse processRequest(AJAXRequest request)
+ throws AJAXException
+ {
+ final String serviceName = request.getServiceName();
+ final String methodName = request.getMethodName();
+ final String templateName = request.getServletRequest().getServletPath();
+
+ final String mappedServiceName = (serviceName+"."+methodName).trim();
+ try
+ {
+ if(engine == null)
+ {
+ engine = new VelocityEngine();
+ Properties props = new Properties();
+ props.load(request.getContext().getResourceAsStream("/WEB-INF/velocity.properties"));
+ engine.init();
+ }
+
+
+ if(!serviceToBeans.containsKey(mappedServiceName))
+ {
+ throw new AJAXException("There is no AJAX service named '"+mappedServiceName+"' defined. "+
+ "Please make sure that your ajax.xml is set up correctly.");
+ }
+
+ String beanId = ((String)serviceToBeans.get(mappedServiceName)).trim();
+ Object targetService = beanFactory.getBean(beanId);
+ final List parameters = request.getParameters();
+ Method method = targetService.getClass().getMethod(methodName, getTypes(parameters));
+ Object result = method.invoke(targetService, getValues(parameters));
+ Context context = new VelocityContext();
+ context.put("ajaxRequest", request);
+ context.put("result", result);
+
+ final InputStream templateResource = request.getContext().getResourceAsStream(templateName);
+
+ if(templateResource == null)
+ {
+ request.getServletResponse().sendError(404, templateName+" ajax template could not be found.");
+ throw new IOException(templateName+" does not exist");
+ }
+ Reader template = new InputStreamReader(templateResource);
+
+ return new AJAXResponseImpl(context, engine, template, request.getServletResponse().getWriter());
+ }
+ catch(AJAXException ae)
+ {
+ throw ae;
+ }
+ catch (Exception e)
+ {
+ throw new AJAXException("Unable to process service" + mappedServiceName + ": " + e.getMessage(), e);
+ }
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
+ */
+ public void setBeanFactory(BeanFactory beanFactory) throws BeansException
+ {
+ this.beanFactory = beanFactory;
+ }
+
+ protected Class[] getTypes(List objects)
+ {
+ Class[] args = new Class[objects.size()];
+ Iterator itr = objects.iterator();
+ int i = 0;
+ while (itr.hasNext())
+ {
+ args[i] = ((AJAXRequestImpl.AJAXParameter)itr.next()).getValue().getClass();
+ i++;
+ }
+ return args;
+ }
+
+ protected Object[] getValues(List objects)
+ {
+ Object[] args = new Object[objects.size()];
+ Iterator itr = objects.iterator();
+ int i = 0;
+ while (itr.hasNext())
+ {
+ args[i] = ((AJAXRequestImpl.AJAXParameter)itr.next()).getValue();
+ i++;
+ }
+ return args;
+ }
+
+}
Added: portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXValve.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXValve.java?rev=190422&view=auto
==============================================================================
--- portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXValve.java (added)
+++ portals/jetspeed-2/trunk/portal/src/java/org/apache/jetspeed/ajax/AJAXValve.java Mon Jun 13 09:44:46 2005
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2000-2001,2004 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.jetspeed.ajax;
+
+import org.apache.jetspeed.pipeline.PipelineException;
+import org.apache.jetspeed.pipeline.valve.AbstractValve;
+import org.apache.jetspeed.pipeline.valve.ValveContext;
+import org.apache.jetspeed.request.RequestContext;
+
+/**
+ * This should eventually replace the AJAX ServletFilter.
+ *
+ * @author <href a="mailto:weaver@apache.org">Scott T. Weaver</a>
+ *
+ */
+public class AJAXValve extends AbstractValve
+{
+ public AJAXValve()
+ {
+ super();
+
+ }
+
+ public void invoke(RequestContext request, ValveContext context)
+ throws PipelineException
+ {
+
+
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org