HttpSimpleCollection.java
/***************************************************************************
Copyright 2012 Emily Estes
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 net.metanotion.contentstore.simple;
import java.io.IOException;
import net.metanotion.json.JsonObject;
import net.metanotion.util.Dictionary;
import net.metanotion.util.Dispatcher;
import net.metanotion.util.Unknown;
import net.metanotion.web.HttpStatus;
import net.metanotion.web.HttpValues;
import net.metanotion.web.RequestObject;
import net.metanotion.web.concrete.HttpException;
import net.metanotion.web.concrete.HttpUtil;
import net.metanotion.web.concrete.ObjectPrefixDispatcher;
import net.metanotion.web.concrete.URIPrefixDispatcher;
import net.metanotion.web.concrete.WebInterfaceDispatcher;
import net.metanotion.contentstore.SimpleCollection;
/** Create a new HTTP Collection Manager to provide sessions to manage access to SimpleCollection instances. */
public final class HttpSimpleCollection {
public static final String COLLECTION = "collection";
public static final String PAGE = "page";
public static final String ID = "id";
public static final String JSON = "json";
private static final HttpValues NOT_FOUND = HttpUtil.new404("");
private static final HttpException FORBIDDEN =
new HttpException(HttpUtil.newJsonResponse("{}", HttpStatus.FORBIDDEN.codeNumber()));
private static final SimpleCollectionApi notAuthorized = new SimpleCollectionApi() {
@Override public HttpValues notFound() { return NOT_FOUND; }
@Override public Object api(final String collection) { throw FORBIDDEN; }
@Override public JList list(final String collection, final int page) { throw FORBIDDEN; }
@Override public JsonObject create(final String collection, final JsonObject json) { throw FORBIDDEN; }
@Override public JsonObject get(final String collection, final String id) { throw FORBIDDEN; }
@Override public JsonObject update(final String collection, final String id, final JsonObject json) { throw FORBIDDEN; }
@Override public JRemoved remove(final String collection, final String id) { throw FORBIDDEN; }
};
/** Return a Dispatcher instance suitable for decoding API calls to CollectionManager instances.
@return The dispatcher.
@throws IOException if a dispatcher could not be generated.
*/
public static Dispatcher<? extends Object,RequestObject> dispatcher() throws IOException {
return new ObjectPrefixDispatcher<Object>(
new URIPrefixDispatcher()
.addDispatcher("item/", new ObjectPrefixDispatcher<Unknown>(new WebInterfaceDispatcher<>(ItemManager.class), ID))
.addDispatcher("", new WebInterfaceDispatcher<>(CollectionManager.class)),
COLLECTION);
}
private final String prefix;
private final int maxPageSize;
/** Create a new HTTP Collection Manager to provide sessions to manage access to SimpleCollection instances.
@param prefix The URI prefix for this CollectionManager.
@param maxPageSize The maximum size of pages returned by the CollectionManager.list method.
*/
public HttpSimpleCollection(final String prefix, final int maxPageSize) {
this.prefix = prefix;
this.maxPageSize = maxPageSize;
}
/** Create a new session backed by a dictionary of SimpleCollection instances.
@param collections The dictionary of collections backing this instance.
@return A SimpleCollectionApi instance to provide HTTP access to the collections.
*/
public SimpleCollectionApi newSession(final Dictionary<String,SimpleCollection<JsonObject>> collections) {
return new JsonCollection(collections, maxPageSize, prefix);
}
/** This creates a session that always returns an HTTP 403 - Forbidden to any requests against the collection.
@return A SimpleCollectionApi instance that makes the session forbidden to access.
*/
public SimpleCollectionApi new403() { return notAuthorized; }
}