Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / admin / RepositoryAdminResource.java
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
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.repository.resources.admin;
13
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.OutputStream;
17
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;
28
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;
37
38 import com.sun.jersey.api.view.Viewable;
39 import com.sun.jersey.core.header.FormDataContentDisposition;
40 import com.sun.jersey.multipart.FormDataParam;
41
42 public class RepositoryAdminResource {
43         
44         private static final Logger logger = LoggerFactory.getLogger(RepositoryAdminResource.class);
45         
46         
47         // @formatter:off
48         @GET
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
55         ) {
56                 // @formatter:on
57                 if (dump != null) {
58                         return this.dumpRepository();
59                 } else if (reset != null) {
60                         try {
61                                 ((GitBasedRepository) Prefs.INSTANCE.getRepository()).cleanAndResetHard();
62                         } catch (Exception e) {
63                                 Response res;
64                                 res = Response.serverError().entity(e.getMessage()).build();
65                                 return res;
66                         }
67                         return Response.noContent().build();
68                 } else if (commit != null) {
69                         try {
70                                 ((GitBasedRepository) Prefs.INSTANCE.getRepository()).addCommitPush();
71                         } catch (Exception e) {
72                                 Response res;
73                                 res = Response.serverError().entity(e.getMessage()).build();
74                                 return res;
75                         }
76                         return Response.noContent().build();
77                 } else {
78                         Viewable viewable = new Viewable("/jsp/admin/repository.jsp", this);
79                         return Response.ok().entity(viewable).build();
80                 }
81         }
82         
83         /**
84          * Imports the given ZIP
85          */
86         @POST
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();
91         }
92         
93         @DELETE
94         public void deleteRepositoryData() {
95                 ((IRepositoryAdministration) Repository.INSTANCE).doClear();
96         }
97         
98         @GET
99         @Produces(org.eclipse.winery.common.constants.MimeTypes.MIMETYPE_ZIP)
100         public Response dumpRepository() {
101                 StreamingOutput so = new StreamingOutput() {
102                         
103                         @Override
104                         public void write(OutputStream output) throws IOException, WebApplicationException {
105                                 ((IRepositoryAdministration) Repository.INSTANCE).doDump(output);
106                         }
107                 };
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();
111         }
112 }