abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From calav...@apache.org
Subject svn commit: r674439 - in /incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server: ./ impl/ processors/
Date Mon, 07 Jul 2008 10:11:52 GMT
Author: calavera
Date: Mon Jul  7 03:11:51 2008
New Revision: 674439

URL: http://svn.apache.org/viewvc?rev=674439&view=rev
Log:
ABDERA-185: Refactoring AbstractProvider to make it open for extension. Thanks to Sergio Bossa

All the bussiness logic has been moved from AbstractProvider to a specific RequestProccessor
implementation. Each implementation serves one type of 
requests, for instance, EntryRequestProcessor class serves TargetType.TYPE_ENTRY requests.

I've changed some stuff of the patch. I've moved processors to a specific package and I've
deleted the NoOpTransaction class since it's not needed. 
Moreover I've renamed the "setAdditionalRequestProcessors" method as "addRequestProcessors",
I think it's more readable and it follows the abdera 
names convention("addFilter", "addCollection" ...).



Added:
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestProcessor.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/CategoriesRequestProcessor.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/CollectionRequestProcessor.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/EntryRequestProcessor.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/MediaRequestProcessor.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/ServiceRequestProcessor.java
Modified:
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/Provider.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractProvider.java

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/Provider.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/Provider.java?rev=674439&r1=674438&r2=674439&view=diff
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/Provider.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/Provider.java
Mon Jul  7 03:11:51 2008
@@ -24,7 +24,9 @@
 import org.apache.abdera.Abdera;
 
 /**
- * Providers are responsible for processing all requests to the Atompub server.
+ * Providers are responsible for processing all requests to the Atompub server.<br>
+ * Actual request processing is delegated to {@link RequestProcessor} implementations, depending
+ * on the request {@link TargetType}.
  */
 public interface Provider {
   
@@ -73,4 +75,19 @@
    */
   Filter[] getFilters(RequestContext request);
   
+  /**
+   * Set a map of {@link RequestProcessor}s to register on this provider, overriding already
registered ones.
+   */
+  void setRequestProcessors(Map<TargetType, RequestProcessor> requestProcessors);
+  
+  /**
+   * Add a map of {@link RequestProcessor}s to register on this provider, without overriding
already registered ones.<br>
+   * Named after a "setter" method in order to be Dependency Injection friendly.
+   */
+  void addRequestProcessors(Map<TargetType, RequestProcessor> requestProcessors);
+  
+  /**
+   * Return a map of registered {@link RequestProcessor}s with related {@link TargetType}.
+   */
+  Map<TargetType, RequestProcessor> getRequestProcessors();
 }

Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestProcessor.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestProcessor.java?rev=674439&view=auto
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestProcessor.java
(added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestProcessor.java
Mon Jul  7 03:11:51 2008
@@ -0,0 +1,38 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* 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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.server;
+
+/**
+ * Request processors implement the actual business logic for handling requests to the Atompub
server
+ * and producing the related response.
+ */
+public interface RequestProcessor {
+
+    /**
+     * Provide the actual request processing logic.
+     * 
+     * @param requestContext The {@link RequestContext} object, 
+     * holding information about the request to process.
+     * @param workspaceManager The {@link WorkspaceManager} object, 
+     * holding information useful for request processing.
+     * @param collectionAdapter The {@link CollectionAdapter} object, 
+     * holding information useful for request processing; may be null if not needed. 
+     * @return A {@link ResponseContext} object, as resulted from the request processing.
+     */
+    ResponseContext process(RequestContext requestContext, WorkspaceManager workspaceManager,
CollectionAdapter collectionAdapter);
+}

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractProvider.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractProvider.java?rev=674439&r1=674438&r2=674439&view=diff
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractProvider.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractProvider.java
Mon Jul  7 03:11:51 2008
@@ -1,25 +1,25 @@
 /*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements.  The ASF licenses this file to You
-* 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.  For additional information regarding
-* copyright in this work, please see the NOTICE file in the top level
-* directory of this distribution.
-*/
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  The ASF licenses this file to You
+ * 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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
 package org.apache.abdera.protocol.server.impl;
 
-import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -28,15 +28,12 @@
 import org.apache.abdera.Abdera;
 import org.apache.abdera.model.Service;
 import org.apache.abdera.protocol.Resolver;
-import org.apache.abdera.protocol.server.CategoriesInfo;
-import org.apache.abdera.protocol.server.CategoryInfo;
 import org.apache.abdera.protocol.server.CollectionAdapter;
-import org.apache.abdera.protocol.server.CollectionInfo;
 import org.apache.abdera.protocol.server.Filter;
-import org.apache.abdera.protocol.server.MediaCollectionAdapter;
 import org.apache.abdera.protocol.server.Provider;
 import org.apache.abdera.protocol.server.ProviderHelper;
 import org.apache.abdera.protocol.server.RequestContext;
+import org.apache.abdera.protocol.server.RequestProcessor;
 import org.apache.abdera.protocol.server.ResponseContext;
 import org.apache.abdera.protocol.server.Target;
 import org.apache.abdera.protocol.server.TargetBuilder;
@@ -45,313 +42,192 @@
 import org.apache.abdera.protocol.server.WorkspaceInfo;
 import org.apache.abdera.protocol.server.WorkspaceManager;
 import org.apache.abdera.protocol.server.context.ResponseContextException;
-import org.apache.abdera.protocol.server.context.StreamWriterResponseContext;
-import org.apache.abdera.util.Constants;
-import org.apache.abdera.writer.StreamWriter;
+import org.apache.abdera.protocol.server.processors.CategoriesRequestProcessor;
+import org.apache.abdera.protocol.server.processors.CollectionRequestProcessor;
+import org.apache.abdera.protocol.server.processors.EntryRequestProcessor;
+import org.apache.abdera.protocol.server.processors.MediaRequestProcessor;
+import org.apache.abdera.protocol.server.processors.ServiceRequestProcessor;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 /**
  * Base Provider implementation that provides the core implementation details 
- * for all Providers.  This class provides the basic request routing logic
+ * for all Providers. This class provides the basic request routing logic.
  */
-public abstract class AbstractProvider 
-  implements Provider {
-  
-  private final static Log log = LogFactory.getLog(AbstractProvider.class);
-  
-  protected Abdera abdera;
-  protected Map<String,String> properties;
-  protected List<Filter> filters = new ArrayList<Filter>();
-  
-  public void init(
-    Abdera abdera, 
-    Map<String, String> properties) {
-      this.abdera = abdera;
-      this.properties = properties;
-  }
-  
-  public String getProperty(String name) {
-    return properties.get(name);
-  }
-  
-  public String[] getPropertyNames() {
-    return properties.keySet().toArray(new String[properties.size()]);
-  }
-  
-  public Abdera getAbdera() {
-    return abdera;
-  }
-
-  public Subject resolveSubject(RequestContext request) {
-    Resolver<Subject> subjectResolver = getSubjectResolver(request);
-    return subjectResolver != null ? 
-       subjectResolver.resolve(request) : null;
-  }
-
-  public Target resolveTarget(RequestContext request) {
-    Resolver<Target> targetResolver = 
-      getTargetResolver(request);
-    return targetResolver != null ? 
-      targetResolver.resolve(request) : null;
-  }
-
-  public String urlFor(
-    RequestContext request, 
-    Object key, 
-    Object param) {
-      TargetBuilder tm = getTargetBuilder(request);
-      return tm != null ? tm.urlFor(request, key, param) : null;
-  }
-  
-  protected Resolver<Subject> getSubjectResolver(RequestContext request) {
-    return new SimpleSubjectResolver();
-  }
-
-  protected abstract TargetBuilder getTargetBuilder(RequestContext request);
-  
-  protected abstract Resolver<Target> getTargetResolver(RequestContext request);
-
-  public ResponseContext process(
-    RequestContext request) {    
-      Target target = request.getTarget();
-      if (target == null || 
-          target.getType() == TargetType.TYPE_NOT_FOUND)
-        return ProviderHelper.notfound(request);
-      TargetType type = target.getType();
-      if (type == TargetType.TYPE_SERVICE)
-        return processService(request);
-      WorkspaceManager wm = getWorkspaceManager(request);      
-      CollectionAdapter adapter = 
-        wm.getCollectionAdapter(request);
-      if (adapter == null)
-        return ProviderHelper.notfound(request);
-      
-      Transactional transaction = 
-        adapter instanceof Transactional ? 
-          (Transactional)adapter : null;
-      ResponseContext response = null;
-      try {
-        if (transaction != null) transaction.start(request);
-        if (type == TargetType.TYPE_CATEGORIES)
-          response = processCategories(request, adapter);
-        else if (type == TargetType.TYPE_COLLECTION)
-          response = processCollection(request, adapter);
-        else if (type == TargetType.TYPE_ENTRY)
-          response = processEntry(request, adapter);
-        else if (type == TargetType.TYPE_MEDIA)
-          response = processMedia(request, adapter);
-        response = 
-          response != null ? 
-            response : 
-            processExtensionRequest(
-              request, 
-              adapter);
-        return 
-          response != null ? 
-            response :
-            ProviderHelper.badrequest(request);
-      } catch (Throwable e) {
-        if (e instanceof ResponseContextException) {
-          ResponseContextException rce = (ResponseContextException) e;
-          if (rce.getStatusCode() >= 400 && rce.getStatusCode() < 500) {
-            // don't report routine 4xx HTTP errors
-            log.info(e);
-          } else {
-            log.error(e);
-          }
-        } else {
-          log.error(e);
+public abstract class AbstractProvider implements Provider {
+
+    private final static Log log = LogFactory.getLog(AbstractProvider.class);
+    protected Abdera abdera;
+    protected Map<String, String> properties;
+    protected List<Filter> filters = new ArrayList<Filter>();
+    protected Map<TargetType, RequestProcessor> requestProcessors = new HashMap<TargetType,
RequestProcessor>();
+
+    public void init(Abdera abdera, Map<String, String> properties) {
+        this.abdera = abdera;
+        this.properties = properties;
+        // Setting default request processors:
+        this.requestProcessors.put(TargetType.TYPE_SERVICE, new ServiceRequestProcessor());
+        this.requestProcessors.put(TargetType.TYPE_CATEGORIES, new CategoriesRequestProcessor());
+        this.requestProcessors.put(TargetType.TYPE_COLLECTION, new CollectionRequestProcessor());
+        this.requestProcessors.put(TargetType.TYPE_ENTRY, new EntryRequestProcessor());
+        this.requestProcessors.put(TargetType.TYPE_MEDIA, new MediaRequestProcessor());
+    }
+
+    public String getProperty(String name) {
+        return properties.get(name);
+    }
+
+    public String[] getPropertyNames() {
+        return properties.keySet().toArray(new String[properties.size()]);
+    }
+
+    public Abdera getAbdera() {
+        return abdera;
+    }
+
+    public Subject resolveSubject(RequestContext request) {
+        Resolver<Subject> subjectResolver = getSubjectResolver(request);
+        return subjectResolver != null ? subjectResolver.resolve(request) : null;
+    }
+
+    public Target resolveTarget(RequestContext request) {
+        Resolver<Target> targetResolver =
+                getTargetResolver(request);
+        return targetResolver != null ? targetResolver.resolve(request) : null;
+    }
+
+    public String urlFor(
+            RequestContext request,
+            Object key,
+            Object param) {
+        TargetBuilder tm = getTargetBuilder(request);
+        return tm != null ? tm.urlFor(request, key, param) : null;
+    }
+
+    protected Resolver<Subject> getSubjectResolver(RequestContext request) {
+        return new SimpleSubjectResolver();
+    }
+
+    protected abstract TargetBuilder getTargetBuilder(RequestContext request);
+
+    protected abstract Resolver<Target> getTargetResolver(RequestContext request);
+
+    public ResponseContext process(RequestContext request) {
+        Target target = request.getTarget();
+        if (target == null || target.getType() == TargetType.TYPE_NOT_FOUND) {
+            return ProviderHelper.notfound(request);
+        }
+
+        TargetType type = target.getType();
+        RequestProcessor processor = this.requestProcessors.get(type);
+        if (processor == null) {
+            return ProviderHelper.notfound(request);
         }
-        transactionCompensate(transaction, request, e);
-        response = createErrorResponse(request,e);
-        return response;
-      } finally {
-        transactionEnd(transaction,request,response);
-      }
-  }
-
-  /**
-   * Subclass to customize the kind of error response to return
-   */
-  protected ResponseContext createErrorResponse(RequestContext request, Throwable e) {
-    return ProviderHelper.servererror(request, e);
-  }
-  
-  protected void transactionCompensate(
-    Transactional transactional,
-    RequestContext request,
-    Throwable e) {
-      if (transactional != null) 
-        transactional.compensate(request,e);      
-  }
-  
-  protected void transactionEnd(
-    Transactional transactional, 
-    RequestContext request,
-    ResponseContext response) {
-    if (transactional != null)
-      transactional.end(request,response);
-  }
-  
-  protected void transactionStart(
-    Transactional transactional, 
-    RequestContext request) 
-      throws ResponseContextException {
-    if (transactional != null)
-      transactional.start(request);
-  }
-  
-  protected ResponseContext processService(
-    RequestContext context) {
-      String method = context.getMethod(); 
-      if (method.equalsIgnoreCase("GET"))
-        return getServiceDocument(context);
-      else return null;
-  }
-  
-  protected ResponseContext processExtensionRequest(
-    RequestContext context, 
-    CollectionAdapter adapter) {    
-      return adapter.extensionRequest(context);
-  }
-  
-  protected ResponseContext processCategories(
-    RequestContext context, 
-    CollectionAdapter adapter) {
-      return context.getMethod().equalsIgnoreCase("GET") ?
-        adapter.getCategories(context) : null;
-  }
-  
-  protected ResponseContext processCollection(
-    RequestContext context,
-    CollectionAdapter adapter) {
-      String method = context.getMethod();
-      if (method.equalsIgnoreCase("GET")) 
-        return adapter.getFeed(context);
-      else if (method.equalsIgnoreCase("POST")) {
-        return ProviderHelper.isAtom(context) ?
-          adapter.postEntry(context) :
-          adapter instanceof MediaCollectionAdapter ?
-            ((MediaCollectionAdapter)adapter).postMedia(context) :
-            ProviderHelper.notallowed(context);
-      } else return null;
-  }
-  
-  protected ResponseContext processEntry(
-    RequestContext context,
-    CollectionAdapter adapter) {
-      String method = context.getMethod();
-      if (method.equalsIgnoreCase("GET")) 
-        return adapter.getEntry(context);
-      else if (method.equalsIgnoreCase("PUT")) 
-        return adapter.putEntry(context);
-      else if (method.equalsIgnoreCase("DELETE")) 
-        return adapter.deleteEntry(context);
-      else if (method.equalsIgnoreCase("HEAD")) 
-        return adapter.headEntry(context);
-      else if (method.equalsIgnoreCase("OPTIONS")) 
-        return adapter.optionsEntry(context);
-      else return null;
-  }
-  
-  protected ResponseContext processMedia(
-    RequestContext context,
-    CollectionAdapter adapter) {
-      String method = context.getMethod();
-      if (adapter instanceof MediaCollectionAdapter) {
-        MediaCollectionAdapter mcadapter = 
-          (MediaCollectionAdapter) adapter;
-        if (method.equalsIgnoreCase("GET")) 
-          return mcadapter.getMedia(context);
-        else if (method.equalsIgnoreCase("PUT")) 
-          return mcadapter.putMedia(context);
-        else if (method.equalsIgnoreCase("DELETE")) 
-          return mcadapter.deleteMedia(context);   
-        else if (method.equalsIgnoreCase("HEAD")) 
-          return mcadapter.headMedia(context);   
-        else if (method.equalsIgnoreCase("OPTIONS")) 
-          return mcadapter.optionsMedia(context);
-        else return null;
-      } else {
-        return ProviderHelper.notallowed(context);
-      }
-  }
-  
-  protected abstract WorkspaceManager getWorkspaceManager(
-    RequestContext request);
-  
-
-  protected Service getServiceElement(RequestContext request) {
-    Service service = abdera.newService();
-    for (WorkspaceInfo wi : getWorkspaceManager(request).getWorkspaces(request))
-      service.addWorkspace(wi.asWorkspaceElement(request));
-    return service;
-  }
-  
-  protected ResponseContext getServiceDocument(
-    final RequestContext request) {
-      return 
-        new StreamWriterResponseContext(request.getAbdera()) {
-          protected void writeTo(
-            StreamWriter sw) 
-              throws IOException {
-            sw.startDocument()
-              .startService();
-            for (WorkspaceInfo wi : getWorkspaceManager(request).getWorkspaces(request))
{
-              sw.startWorkspace()
-                .writeTitle(wi.getTitle(request));
-              Collection<CollectionInfo> collections = wi.getCollections(request);
-              if (collections != null) {
-                for (CollectionInfo ci : collections) {
-                  sw.startCollection(ci.getHref(request))
-                    .writeTitle(ci.getTitle(request))
-                    .writeAccepts(ci.getAccepts(request));
-                  CategoriesInfo[] catinfos = ci.getCategoriesInfo(request);
-                  if (catinfos != null) {
-                    for (CategoriesInfo catinfo : catinfos) {
-                      String cathref = catinfo.getHref(request);
-                      if (cathref != null) {
-                        sw.startCategories()
-                          .writeAttribute("href", request.getTargetBasePath() + cathref)
-                          .endCategories();
-                      } else {
-                        sw.startCategories(
-                          catinfo.isFixed(request), 
-                          catinfo.getScheme(request));
-                        for (CategoryInfo cat : catinfo) {
-                          sw.writeCategory(
-                            cat.getTerm(request), 
-                            cat.getScheme(request), 
-                            cat.getLabel(request));
-                        }
-                        sw.endCategories();
-                      }
-                    }
-                  }
-                  sw.endCollection();
+
+        WorkspaceManager wm = getWorkspaceManager(request);
+        CollectionAdapter adapter = wm.getCollectionAdapter(request);
+        Transactional transaction = adapter instanceof Transactional ? (Transactional) adapter
: null;
+        ResponseContext response = null;
+        try {
+            transactionStart(transaction, request);
+            response = processor.process(request, wm, adapter);
+            response = response != null ? response : processExtensionRequest(request, adapter);
           
+        } catch (Throwable e) {
+            if (e instanceof ResponseContextException) {
+                ResponseContextException rce = (ResponseContextException) e;
+                if (rce.getStatusCode() >= 400 && rce.getStatusCode() < 500)
{
+                    // don't report routine 4xx HTTP errors
+                    log.info(e);
+                } else {
+                    log.error(e);
                 }
-              }
-              sw.endWorkspace();
+            } else {
+                log.error(e);
             }
-            sw.endService()
-              .endDocument();
-          }
+            transactionCompensate(transaction, request, e);
+            response = createErrorResponse(request, e);
+            return response;
+        } finally {
+            transactionEnd(transaction, request, response);
+        }
+        return response != null ? response : ProviderHelper.badrequest(request);
+    }
+
+    /**
+     * Subclass to customize the kind of error response to return
+     */
+    protected ResponseContext createErrorResponse(RequestContext request, Throwable e) {
+        return ProviderHelper.servererror(request, e);
+    }
+
+    protected void transactionCompensate(
+            Transactional transactional,
+            RequestContext request,
+            Throwable e) {
+        if (transactional != null) {
+          transactional.compensate(request, e);
+        }
+    }
+
+    protected void transactionEnd(
+            Transactional transactional,
+            RequestContext request,
+            ResponseContext response) {
+        if (transactional != null) {
+          transactional.end(request, response);
+        }
+    }
+
+    protected void transactionStart(
+            Transactional transactional,
+            RequestContext request)
+            throws ResponseContextException {
+        if (transactional != null) {
+          transactional.start(request);
+        }
+    }
+
+    protected ResponseContext processExtensionRequest(
+            RequestContext context,
+            CollectionAdapter adapter) {
+        return adapter.extensionRequest(context);
+    }
+
+    protected abstract WorkspaceManager getWorkspaceManager(
+            RequestContext request);
+
+    protected Service getServiceElement(RequestContext request) {
+        Service service = abdera.newService();
+        for (WorkspaceInfo wi : getWorkspaceManager(request).getWorkspaces(request)) {
+            service.addWorkspace(wi.asWorkspaceElement(request));
+        }
+        return service;
+    }
+
+    public void setFilters(List<Filter> filters) {
+        this.filters = filters;
+    }
+
+    public Filter[] getFilters(RequestContext request) {
+        return filters.toArray(new Filter[filters.size()]);
+    }
+
+    public void addFilter(Filter... filters) {
+        for (Filter filter : filters) {
+            this.filters.add(filter);
         }
-        .setStatus(200)
-        .setContentType(Constants.APP_MEDIA_TYPE);
-  }
-
-  public void setFilters(List<Filter> filters) {
-    this.filters = filters;
-  }
-  
-  public Filter[] getFilters(RequestContext request) {
-    return filters.toArray(new Filter[filters.size()]);
-  }
-
-  public void addFilter(Filter... filters) {
-    for (Filter filter : filters)
-      this.filters.add(filter);
-  }  
+    }
+
+    public void setRequestProcessors(Map<TargetType, RequestProcessor> requestProcessors)
{
+        this.requestProcessors.clear();
+        this.requestProcessors.putAll(requestProcessors);
+    }
+
+    public void addRequestProcessors(Map<TargetType, RequestProcessor> requestProcessors)
{
+        this.requestProcessors.putAll(requestProcessors);
+    }
+
+    public Map<TargetType, RequestProcessor> getRequestProcessors() {
+        return Collections.unmodifiableMap(this.requestProcessors);
+    }
 }

Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/CategoriesRequestProcessor.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/CategoriesRequestProcessor.java?rev=674439&view=auto
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/CategoriesRequestProcessor.java
(added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/CategoriesRequestProcessor.java
Mon Jul  7 03:11:51 2008
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  The ASF licenses this file to You
+ * 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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.abdera.protocol.server.processors;
+
+import org.apache.abdera.protocol.server.CollectionAdapter;
+import org.apache.abdera.protocol.server.ProviderHelper;
+import org.apache.abdera.protocol.server.RequestContext;
+import org.apache.abdera.protocol.server.RequestProcessor;
+import org.apache.abdera.protocol.server.ResponseContext;
+import org.apache.abdera.protocol.server.WorkspaceManager;
+
+/**
+ * {@link org.apache.abdera.protocol.server.RequestProcessor} implementation which
+ * processes requests for categories documents.
+ */
+public class CategoriesRequestProcessor implements RequestProcessor {
+
+    public ResponseContext process(RequestContext context, WorkspaceManager workspaceManager,
CollectionAdapter collectionAdapter) {
+        if (collectionAdapter == null) {
+            return ProviderHelper.notfound(context);
+        } else {
+            return this.processCategories(context, collectionAdapter);
+        }
+    }
+
+    protected ResponseContext processCategories(RequestContext context, CollectionAdapter
adapter) {
+        return context.getMethod().equalsIgnoreCase("GET") ? adapter.getCategories(context)
: null;
+    }
+}

Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/CollectionRequestProcessor.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/CollectionRequestProcessor.java?rev=674439&view=auto
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/CollectionRequestProcessor.java
(added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/CollectionRequestProcessor.java
Mon Jul  7 03:11:51 2008
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  The ASF licenses this file to You
+ * 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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.abdera.protocol.server.processors;
+
+import org.apache.abdera.protocol.server.CollectionAdapter;
+import org.apache.abdera.protocol.server.MediaCollectionAdapter;
+import org.apache.abdera.protocol.server.ProviderHelper;
+import org.apache.abdera.protocol.server.RequestContext;
+import org.apache.abdera.protocol.server.RequestProcessor;
+import org.apache.abdera.protocol.server.ResponseContext;
+import org.apache.abdera.protocol.server.WorkspaceManager;
+
+/**
+ * {@link org.apache.abdera.protocol.server.RequestProcessor} implementation which
+ * processes requests for collection documents.
+ */
+public class CollectionRequestProcessor implements RequestProcessor {
+
+    public ResponseContext process(RequestContext context, WorkspaceManager workspaceManager,
CollectionAdapter collectionAdapter) {
+        if (collectionAdapter == null) {
+            return ProviderHelper.notfound(context);
+        } else {
+            return this.processCollection(context, collectionAdapter);
+        }
+    }
+
+    private ResponseContext processCollection(RequestContext context, CollectionAdapter adapter)
{
+        String method = context.getMethod();
+        if (method.equalsIgnoreCase("GET")) {
+            return adapter.getFeed(context);
+        } else if (method.equalsIgnoreCase("POST")) {
+            return ProviderHelper.isAtom(context) ? adapter.postEntry(context) : adapter
instanceof MediaCollectionAdapter ? ((MediaCollectionAdapter) adapter).postMedia(context)
: ProviderHelper.notallowed(context);
+        } else {
+            return null;
+        }
+    }
+}

Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/EntryRequestProcessor.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/EntryRequestProcessor.java?rev=674439&view=auto
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/EntryRequestProcessor.java
(added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/EntryRequestProcessor.java
Mon Jul  7 03:11:51 2008
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  The ASF licenses this file to You
+ * 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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.abdera.protocol.server.processors;
+
+import org.apache.abdera.protocol.server.CollectionAdapter;
+import org.apache.abdera.protocol.server.ProviderHelper;
+import org.apache.abdera.protocol.server.RequestContext;
+import org.apache.abdera.protocol.server.RequestProcessor;
+import org.apache.abdera.protocol.server.ResponseContext;
+import org.apache.abdera.protocol.server.WorkspaceManager;
+
+/**
+ * {@link org.apache.abdera.protocol.server.RequestProcessor} implementation which
+ * processes requests for entry documents.
+ */
+public class EntryRequestProcessor implements RequestProcessor {
+
+    public ResponseContext process(RequestContext context, WorkspaceManager workspaceManager,
CollectionAdapter collectionAdapter) {
+        if (collectionAdapter == null) {
+            return ProviderHelper.notfound(context);
+        } else {
+            return this.processEntry(context, collectionAdapter);
+        }
+    }
+
+    protected ResponseContext processEntry(RequestContext context, CollectionAdapter adapter)
{
+        String method = context.getMethod();
+        if (method.equalsIgnoreCase("GET")) {
+            return adapter.getEntry(context);
+        } else if (method.equalsIgnoreCase("PUT")) {
+            return adapter.putEntry(context);
+        } else if (method.equalsIgnoreCase("DELETE")) {
+            return adapter.deleteEntry(context);
+        } else if (method.equalsIgnoreCase("HEAD")) {
+            return adapter.headEntry(context);
+        } else if (method.equalsIgnoreCase("OPTIONS")) {
+            return adapter.optionsEntry(context);
+        } else {
+            return null;
+        }
+    }
+}

Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/MediaRequestProcessor.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/MediaRequestProcessor.java?rev=674439&view=auto
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/MediaRequestProcessor.java
(added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/MediaRequestProcessor.java
Mon Jul  7 03:11:51 2008
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  The ASF licenses this file to You
+ * 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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.abdera.protocol.server.processors;
+
+import org.apache.abdera.protocol.server.CollectionAdapter;
+import org.apache.abdera.protocol.server.MediaCollectionAdapter;
+import org.apache.abdera.protocol.server.ProviderHelper;
+import org.apache.abdera.protocol.server.RequestContext;
+import org.apache.abdera.protocol.server.RequestProcessor;
+import org.apache.abdera.protocol.server.ResponseContext;
+import org.apache.abdera.protocol.server.WorkspaceManager;
+
+/**
+ * {@link org.apache.abdera.protocol.server.RequestProcessor} implementation which
+ * processes requests for media documents.
+ */
+public class MediaRequestProcessor implements RequestProcessor {
+
+    public ResponseContext process(RequestContext context, WorkspaceManager workspaceManager,
CollectionAdapter collectionAdapter) {
+        if (collectionAdapter == null) {
+            return ProviderHelper.notfound(context);
+        } else {
+            return this.processMedia(context, collectionAdapter);
+        }
+    }
+
+    protected ResponseContext processMedia(RequestContext context, CollectionAdapter adapter)
{
+        String method = context.getMethod();
+        if (adapter instanceof MediaCollectionAdapter) {
+            MediaCollectionAdapter mcadapter = (MediaCollectionAdapter) adapter;
+            if (method.equalsIgnoreCase("GET")) {
+                return mcadapter.getMedia(context);
+            } else if (method.equalsIgnoreCase("PUT")) {
+                return mcadapter.putMedia(context);
+            } else if (method.equalsIgnoreCase("DELETE")) {
+                return mcadapter.deleteMedia(context);
+            } else if (method.equalsIgnoreCase("HEAD")) {
+                return mcadapter.headMedia(context);
+            } else if (method.equalsIgnoreCase("OPTIONS")) {
+                return mcadapter.optionsMedia(context);
+            } else {
+                return null;
+            }
+        } else {
+            return ProviderHelper.notallowed(context);
+        }
+    }
+}

Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/ServiceRequestProcessor.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/ServiceRequestProcessor.java?rev=674439&view=auto
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/ServiceRequestProcessor.java
(added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/processors/ServiceRequestProcessor.java
Mon Jul  7 03:11:51 2008
@@ -0,0 +1,96 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* 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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.server.processors;
+
+import java.io.IOException;
+import java.util.Collection;
+import org.apache.abdera.protocol.server.CategoriesInfo;
+import org.apache.abdera.protocol.server.CategoryInfo;
+import org.apache.abdera.protocol.server.CollectionAdapter;
+import org.apache.abdera.protocol.server.CollectionInfo;
+import org.apache.abdera.protocol.server.RequestContext;
+import org.apache.abdera.protocol.server.RequestProcessor;
+import org.apache.abdera.protocol.server.ResponseContext;
+import org.apache.abdera.protocol.server.WorkspaceInfo;
+import org.apache.abdera.protocol.server.WorkspaceManager;
+import org.apache.abdera.protocol.server.context.StreamWriterResponseContext;
+import org.apache.abdera.util.Constants;
+import org.apache.abdera.writer.StreamWriter;
+
+/**
+ * {@link org.apache.abdera.protocol.server.RequestProcessor} implementation which
+ * processes requests for service documents.
+ */
+public class ServiceRequestProcessor implements RequestProcessor {
+
+    public ResponseContext process(RequestContext context, WorkspaceManager workspaceManager,
CollectionAdapter collectionAdapter) {
+        return this.processService(context, workspaceManager);
+    }
+
+    private ResponseContext processService(RequestContext context, WorkspaceManager workspaceManager)
{
+        String method = context.getMethod();
+        if (method.equalsIgnoreCase("GET")) {
+            return this.getServiceDocument(context, workspaceManager);
+        } else {
+            return null;
+        }
+    }
+
+    private ResponseContext getServiceDocument(final RequestContext request, final WorkspaceManager
workspaceManager) {
+        return new StreamWriterResponseContext(request.getAbdera()) {
+
+            protected void writeTo(
+                    StreamWriter sw)
+                    throws IOException {
+                sw.startDocument().startService();
+                for (WorkspaceInfo wi : workspaceManager.getWorkspaces(request)) {
+                    sw.startWorkspace().writeTitle(wi.getTitle(request));
+                    Collection<CollectionInfo> collections = wi.getCollections(request);
+                    if (collections != null) {
+                        for (CollectionInfo ci : collections) {
+                            sw.startCollection(ci.getHref(request)).writeTitle(ci.getTitle(request)).writeAccepts(ci.getAccepts(request));
+                            CategoriesInfo[] catinfos = ci.getCategoriesInfo(request);
+                            if (catinfos != null) {
+                                for (CategoriesInfo catinfo : catinfos) {
+                                    String cathref = catinfo.getHref(request);
+                                    if (cathref != null) {
+                                        sw.startCategories().writeAttribute("href", request.getTargetBasePath()
+ cathref).endCategories();
+                                    } else {
+                                        sw.startCategories(
+                                                catinfo.isFixed(request),
+                                                catinfo.getScheme(request));
+                                        for (CategoryInfo cat : catinfo) {
+                                            sw.writeCategory(
+                                                    cat.getTerm(request),
+                                                    cat.getScheme(request),
+                                                    cat.getLabel(request));
+                                        }
+                                        sw.endCategories();
+                                    }
+                                }
+                            }
+                            sw.endCollection();
+                        }
+                    }
+                    sw.endWorkspace();
+                }
+                sw.endService().endDocument();
+            }
+        }.setStatus(200).setContentType(Constants.APP_MEDIA_TYPE);
+    }
+}



Mime
View raw message