You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2008/01/22 01:53:10 UTC
svn commit: r614076 [2/2] - in
/incubator/abdera/java/branches/server_refactor/src:
main/java/org/apache/abdera/protocol/server/
main/java/org/apache/abdera/protocol/server/context/
main/java/org/apache/abdera/protocol/server/impl/ main/java/org/apache...
Modified: incubator/abdera/java/branches/server_refactor/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java?rev=614076&r1=614075&r2=614076&view=diff
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java (original)
+++ incubator/abdera/java/branches/server_refactor/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java Mon Jan 21 16:53:07 2008
@@ -102,6 +102,7 @@
provider.process(
reqcontext));
} catch (Throwable t) {
+ t.printStackTrace();
error("Error servicing request", t, response);
return;
}
Modified: incubator/abdera/java/branches/server_refactor/src/main/java/org/apache/abdera/protocol/server/servlet/ServletRequestContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/main/java/org/apache/abdera/protocol/server/servlet/ServletRequestContext.java?rev=614076&r1=614075&r2=614076&view=diff
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/main/java/org/apache/abdera/protocol/server/servlet/ServletRequestContext.java (original)
+++ incubator/abdera/java/branches/server_refactor/src/main/java/org/apache/abdera/protocol/server/servlet/ServletRequestContext.java Mon Jan 21 16:53:07 2008
@@ -37,7 +37,7 @@
import org.apache.abdera.protocol.server.RequestContext;
import org.apache.abdera.protocol.server.ServiceContext;
import org.apache.abdera.protocol.server.Target;
-import org.apache.abdera.protocol.server.impl.AbstractRequestContext;
+import org.apache.abdera.protocol.server.context.AbstractRequestContext;
public class ServletRequestContext
extends AbstractRequestContext
Modified: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/basic/BasicTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/basic/BasicTest.java?rev=614076&r1=614075&r2=614076&view=diff
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/basic/BasicTest.java (original)
+++ incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/basic/BasicTest.java Mon Jan 21 16:53:07 2008
@@ -23,9 +23,12 @@
import junit.framework.TestCase;
import org.apache.abdera.Abdera;
+import org.apache.abdera.model.Collection;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Feed;
+import org.apache.abdera.model.Service;
+import org.apache.abdera.model.Workspace;
import org.apache.abdera.protocol.Response.ResponseType;
import org.apache.abdera.protocol.client.AbderaClient;
import org.apache.abdera.protocol.client.ClientResponse;
@@ -51,10 +54,26 @@
} catch (Exception e) {}
}
- private int count = 5;
+ private int count = 6;
@Override protected void tearDown() throws Exception {
if (--count == 0) server.stop();
+ }
+
+ public void testGetService() {
+ ClientResponse resp = client.get("http://localhost:8080/");
+ assertNotNull(resp);
+ assertEquals(resp.getType(), ResponseType.SUCCESS);
+ assertTrue(MimeTypeHelper.isMatch(resp.getContentType().toString(), Constants.APP_MEDIA_TYPE));
+ Document<Service> doc = resp.getDocument();
+ Service service = doc.getRoot();
+ assertEquals(service.getWorkspaces().size(),1);
+ Workspace workspace = service.getWorkspace("Abdera");
+ assertEquals(workspace.getCollections().size(),1);
+ Collection collection = workspace.getCollection("title for any sample feed");
+ assertNotNull(collection);
+ assertTrue(collection.acceptsEntry());
+ assertEquals(collection.getResolvedHref().toString(), "http://localhost:8080/sample");
}
public void testGetFeed() {
Added: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/basic/SampleBasicAdapter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/basic/SampleBasicAdapter.java?rev=614076&view=auto
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/basic/SampleBasicAdapter.java (added)
+++ incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/basic/SampleBasicAdapter.java Mon Jan 21 16:53:07 2008
@@ -0,0 +1,141 @@
+/*
+* 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.test.basic;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Properties;
+import java.util.Set;
+import java.util.logging.Logger;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.model.Feed;
+import org.apache.abdera.protocol.server.provider.basic.BasicAdapter;
+
+public class SampleBasicAdapter
+ extends BasicAdapter {
+
+ private static final String ERROR_DUP_ENTRY = "Entry Already Exists";
+ private static final String ERROR_INVALID_ENTRY = "No Such Entry in the Feed";
+
+ public static Logger logger = Logger.getLogger(SampleBasicAdapter.class.getName());
+ protected HashMap<String, byte[]> entries = new HashMap<String, byte[]>();
+
+ public SampleBasicAdapter(Abdera abdera, Properties feedProperties,
+ String feedId) {
+ super(abdera, feedProperties, feedId);
+ }
+
+ public Feed getFeed() throws Exception {
+ Feed feed = createFeed();
+
+ // get all keys in the feed - with keys in descending order of
+ // lastUpdatedDate
+ Set<String> keys = entries.keySet();
+ for (String s : keys) {
+ Entry entry = getEntry(s);
+ // TODO: why clone this? Abdera seems to mess up the object
+ // if we pass the reference to this object
+ feed.addEntry((Entry) entry.clone());
+ }
+ return feed;
+ }
+
+ public Entry getEntry(Object entryId) throws Exception {
+ return retrieveEntry((String) entryId);
+ }
+
+ public Entry createEntry(Entry entry) throws Exception {
+ // entryId may be null. if it is, assign one
+ setEntryIdIfNull(entry);
+ logger.info("assigning id to Entry: " + entry.getId().toString());
+ String entryId = getEntryIdFromUri(entry.getId().toString());
+
+ if (entries.containsKey(entryId)) {
+ throw new Exception(ERROR_DUP_ENTRY);
+ }
+ // add an "updated" element if one was not provided
+ if (entry.getUpdated() == null) {
+ entry.setUpdated(new Date());
+ }
+ addEditLinkToEntry(entry);
+ storeEntry(entryId, entry);
+ logger.finest("returning this entry from sampleadapter.createEntry: " +
+ entry.toString());
+ return entry;
+ }
+
+ public Entry updateEntry(Object entryId, Entry entry) throws Exception {
+ if (!entries.containsKey(entryId)) {
+ throw new Exception(ERROR_INVALID_ENTRY);
+ }
+ entries.remove(entryId);
+ // add an "updated" element if one was not provided
+ if (entry.getUpdated() == null) {
+ entry.setUpdated(new Date());
+ }
+ addEditLinkToEntry(entry);
+ storeEntry((String) entryId, entry);
+ logger.finest("returning this entry from sampleadapter.updateEntry: " +
+ entry.toString());
+ return entry;
+ }
+
+ public boolean deleteEntry(Object entryId) throws Exception {
+ if (!entries.containsKey(entryId)) {
+ return false;
+ }
+ entries.remove(entryId);
+ return true;
+ }
+
+ protected String getEntryIdFromUri(String uri) {
+ String[] segments = uri.split("/");
+ return segments[segments.length - 1];
+ }
+
+ protected void storeEntry(String entryId, Entry entry) throws Exception {
+ Document<Element> entryDoc = entry.getDocument();
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ entryDoc.writeTo(bos);
+
+ // Get the bytes of the serialized object and store in hashmap
+ byte[] buf = bos.toByteArray();
+ bos.close();
+ entries.put(entryId, buf);
+ }
+
+ protected Entry retrieveEntry(String entryId) throws Exception {
+ // Deserialize from a byte array
+ byte[] bytes = entries.get(entryId);
+ if (bytes == null) {
+ // entry not found
+ return null;
+ }
+ ByteArrayInputStream in = new ByteArrayInputStream(bytes);
+ Document<Entry> entryDoc = abdera.getParser().parse(in);
+ Entry entry = entryDoc.getRoot();
+ return entry;
+ }
+}
+
Added: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/ext/ExtendedTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/ext/ExtendedTest.java?rev=614076&view=auto
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/ext/ExtendedTest.java (added)
+++ incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/ext/ExtendedTest.java Mon Jan 21 16:53:07 2008
@@ -0,0 +1,170 @@
+/*
+* 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.test.ext;
+
+import java.io.ByteArrayInputStream;
+import java.util.Date;
+
+import junit.framework.TestCase;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.model.Collection;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.model.Feed;
+import org.apache.abdera.model.Service;
+import org.apache.abdera.model.Workspace;
+import org.apache.abdera.protocol.Response.ResponseType;
+import org.apache.abdera.protocol.client.AbderaClient;
+import org.apache.abdera.protocol.client.ClientResponse;
+import org.apache.abdera.protocol.client.RequestOptions;
+import org.apache.abdera.protocol.server.test.JettyServer;
+import org.apache.abdera.util.Constants;
+import org.apache.abdera.util.MimeTypeHelper;
+
+public class ExtendedTest
+ extends TestCase {
+
+ private static JettyServer server;
+ private static Abdera abdera = Abdera.getInstance();
+ private static AbderaClient client = new AbderaClient();
+
+ public ExtendedTest() {
+ try {
+ if (server == null) {
+ server = new JettyServer();
+ server.start(TestServiceContext.class);
+ }
+ } catch (Exception e) {}
+ }
+
+ private int count = 6;
+
+ @Override protected void tearDown() throws Exception {
+ if (--count == 0)
+ server.stop();
+ }
+
+ public void testGetService() {
+ ClientResponse resp = client.get("http://localhost:8080/atom");
+ assertNotNull(resp);
+ assertEquals(resp.getType(),ResponseType.SUCCESS);
+ assertTrue(MimeTypeHelper.isMatch(resp.getContentType().toString(), Constants.APP_MEDIA_TYPE));
+ Document<Service> doc = resp.getDocument();
+ Service service = doc.getRoot();
+ assertEquals(service.getWorkspaces().size(),1);
+ Workspace workspace = service.getWorkspaces().get(0);
+ assertEquals(workspace.getCollections().size(),2);
+ Collection collection = workspace.getCollections().get(0);
+ assertEquals(collection.getResolvedHref().toString(), "http://localhost:8080/atom/feed1");
+ assertEquals(collection.getTitle().toString(), "entries1");
+ collection = workspace.getCollections().get(1);
+ assertEquals(collection.getResolvedHref().toString(), "http://localhost:8080/atom/feed2");
+ assertEquals(collection.getTitle().toString(), "entries2");
+ resp.release();
+ }
+
+ public void testGetFeed() {
+ ClientResponse resp = client.get("http://localhost:8080/atom/feed1");
+ assertNotNull(resp);
+ assertEquals(resp.getType(),ResponseType.SUCCESS);
+ assertTrue(MimeTypeHelper.isMatch(resp.getContentType().toString(), Constants.ATOM_MEDIA_TYPE));
+ Document<Feed> doc = resp.getDocument();
+ Feed feed = doc.getRoot();
+ assertEquals(feed.getId().toString(), "tag:example.org,2006:feed");
+ assertEquals(feed.getTitle(), "Simple");
+ assertEquals(feed.getAuthor().getName(), "Simple");
+ assertEquals(feed.getEntries().size(), 0);
+ resp.release();
+ }
+
+ public void testPostEntry() {
+ Entry entry = abdera.newEntry();
+ entry.setId("http://localhost:8080/atom/feed1/entries/1");
+ entry.setTitle("test entry");
+ entry.setContent("Test Content");
+ entry.addLink("http://example.org");
+ entry.setUpdated(new Date());
+ entry.addAuthor("James");
+ ClientResponse resp = client.post("http://localhost:8080/atom/feed1", entry);
+ assertNotNull(resp);
+ assertEquals(resp.getType(),ResponseType.SUCCESS);
+ assertEquals(resp.getStatus(), 201);
+ assertNotNull(resp.getLocation());
+ resp.release();
+ resp = client.get("http://localhost:8080/atom/feed1");
+ Document<Feed> feed_doc = resp.getDocument();
+ Feed feed = feed_doc.getRoot();
+ assertEquals(feed.getEntries().size(),1);
+ }
+
+ public void testPostMedia() {
+ ByteArrayInputStream in = new ByteArrayInputStream(new byte[] {0x01,0x02,0x03,0x04});
+ RequestOptions options = client.getDefaultRequestOptions();
+ options.setContentType("application/octet-stream");
+ ClientResponse resp = client.post("http://localhost:8080/atom/feed1", in, options);
+ assertEquals(resp.getType(),ResponseType.CLIENT_ERROR);
+ assertEquals(resp.getStatus(), 415);
+ resp.release();
+ }
+
+ public void testPutEntry() {
+ ClientResponse resp = client.get("http://localhost:8080/atom/feed1");
+ Document<Feed> feed_doc = resp.getDocument();
+ Feed feed = feed_doc.getRoot();
+ Entry entry = feed.getEntries().get(0);
+ String edit = entry.getEditLinkResolvedHref().toString();
+ resp.release();
+ resp = client.get(edit);
+ Document<Entry> doc = resp.getDocument();
+ entry = doc.getRoot();
+ entry.setTitle("This is the modified title");
+ resp.release();
+ resp = client.put(edit, entry);
+ assertEquals(resp.getType(), ResponseType.SUCCESS);
+ assertEquals(resp.getStatus(), 204);
+ resp.release();
+ resp = client.get(edit);
+ doc = resp.getDocument();
+ entry = doc.getRoot();
+ assertEquals(entry.getTitle(), "This is the modified title");
+ resp.release();
+ resp = client.get("http://localhost:8080/atom/feed1");
+ feed_doc = resp.getDocument();
+ feed = feed_doc.getRoot();
+ assertEquals(feed.getEntries().size(),1);
+ resp.release();
+ }
+
+ public void testDeleteEntry() {
+ ClientResponse resp = client.get("http://localhost:8080/atom/feed1");
+ Document<Feed> feed_doc = resp.getDocument();
+ Feed feed = feed_doc.getRoot();
+ Entry entry = feed.getEntries().get(0);
+ String edit = entry.getEditLinkResolvedHref().toString();
+ resp.release();
+ resp = client.delete(edit);
+ assertEquals(resp.getType(),ResponseType.SUCCESS);
+ resp.release();
+ resp = client.get("http://localhost:8080/atom/feed1");
+ feed_doc = resp.getDocument();
+ feed = feed_doc.getRoot();
+ assertEquals(feed.getEntries().size(),0);
+ resp.release();
+ }
+}
Added: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/ext/TestServiceContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/ext/TestServiceContext.java?rev=614076&view=auto
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/ext/TestServiceContext.java (added)
+++ incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/ext/TestServiceContext.java Mon Jan 21 16:53:07 2008
@@ -0,0 +1,71 @@
+/*
+* 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.test.ext;
+
+import org.apache.abdera.protocol.Resolver;
+import org.apache.abdera.protocol.server.Provider;
+import org.apache.abdera.protocol.server.RequestContext;
+import org.apache.abdera.protocol.server.Target;
+import org.apache.abdera.protocol.server.TargetType;
+import org.apache.abdera.protocol.server.context.DefaultServiceContext;
+import org.apache.abdera.protocol.server.impl.RegexTargetResolver;
+import org.apache.abdera.protocol.server.provider.ext.ExtendedProvider;
+import org.apache.abdera.protocol.server.provider.ext.SimpleCollection;
+import org.apache.abdera.protocol.server.provider.ext.SimpleWorkspace;
+import org.apache.abdera.protocol.server.test.simple.SimpleAdapter;
+
+public class TestServiceContext
+ extends DefaultServiceContext {
+
+ public synchronized Provider getProvider(
+ RequestContext request) {
+ if (provider == null) {
+ ExtendedProvider p = new ExtendedProvider();
+ SimpleWorkspace workspace = new SimpleWorkspace();
+ workspace.setTitle("My Blog");
+ workspace.addCollection(
+ new SimpleCollection(
+ new SimpleAdapter(),
+ "feed1",
+ "entries1",
+ "/atom/feed1",
+ "application/atom+xml;type=entry"
+ )
+ );
+ workspace.addCollection(
+ new SimpleCollection(
+ new SimpleAdapter(),
+ "feed2",
+ "entries2",
+ "/atom/feed2",
+ "application/atom+xml;type=entry"
+ )
+ );
+ p.addWorkspace(workspace);
+ provider = p;
+ }
+ return provider;
+ }
+
+ public Resolver<Target> getTargetResolver() {
+ return new RegexTargetResolver()
+ .setPattern("/atom(\\?[^#]*)?", TargetType.TYPE_SERVICE)
+ .setPattern("/atom/([^/#?]+)(\\?[^#]*)?", TargetType.TYPE_COLLECTION, "collection", "entry")
+ .setPattern("/atom/([^/#?]+)/([^/#?]+)(\\?[^#]*)?", TargetType.TYPE_ENTRY, "collection", "entry");
+ }
+}
Added: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleAdapter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleAdapter.java?rev=614076&view=auto
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleAdapter.java (added)
+++ incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleAdapter.java Mon Jan 21 16:53:07 2008
@@ -0,0 +1,255 @@
+/*
+* 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.test.simple;
+
+import java.util.Date;
+
+import javax.activation.MimeType;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.i18n.text.UrlEncoding;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.model.Feed;
+import org.apache.abdera.parser.ParseException;
+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.ResponseContext;
+import org.apache.abdera.protocol.server.TargetType;
+import org.apache.abdera.protocol.server.RequestContext.Scope;
+import org.apache.abdera.protocol.server.context.BaseResponseContext;
+import org.apache.abdera.util.MimeTypeHelper;
+
+public class SimpleAdapter
+ implements CollectionAdapter {
+
+ private Document<Feed> feed_doc;
+
+ public ResponseContext extensionRequest(RequestContext request) {
+ return ProviderHelper.notallowed(
+ request,
+ "Method Not Allowed",
+ ProviderHelper.getDefaultMethods(request));
+ }
+
+ private Document<Feed> init_feed_doc(
+ Abdera abdera,
+ RequestContext context) {
+ Feed feed =
+ (Feed) context.getAttribute(
+ Scope.SESSION,
+ "feed");
+ if (feed == null) {
+ Factory factory = abdera.getFactory();
+ feed = factory.newFeed();
+ try {
+ feed.setId("tag:example.org,2006:feed");
+ feed.setTitle("Simple");
+ feed.setUpdated(new Date());
+ feed.addLink("");
+ feed.addLink("","self");
+ feed.addAuthor("Simple");
+ feed.setBaseUri(context.getResolvedUri());
+ } catch (Exception e) {}
+ context.setAttribute(
+ Scope.SESSION,
+ "feed",
+ feed);
+ }
+ return feed.getDocument();
+ }
+
+ private synchronized Document<Feed> get_feed_doc(
+ RequestContext context) {
+ if (feed_doc == null)
+ feed_doc =
+ init_feed_doc(
+ context.getAbdera(),
+ context);
+ return feed_doc;
+ }
+
+ public ResponseContext getFeed(
+ RequestContext request) {
+ Document<Feed> feed = get_feed_doc(request);
+ return ProviderHelper.returnBase(
+ feed,
+ 200,
+ feed.getRoot().getUpdated())
+ .setEntityTag(
+ ProviderHelper.calculateEntityTag(
+ feed.getRoot()));
+ }
+
+
+ public ResponseContext deleteEntry(
+ RequestContext request) {
+ Entry entry = getAbderaEntry(request);
+ if (entry != null)
+ entry.discard();
+ return ProviderHelper.nocontent();
+ }
+
+ public ResponseContext getEntry(
+ RequestContext request) {
+ Entry entry = (Entry) getAbderaEntry(request);
+ if (entry != null) {
+ Feed feed = entry.getParentElement();
+ entry = (Entry) entry.clone();
+ entry.setSource(feed.getAsSource());
+ Document<Entry> entry_doc = entry.getDocument();
+ return
+ ProviderHelper.returnBase(
+ entry_doc,
+ 200,
+ entry.getEdited())
+ .setEntityTag(
+ ProviderHelper.calculateEntityTag(
+ entry));
+ } else {
+ return ProviderHelper.notfound(request);
+ }
+ }
+
+ public ResponseContext postEntry(
+ RequestContext request) {
+ Abdera abdera = request.getAbdera();
+ try {
+ if (!checkMediaType(request))
+ return ProviderHelper.notsupported(request);
+ Document<Entry> entry_doc =
+ (Document<Entry>) request.getDocument(
+ abdera.getParser()).clone();
+ if (entry_doc != null) {
+ Entry entry = entry_doc.getRoot();
+ if (!ProviderHelper.isValidEntry(entry))
+ return ProviderHelper.badrequest(request);
+ setEntryDetails(
+ entry,
+ abdera.getFactory().newUuidUri(),
+ request.getTarget().getParameter("collection"));
+ Feed feed = get_feed_doc(request).getRoot();
+ feed.insertEntry(entry);
+ feed.setUpdated(new Date());
+ BaseResponseContext rc =
+ (BaseResponseContext) ProviderHelper.returnBase(
+ entry_doc, 201, entry.getEdited());
+ return
+ rc.setLocation(
+ ProviderHelper.resolveBase(request).resolve(
+ entry.getEditLinkResolvedHref()).toString())
+ .setContentLocation(rc.getLocation().toString())
+ .setEntityTag(ProviderHelper.calculateEntityTag(entry));
+ } else {
+ return ProviderHelper.badrequest(request);
+ }
+ } catch (ParseException pe) {
+ return ProviderHelper.notsupported(request);
+ } catch (ClassCastException cce) {
+ return ProviderHelper.notsupported(request);
+ } catch (Exception e) {
+ return ProviderHelper.badrequest(request);
+ }
+ }
+
+ private boolean checkMediaType(RequestContext request) {
+ MimeType contentType = request.getContentType();
+ String ctype =
+ contentType != null ?
+ contentType.toString() :
+ null;
+ return ctype != null && MimeTypeHelper.isAtom(ctype);
+ }
+
+ private void setEntryDetails(
+ Entry entry,
+ String id,
+ String collection) {
+ entry.setUpdated(new Date());
+ entry.setEdited(entry.getUpdated());
+ entry.getIdElement().setValue(id);
+ entry.addLink(
+ "/atom/" + collection + "/" + entry.getId().toASCIIString(),
+ "edit");
+ }
+
+ public ResponseContext putEntry(
+ RequestContext request) {
+ Abdera abdera = request.getAbdera();
+ Entry orig_entry = getAbderaEntry(request);
+ if (orig_entry != null) {
+ try {
+ if (!checkMediaType(request))
+ return ProviderHelper.notsupported(
+ request);
+ Document<Entry> entry_doc =
+ (Document<Entry>) request.getDocument(
+ abdera.getParser()).clone();
+ if (entry_doc != null) {
+ Entry entry = entry_doc.getRoot();
+ if (!entry.getId().equals(orig_entry.getId()))
+ return ProviderHelper.conflict(request);
+ if (!ProviderHelper.isValidEntry(entry))
+ return ProviderHelper.badrequest(request);
+ setEntryDetails(
+ entry,
+ orig_entry.getId().toString(),
+ request.getTarget().getParameter("collection"));
+ orig_entry.discard();
+ Feed feed = get_feed_doc(request).getRoot();
+ feed.insertEntry(entry);
+ feed.setUpdated(new Date());
+ return ProviderHelper.nocontent();
+ } else {
+ return ProviderHelper.badrequest(request);
+ }
+ } catch (ParseException pe) {
+ return ProviderHelper.notsupported(request);
+ } catch (ClassCastException cce) {
+ return ProviderHelper.notsupported(request);
+ } catch (Exception e) {
+ return ProviderHelper.badrequest(request);
+ }
+ } else {
+ return ProviderHelper.notfound(request);
+ }
+ }
+
+ private Entry getAbderaEntry(
+ RequestContext request) {
+ try {
+ return get_feed_doc(
+ request)
+ .getRoot()
+ .getEntry(
+ getEntryID(request));
+ } catch (Exception e) {}
+ return null;
+ }
+
+ private String getEntryID(
+ RequestContext request) {
+ if (request.getTarget().getType() != TargetType.TYPE_ENTRY)
+ return null;
+ String[] segments = request.getUri().toString().split("/");
+ return UrlEncoding.decode(segments[segments.length-1]);
+ }
+
+}
\ No newline at end of file
Added: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleProvider.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleProvider.java?rev=614076&view=auto
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleProvider.java (added)
+++ incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleProvider.java Mon Jan 21 16:53:07 2008
@@ -0,0 +1,76 @@
+/*
+* 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.test.simple;
+
+import java.io.IOException;
+
+import org.apache.abdera.protocol.server.CollectionAdapter;
+import org.apache.abdera.protocol.server.RequestContext;
+import org.apache.abdera.protocol.server.ResponseContext;
+import org.apache.abdera.protocol.server.context.StreamWriterResponseContext;
+import org.apache.abdera.protocol.server.impl.AbstractWorkspaceProvider;
+import org.apache.abdera.util.Constants;
+import org.apache.abdera.writer.StreamWriter;
+
+public class SimpleProvider
+ extends AbstractWorkspaceProvider {
+
+ private final CollectionAdapter adapter;
+ private final String name;
+ private final String href;
+
+ public SimpleProvider(
+ CollectionAdapter adapter,
+ String name,
+ String href) {
+ this.adapter = adapter;
+ this.name = name;
+ this.href = href;
+ }
+
+ public CollectionAdapter getCollectionAdapter(
+ RequestContext request) {
+ return adapter;
+ }
+
+ protected ResponseContext getServiceDocument(
+ RequestContext request) {
+ return
+ new StreamWriterResponseContext(request.getAbdera()) {
+ protected void writeTo(
+ StreamWriter sw)
+ throws IOException {
+ sw.startDocument()
+ .startService()
+ .startWorkspace()
+ .writeTitle(name)
+ .startCollection(href)
+ .writeTitle(name)
+ .writeAcceptsEntry()
+ .startCategories(false)
+ .endCategories()
+ .endCollection()
+ .endWorkspace()
+ .endService()
+ .endDocument();
+ }
+ }
+ .setStatus(200)
+ .setContentType(Constants.APP_MEDIA_TYPE);
+ }
+}
Modified: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleServiceContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleServiceContext.java?rev=614076&r1=614075&r2=614076&view=diff
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleServiceContext.java (original)
+++ incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleServiceContext.java Mon Jan 21 16:53:07 2008
@@ -17,35 +17,15 @@
*/
package org.apache.abdera.protocol.server.test.simple;
-import java.io.IOException;
-import java.util.Date;
-import javax.activation.MimeType;
-import org.apache.abdera.Abdera;
-import org.apache.abdera.factory.Factory;
-import org.apache.abdera.i18n.text.UrlEncoding;
-import org.apache.abdera.model.Document;
-import org.apache.abdera.model.Entry;
-import org.apache.abdera.model.Feed;
-import org.apache.abdera.parser.ParseException;
import org.apache.abdera.protocol.Resolver;
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.ResponseContext;
import org.apache.abdera.protocol.server.Target;
import org.apache.abdera.protocol.server.TargetType;
-import org.apache.abdera.protocol.server.RequestContext.Scope;
-import org.apache.abdera.protocol.server.impl.AbstractCombinedProvider;
-import org.apache.abdera.protocol.server.impl.BaseResponseContext;
-import org.apache.abdera.protocol.server.impl.DefaultServiceContext;
+import org.apache.abdera.protocol.server.context.DefaultServiceContext;
import org.apache.abdera.protocol.server.impl.RegexTargetResolver;
-import org.apache.abdera.protocol.server.impl.StreamWriterResponseContext;
-import org.apache.abdera.util.Constants;
-import org.apache.abdera.util.EntityTag;
-import org.apache.abdera.util.MimeTypeHelper;
-import org.apache.abdera.writer.StreamWriter;
@SuppressWarnings("unchecked")
public class SimpleServiceContext
@@ -54,245 +34,18 @@
public synchronized Provider getProvider(
RequestContext request) {
if (provider == null)
- provider = new SimpleProvider();
+ provider =
+ new SimpleProvider(
+ new SimpleAdapter(),
+ "Simple",
+ "/atom/feed");
return provider;
}
public Resolver<Target> getTargetResolver() {
return new RegexTargetResolver()
.setPattern("/atom(\\?[^#]*)?", TargetType.TYPE_SERVICE)
- .setPattern("/atom/feed(\\?[^#]*)?", TargetType.TYPE_COLLECTION)
- .setPattern("/atom/feed/([^/#?]+)(\\?[^#]*)?", TargetType.TYPE_ENTRY);
- }
-
- public static class SimpleProvider
- extends AbstractCombinedProvider {
-
- private EntityTag service_etag = new EntityTag("simple");
- private Document<Feed> feed_doc;
-
- private Document<Feed> init_feed_doc(
- Abdera abdera,
- RequestContext context) {
- Feed feed =
- (Feed) context.getAttribute(
- Scope.SESSION,
- "feed");
- if (feed == null) {
- Factory factory = abdera.getFactory();
- feed = factory.newFeed();
- try {
- feed.setId("tag:example.org,2006:feed");
- feed.setTitle("Simple");
- feed.setUpdated(new Date());
- feed.addLink("");
- feed.addLink("","self");
- feed.addAuthor("Simple");
- } catch (Exception e) {}
- context.setAttribute(
- Scope.SESSION,
- "feed",
- feed);
- }
- return feed.getDocument();
- }
-
- private synchronized Document<Feed> get_feed_doc(
- RequestContext context) {
- if (feed_doc == null)
- feed_doc =
- init_feed_doc(
- context.getAbdera(),
- context);
- return feed_doc;
- }
-
-
- protected ResponseContext getServiceDocument(
- RequestContext request) {
- return
- new StreamWriterResponseContext(request.getAbdera()) {
- protected void writeTo(
- StreamWriter sw)
- throws IOException {
- sw.startDocument()
- .startService()
- .startWorkspace()
- .writeTitle("Simple")
- .startCollection("atom/feed")
- .writeTitle("Simple")
- .writeAcceptsEntry()
- .startCategories(false)
- .endCategories()
- .endCollection()
- .endWorkspace()
- .endService()
- .endDocument();
- }
- }
- .setStatus(200)
- .setEntityTag(service_etag)
- .setContentType(Constants.APP_MEDIA_TYPE);
- }
-
- public ResponseContext getFeed(
- RequestContext request) {
- Document<Feed> feed = get_feed_doc(request);
- return ProviderHelper.returnBase(
- feed,
- 200,
- feed.getRoot().getUpdated())
- .setEntityTag(
- ProviderHelper.calculateEntityTag(
- feed.getRoot()));
- }
-
-
- public ResponseContext deleteEntry(
- RequestContext request) {
- Entry entry = getAbderaEntry(request);
- if (entry != null)
- entry.discard();
- return ProviderHelper.nocontent();
- }
-
- public ResponseContext getEntry(
- RequestContext request) {
- Entry entry = (Entry) getAbderaEntry(request);
- if (entry != null) {
- Feed feed = entry.getParentElement();
- entry = (Entry) entry.clone();
- entry.setSource(feed.getAsSource());
- Document<Entry> entry_doc = entry.getDocument();
- return
- ProviderHelper.returnBase(
- entry_doc,
- 200,
- entry.getEdited())
- .setEntityTag(
- ProviderHelper.calculateEntityTag(
- entry));
- } else {
- return ProviderHelper.notfound(request);
- }
- }
-
- public ResponseContext postEntry(
- RequestContext request) {
- Abdera abdera = request.getAbdera();
- try {
- if (!checkMediaType(request))
- return ProviderHelper.notsupported(request);
- Document<Entry> entry_doc =
- (Document<Entry>) request.getDocument(
- abdera.getParser()).clone();
- if (entry_doc != null) {
- Entry entry = entry_doc.getRoot();
- if (!ProviderHelper.isValidEntry(entry))
- return ProviderHelper.badrequest(request);
- setEntryDetails(entry, abdera.getFactory().newUuidUri());
- Feed feed = get_feed_doc(request).getRoot();
- feed.insertEntry(entry);
- feed.setUpdated(new Date());
- BaseResponseContext rc =
- (BaseResponseContext) ProviderHelper.returnBase(
- entry_doc, 201, entry.getEdited());
- return
- rc.setLocation(
- ProviderHelper.resolveBase(request).resolve(
- entry.getEditLinkResolvedHref()).toString())
- .setContentLocation(rc.getLocation().toString())
- .setEntityTag(ProviderHelper.calculateEntityTag(entry));
- } else {
- return ProviderHelper.badrequest(request);
- }
- } catch (ParseException pe) {
- return ProviderHelper.notsupported(request);
- } catch (ClassCastException cce) {
- return ProviderHelper.notsupported(request);
- } catch (Exception e) {
- return ProviderHelper.badrequest(request);
- }
- }
-
- private boolean checkMediaType(RequestContext request) {
- MimeType contentType = request.getContentType();
- String ctype =
- contentType != null ?
- contentType.toString() :
- null;
- return ctype != null && MimeTypeHelper.isAtom(ctype);
- }
-
- private void setEntryDetails(
- Entry entry,
- String id) {
- entry.setUpdated(new Date());
- entry.setEdited(entry.getUpdated());
- entry.getIdElement().setValue(id);
- entry.addLink(
- "atom/feed/" + entry.getId().toString(),
- "edit");
- }
-
- public ResponseContext putEntry(
- RequestContext request) {
- Abdera abdera = request.getAbdera();
- Entry orig_entry = getAbderaEntry(request);
- if (orig_entry != null) {
- try {
- if (!checkMediaType(request))
- return ProviderHelper.notsupported(
- request);
- Document<Entry> entry_doc =
- (Document<Entry>) request.getDocument(
- abdera.getParser()).clone();
- if (entry_doc != null) {
- Entry entry = entry_doc.getRoot();
- if (!entry.getId().equals(orig_entry.getId()))
- return ProviderHelper.conflict(request);
- if (!ProviderHelper.isValidEntry(entry))
- return ProviderHelper.badrequest(request);
- setEntryDetails(entry, orig_entry.getId().toString());
- orig_entry.discard();
- Feed feed = get_feed_doc(request).getRoot();
- feed.insertEntry(entry);
- feed.setUpdated(new Date());
- return ProviderHelper.nocontent();
- } else {
- return ProviderHelper.badrequest(request);
- }
- } catch (ParseException pe) {
- return ProviderHelper.notsupported(request);
- } catch (ClassCastException cce) {
- return ProviderHelper.notsupported(request);
- } catch (Exception e) {
- return ProviderHelper.badrequest(request);
- }
- } else {
- return ProviderHelper.notfound(request);
- }
- }
-
- private Entry getAbderaEntry(
- RequestContext request) {
- try {
- return get_feed_doc(
- request)
- .getRoot()
- .getEntry(
- getEntryID(request));
- } catch (Exception e) {}
- return null;
- }
-
- private String getEntryID(
- RequestContext request) {
- if (request.getTarget().getType() != TargetType.TYPE_ENTRY)
- return null;
- String[] segments = request.getUri().toString().split("/");
- return UrlEncoding.decode(segments[segments.length-1]);
- }
-
+ .setPattern("/atom/([^/#?]+)(\\?[^#]*)?", TargetType.TYPE_COLLECTION, "collection")
+ .setPattern("/atom/([^/#?]+)/([^/#?]+)(\\?[^#]*)?", TargetType.TYPE_ENTRY, "collection","entry");
}
}
Modified: incubator/abdera/java/branches/server_refactor/src/test/resources/abdera/adapter/sample.properties
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/resources/abdera/adapter/sample.properties?rev=614076&r1=614075&r2=614076&view=diff
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/resources/abdera/adapter/sample.properties (original)
+++ incubator/abdera/java/branches/server_refactor/src/test/resources/abdera/adapter/sample.properties Mon Jan 21 16:53:07 2008
@@ -1,4 +1,4 @@
feedUri=http://localhost:8080/sample
-adapterClassName=org.apache.abdera.protocol.server.provider.basic.SampleBasicAdapter
+adapterClassName=org.apache.abdera.protocol.server.test.basic.SampleBasicAdapter
title=title for any sample feed
author=rayc