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.servicetemplates.selfserviceportal;
14 import java.io.IOException;
15 import java.util.List;
17 import javax.ws.rs.GET;
18 import javax.ws.rs.HeaderParam;
19 import javax.ws.rs.Path;
20 import javax.ws.rs.core.Response;
21 import javax.ws.rs.core.Response.Status;
23 import org.eclipse.winery.model.selfservice.ApplicationOption;
24 import org.eclipse.winery.common.RepositoryFileReference;
25 import org.eclipse.winery.repository.backend.BackendUtils;
26 import org.eclipse.winery.repository.backend.Repository;
27 import org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId;
28 import org.eclipse.winery.repository.resources._support.collections.IIdDetermination;
29 import org.eclipse.winery.repository.resources._support.collections.withid.EntityWithIdResource;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class OptionResource extends EntityWithIdResource<ApplicationOption> {
35 private static final Logger logger = LoggerFactory.getLogger(OptionResource.class);
37 static final String ICON_JPG = "icon.jpg";
38 static final String PLAN_INPUT_XML = "plan.input.xml";
39 private SelfServiceMetaDataId ssmdId;
42 public OptionResource(IIdDetermination<ApplicationOption> idDetermination, ApplicationOption o, int idx, List<ApplicationOption> list, SelfServicePortalResource res) {
43 super(idDetermination, o, idx, list, res);
44 this.ssmdId = ((SelfServicePortalResource) this.res).getId();
47 private String getFileNamePrefix() {
48 return OptionResource.getFileNamePrefix(this.o.getId());
51 public static String getFileNamePrefix(String id) {
52 return "option_" + id + "_";
55 @Path(OptionResource.ICON_JPG)
57 public Response getIcon(@HeaderParam("If-Modified-Since") String modified) {
58 RepositoryFileReference ref = new RepositoryFileReference(this.ssmdId, this.getFileNamePrefix() + OptionResource.ICON_JPG);
59 return BackendUtils.returnRepoPath(ref, modified);
62 @Path("planinputmessage")
64 public Response getPlanInputMessage(@HeaderParam("If-Modified-Since") String modified) {
65 RepositoryFileReference ref = new RepositoryFileReference(this.ssmdId, this.getFileNamePrefix() + OptionResource.PLAN_INPUT_XML);
66 return BackendUtils.returnRepoPath(ref, modified);
70 public Response onDelete() {
71 // delete icon and plan model reference ...
74 // we use the URL stored in the data instead of the generated URL to be compatible with manually edits
75 RepositoryFileReference ref = new RepositoryFileReference(this.ssmdId, this.o.getIconUrl());
77 Repository.INSTANCE.forceDelete(ref);
78 } catch (IOException e) {
79 OptionResource.logger.error("Could not remove file", e);
80 return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
84 // we use the URL stored in the data instead of the generated URL to be compatible with manually edits
85 ref = new RepositoryFileReference(this.ssmdId, this.o.getPlanInputMessageUrl());
87 Repository.INSTANCE.forceDelete(ref);
88 } catch (IOException e) {
89 OptionResource.logger.error("Could not remove file", e);
90 return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
93 // after deleting files, continue with list deletion
94 return super.onDelete();