/*
* 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 javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
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.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import com.semagia.atomico.MediaType;
import com.semagia.atomico.dm.IWritableRepresentation;
import com.semagia.atomico.server.dm.ICollectionInfo;
import com.semagia.atomico.server.dm.ISnapshotInfo;
import com.semagia.atomico.server.storage.IModifiableStorage;
import com.semagia.atomico.server.storage.StorageException;
/**
* Represents a snapshot.
*
* @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
*/
public final class SnapshotResource extends AbstractBaseResource {
private final ICollectionInfo _collInfo;
private final ISnapshotInfo _snapshotInfo;
private final long _lastModification;
public SnapshotResource(final Request req, final ICollectionInfo collInfo,
final String snapshot) throws StorageException {
super(req);
_collInfo = collInfo;
_snapshotInfo = getStorage().getSnapshotInfo(collInfo.getId(), snapshot);
ResponseUtils.ensureExists(_snapshotInfo);
_lastModification = _snapshotInfo.getUpdated();
}
/* (non-Javadoc)
* @see com.semagia.atomico.server.impl.jaxrs.AbstractBaseResource#lastModification()
*/
@Override
protected long lastModification() {
return _lastModification;
}
/**
* Returns the snapshot.
*
* @param headers Http headers.
* @return The snapshot.
* @throws StorageException In case of an error.
*/
@GET
@Produces("*/*")
public Response getSnapshot(@Context HttpHeaders headers) throws StorageException {
final MediaType mediaType = com.semagia.atomico.server.utils.MediaTypeUtils.getPreferredMediaType(_snapshotInfo.getMediaTypes(), MediaTypeUtils.toAtomicoMediaType(headers.getAcceptableMediaTypes()));
if (mediaType == null) {
return ResponseUtils.notAcceptable(_snapshotInfo.getMediaTypes());
}
final ResponseBuilder builder = makeResponseBuilder();
// Add Vary header
builder.variants(MediaTypeUtils.asVariants(_snapshotInfo.getMediaTypes()));
builder.type(MediaTypeUtils.toJaxRSMediaType(mediaType));
return ResponseUtils.buildStreamingEntity(builder, _getSnapshot(mediaType));
}
/**
* Deletes the snapshot.
*
* @return A response indicating if the snapshot was sucessfully deleted.
* @throws StorageException In case of an error.
*/
@DELETE
@Produces("*/*")
public Response deleteSnapshot() throws StorageException {
final IModifiableStorage storage = super.getModifiableStorage();
if (storage == null) {
return ResponseUtils.methodNotAllowed();
}
final boolean success = storage.deleteSnapshot(_collInfo.getCollectionId(), _snapshotInfo.getSnapshotId());
final ResponseBuilder builder = success ? Response.ok() : Response.status(Status.INTERNAL_SERVER_ERROR);
return builder.build();
}
/**
* Returns a representation of the requested snapshot.
*
* @param mediaType The media type.
* @return A representation of the requested snapshot.
* @throws StorageException In case of an error.
*/
private IWritableRepresentation _getSnapshot(final MediaType mediaType) throws StorageException {
return getStorage().getSnapshot(_collInfo.getCollectionId(),
_snapshotInfo.getSnapshotId(), mediaType);
}
}