1 /*******************************************************************************
2 * Copyright (c) 2012-2013 University of Stuttgart.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.repository.resources.admin;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.OutputStream;
18 import javax.ws.rs.Consumes;
19 import javax.ws.rs.DELETE;
20 import javax.ws.rs.GET;
21 import javax.ws.rs.POST;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.QueryParam;
24 import javax.ws.rs.WebApplicationException;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
27 import javax.ws.rs.core.StreamingOutput;
29 import org.eclipse.winery.repository.Prefs;
30 import org.eclipse.winery.repository.backend.IRepositoryAdministration;
31 import org.eclipse.winery.repository.backend.Repository;
32 import org.eclipse.winery.repository.backend.filebased.GitBasedRepository;
33 import org.restdoc.annotations.RestDoc;
34 import org.restdoc.annotations.RestDocParam;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.sun.jersey.api.view.Viewable;
39 import com.sun.jersey.core.header.FormDataContentDisposition;
40 import com.sun.jersey.multipart.FormDataParam;
42 public class RepositoryAdminResource {
44 private static final Logger logger = LoggerFactory.getLogger(RepositoryAdminResource.class);
49 @Produces(MediaType.TEXT_HTML) // we cannot add MimeTypes.MIMETYPE_ZIP as dumpRepository also produces that mimetype
50 @RestDoc(methodDescription = "Returns the repository admin page and implements administration utility")
51 public Response onGet(
52 @QueryParam(value = "dump") @RestDocParam(description = "If given, a dump of the repository is sent") String dump,
53 @QueryParam(value = "reset") @RestDocParam(description = "Resets the repository to the last “official” known state") String reset,
54 @QueryParam(value = "commit") @RestDocParam(description = "Commits the current state to the repository and pushes it upstream") String commit
58 return this.dumpRepository();
59 } else if (reset != null) {
61 ((GitBasedRepository) Prefs.INSTANCE.getRepository()).cleanAndResetHard();
62 } catch (Exception e) {
64 res = Response.serverError().entity(e.getMessage()).build();
67 return Response.noContent().build();
68 } else if (commit != null) {
70 ((GitBasedRepository) Prefs.INSTANCE.getRepository()).addCommitPush();
71 } catch (Exception e) {
73 res = Response.serverError().entity(e.getMessage()).build();
76 return Response.noContent().build();
78 Viewable viewable = new Viewable("/jsp/admin/repository.jsp", this);
79 return Response.ok().entity(viewable).build();
84 * Imports the given ZIP
87 @Consumes(MediaType.MULTIPART_FORM_DATA)
88 public Response importRepositoryDump(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) {
89 ((IRepositoryAdministration) Repository.INSTANCE).doImport(uploadedInputStream);
90 return Response.noContent().build();
94 public void deleteRepositoryData() {
95 ((IRepositoryAdministration) Repository.INSTANCE).doClear();
99 @Produces(org.eclipse.winery.common.constants.MimeTypes.MIMETYPE_ZIP)
100 public Response dumpRepository() {
101 StreamingOutput so = new StreamingOutput() {
104 public void write(OutputStream output) throws IOException, WebApplicationException {
105 ((IRepositoryAdministration) Repository.INSTANCE).doDump(output);
108 StringBuilder sb = new StringBuilder();
109 sb.append("attachment;filename=\"repository.zip\"");
110 return Response.ok().header("Content-Disposition", sb.toString()).type(org.eclipse.winery.common.constants.MimeTypes.MIMETYPE_ZIP).entity(so).build();