Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / GenericVisualAppearanceResource.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;
13
14 import java.io.InputStream;
15 import java.net.URI;
16 import java.util.Map;
17
18 import javax.ws.rs.Consumes;
19 import javax.ws.rs.DELETE;
20 import javax.ws.rs.GET;
21 import javax.ws.rs.HeaderParam;
22 import javax.ws.rs.PUT;
23 import javax.ws.rs.Path;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 import javax.xml.namespace.QName;
27
28 import org.eclipse.winery.common.RepositoryFileReference;
29 import org.eclipse.winery.common.ids.elements.TOSCAElementId;
30 import org.eclipse.winery.repository.Prefs;
31 import org.eclipse.winery.repository.Utils;
32 import org.eclipse.winery.repository.backend.BackendUtils;
33 import org.eclipse.winery.repository.backend.constants.Filename;
34 import org.eclipse.winery.repository.datatypes.ids.elements.VisualAppearanceId;
35 import org.eclipse.winery.repository.resources.entitytypes.TopologyGraphElementEntityTypeResource;
36
37 //import com.fasterxml.jackson.annotation.JsonIgnore; // currently not required
38 import com.sun.jersey.multipart.FormDataBodyPart;
39 import com.sun.jersey.multipart.FormDataParam;
40
41 /**
42  * Contains methods for both visual appearance for
43  * <ul>
44  * <li>node types</li>
45  * <li>relationship types</li>
46  * </ul>
47  */
48 public abstract class GenericVisualAppearanceResource {
49         
50         protected final Map<QName, String> otherAttributes;
51         protected final TopologyGraphElementEntityTypeResource res;
52         protected final TOSCAElementId id;
53         
54         
55         @DELETE
56         public Response onDelete() {
57                 return BackendUtils.delete(this.id);
58         }
59         
60         /**
61          * Used for GUI when accessing the resource as data E.g., for topology
62          * template
63          */
64         //@JsonIgnore
65         public URI getAbsoluteURL() {
66                 String URI = Prefs.INSTANCE.getResourcePath();
67                 URI = URI + "/" + Utils.getURLforPathInsideRepo(BackendUtils.getPathInsideRepo(this.id));
68                 return Utils.createURI(URI);
69         }
70         
71         //@JsonIgnore
72         public TOSCAElementId getId() {
73                 return this.id;
74         }
75         
76         /**
77          * @param res
78          * @param otherAttributes the other attributes of the node/relationship type
79          * @param id the id of this subresource required for storing the images
80          */
81         public GenericVisualAppearanceResource(TopologyGraphElementEntityTypeResource res, Map<QName, String> otherAttributes, VisualAppearanceId id) {
82                 this.id = id;
83                 this.res = res;
84                 this.otherAttributes = otherAttributes;
85         }
86         
87         /**
88          * Determines repository reference to file in repo
89          */
90         protected RepositoryFileReference getRepoFileRef(String name) {
91                 return new RepositoryFileReference(this.id, name);
92         }
93         
94         protected Response getImage(String name, String modified) {
95                 RepositoryFileReference target = this.getRepoFileRef(name);
96                 return BackendUtils.returnRepoPath(target, modified);
97         }
98         
99         /**
100          * Arbitrary images are supported. There currently is no check for valid
101          * image media types
102          */
103         protected Response putImage(String name, InputStream uploadedInputStream, MediaType mediaType) {
104                 RepositoryFileReference target = this.getRepoFileRef(name);
105                 return BackendUtils.putContentToFile(target, uploadedInputStream, mediaType);
106         }
107         
108         @GET
109         @Path("16x16")
110         public Response get16x16Image(@HeaderParam("If-Modified-Since") String modified) {
111                 // Even if the extension is "png", it might contain a jpg, too
112                 // We keep the file extension as the windows explorer can display previews even if the content is not a png
113                 return this.getImage(Filename.FILENAME_SMALL_ICON, modified);
114         }
115         
116         @PUT
117         @Path("16x16")
118         @Consumes(MediaType.MULTIPART_FORM_DATA)
119         public Response post16x16Image(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataBodyPart body) {
120                 return this.putImage(Filename.FILENAME_SMALL_ICON, uploadedInputStream, body.getMediaType());
121         }
122         
123 }