ArchiveService.java [src/csip] Revision: 71821307bfe742c00c6dc582c171224a9ac59935 Date: Fri Apr 21 11:46:19 MDT 2017
/*
* $Id$
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API and application suite.
*
* 2012-2017, Olaf David and others, OMSLab, Colorado State University.
*
* OMSLab licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
*/
package csip;
import csip.utils.Dates;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import csip.utils.JSONUtils;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
/**
* Archive service.
*
* @author od
*/
@Path("/a")
public class ArchiveService {
static final Logger LOG = Logger.getLogger(ArchiveService.class.getName());
/**
* Download an archive.
* @param uriInfo
* @param suid
* @return
*/
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Path("download/{suid}")
public Response downloadArchive(@Context UriInfo uriInfo, @Context HttpServletRequest req, @PathParam("suid") final String suid) {
Utils.checkRemoteAccessACL(req);
if (!Config.isArchiveEnabled()) {
String o = JSONUtils.error("archive disabled").toString();
return Response.status(404).entity(o).type(MediaType.APPLICATION_JSON).build();
}
try {
ModelArchive archive = Config.getArchiveStore().getArchive(suid);
if (archive != null) {
byte[] b = Config.getArchiveStore().getFile(suid, "files-" + suid + ".zip");
if (b != null) {
String ext = ".zip";
if (Arrays.equals(ArchiveStore.ARCHIVE_TOO_BIG.getBytes(), b)) {
ext = ".txt";
}
return Response.ok(QueryService.output(b)).header("Content-Disposition", "attachment; filename=archive-" + suid + ext).build();
}
}
} catch (Exception ex) {
Logger.getLogger(ArchiveService.class.getName()).log(Level.SEVERE, null, ex);
}
String o = JSONUtils.error("archive not found, suid unknown : " + suid).toString();
return Response.status(404).entity(o).type(MediaType.APPLICATION_JSON).build();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("remove/{suid}")
public String removeArchive(@Context UriInfo uriInfo, @Context HttpServletRequest req, @PathParam("suid") final String suid) {
Utils.checkRemoteAccessACL(req);
try {
if (!Config.isArchiveEnabled()) {
return JSONUtils.error("archive disabled. ").toString();
}
// try to find the file.
if (Config.getArchiveStore().hasArchive(suid)) {
Config.getArchiveStore().removeArchive(suid);
return JSONUtils.ok("removed: " + suid).toString();
}
return JSONUtils.error("suid unknown").toString();
} catch (Exception E) {
return JSONUtils.error("archive access problem: " + E.getMessage()).toString();
}
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("clean")
public String clean(@Context UriInfo uriInfo, @Context HttpServletRequest req) throws JSONException {
Utils.checkRemoteAccessACL(req);
if (!Config.isArchiveEnabled()) {
return JSONUtils.error("archive disabled. ").toString();
}
JSONObject json = new JSONObject();
int removed = 0;
int invalid = 0;
Set<String> keys = Config.getArchiveStore().keys(0, Integer.MAX_VALUE, null, true);
json.put("archive.total", Config.getArchiveStore().getCount());
DateFormat df = Dates.newISOFormat();
Date now = new Date();
for (String key : keys) {
try {
ModelArchive archive = Config.getArchiveStore().getArchive(key);
if (archive == null) {
continue;
}
Date etime = df.parse(archive.getEtime());
if (now.after(etime)) {
removed++;
Config.getArchiveStore().removeArchive(key);
}
} catch (Exception E) {
invalid++;
}
}
json.put("archive.removed", removed);
json.put("archive.invalid", invalid);
return json.toString(4);
}
}