/*
* 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.restlet.resources;
import java.util.Collections;
import org.restlet.data.Method;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.Variant;
import org.restlet.resource.ResourceException;
import com.semagia.atomico.MediaType;
import com.semagia.atomico.dm.IWritableRepresentation;
import com.semagia.atomico.server.dm.ISnapshotInfo;
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>
* @version $Rev: 66 $ - $Date: 2010-09-06 10:29:07 -0500 (Mon, 06 Sep 2010) $
*/
public class SnapshotResource extends AbstractBaseResource implements IVariantConstants {
/**
* The snapshot info to operate on.
*/
private ISnapshotInfo _snapshotInfo;
/**
* Initializes the instance.
*/
public SnapshotResource() {
super();
}
/* (non-Javadoc)
* @see org.restlet.resource.ServerResource#getPreferredVariant(org.restlet.data.Method)
*/
@Override
public Variant getPreferredVariant(Method method) {
if (Method.GET.equals(method)) {
return VARIANT_XTM;
}
return super.getPreferredVariant(method);
}
/* (non-Javadoc)
* @see org.restlet.resource.UniformResource#doInit()
*/
@Override
protected void doInit() throws ResourceException {
super.initializeCollectionInfo(false);
final String snapshotId = (String) getRequest().getAttributes().get(SNAPSHOT_ID);
if (_collInfo != null) {
try {
_snapshotInfo = snapshotId != null ? _storage.getSnapshotInfo(_collInfo.getCollectionId(), snapshotId)
: null;
}
catch (StorageException ex) {
throw new ResourceException(ex);
}
}
}
/* (non-Javadoc)
* @see org.restlet.resource.ServerResource#isExists()
*/
@Override
public boolean isExisting() {
return _snapshotInfo != null;
}
/* (non-Javadoc)
* @see com.semagia.atomico.server.impl.restlet.resources.AbstractBaseResource#getMediaTypes()
*/
@Override
protected Iterable<MediaType> getMediaTypes() {
return _snapshotInfo != null ? _snapshotInfo.getMediaTypes()
: Collections.<MediaType>emptySet();
}
/* (non-Javadoc)
* @see com.semagia.atomico.server.impl.restlet.resources.AbstractBaseResource#lastModification()
*/
@Override
protected long lastModification() {
return _snapshotInfo != null ? _snapshotInfo.getUpdated()
: -1;
}
/* (non-Javadoc)
* @see org.restlet.resource.ServerResource#get(org.restlet.representation.Variant)
*/
@Override
public Representation get(Variant variant) throws ResourceException {
if (!isExisting()) {
throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);
}
IWritableRepresentation tm = null;
try {
tm = _storage.getSnapshot(_collInfo.getCollectionId(),
_snapshotInfo.getSnapshotId(),
MediaTypeUtils.toAtomicoMediaType(variant.getMediaType()));
}
catch (StorageException ex) {
throw new ResourceException(ex);
}
return new WritableAwareRepresentation(variant.getMediaType(), tm,
_snapshotInfo.getUpdated());
}
}