/*
* Copyright 2008 - 2009 Lars Heuer (heuer[at]semagia.com). All rights reserved.
*
* 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 com.semagia.atomico.server.impl.jaxrs;
import java.io.InputStream;
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Response.Status;
import com.semagia.atomico.MediaType;
import com.semagia.atomico.dm.IWritableRepresentation;
import com.semagia.atomico.server.UnsupportedMediaTypeException;
import com.semagia.atomico.server.dm.ICollectionInfo;
import com.semagia.atomico.server.dm.IFragmentInfo;
import com.semagia.atomico.server.storage.IModifiableStorage;
import com.semagia.atomico.server.storage.StorageException;
import com.semagia.atomico.server.utils.LinkUtils;
/**
* Resource representing a list of available fragments for a particular
* collection.
*
* @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
*/
public final class FragmentsFeedResource extends AbstractCollectionAwareFeedResource {
private final long _lastModification;
FragmentsFeedResource(final Request req, final UriInfo uriInfo,
final ICollectionInfo collInfo) throws StorageException {
super(req, uriInfo, collInfo);
_lastModification = getStorage().lastFragmentModification(_collInfo.getCollectionId());
}
/* (non-Javadoc)
* @see com.semagia.atomico.server.impl.jaxrs.AbstractBaseResource#lastModification()
*/
@Override
protected long lastModification() {
return _lastModification;
}
/**
* Returns a fragments feed.
*
* @return The fragments feed.
* @throws StorageException In case of an error.
* @throws UnsupportedMediaTypeException
*/
@GET
public Response getFragments(@Context HttpHeaders headers) throws StorageException, UnsupportedMediaTypeException {
final Response.ResponseBuilder builder = makeResponseBuilder();
final IWritableRepresentation writable =
_feedFactory.createFragmentsFeed(getBaseURI(),
getStorage(), getConfiguration(), _lastModification,
_collInfo, MediaTypeUtils.toAtomicoMediaType(headers.getAcceptableMediaTypes()));
return ResponseUtils.buildStreamingEntity(builder, writable);
}
/**
* Returns a fragment.
*
* @param fragment The fragment identifier.
* @return A fragment.
* @throws StorageException In case of an error.
*/
@Path("/{fragment}")
public FragmentResource getFragment(@PathParam("fragment") String fragment) throws StorageException {
return new FragmentResource(_request, _collInfo, fragment);
}
/**
* Adds a fragment.
*
* @param in
* @return
* @throws StorageException
* @throws UnsupportedMediaTypeException
*/
@POST
public Response addFragment(final InputStream in, @Context HttpHeaders headers) throws StorageException, UnsupportedMediaTypeException {
final IModifiableStorage storage = super.getModifiableStorage();
final MediaType mediaType = MediaTypeUtils.toAtomicoMediaType(headers.getMediaType());
// Reject request iff the media type is null.
if (mediaType == null) {
// 415 == Unsupported Media Type
return Response.status(415).build();
}
final IFragmentInfo info = storage.addFragment(_collInfo.getCollectionId(), null);
if (info == null) {
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
}
return Response.created(LinkUtils.linkTo(getBaseURI(), _collInfo, info))
.lastModified(new Date(info.getUpdated()))
.build();
}
}