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;
14 import java.io.InputStream;
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;
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;
37 //import com.fasterxml.jackson.annotation.JsonIgnore; // currently not required
38 import com.sun.jersey.multipart.FormDataBodyPart;
39 import com.sun.jersey.multipart.FormDataParam;
42 * Contains methods for both visual appearance for
45 * <li>relationship types</li>
48 public abstract class GenericVisualAppearanceResource {
50 protected final Map<QName, String> otherAttributes;
51 protected final TopologyGraphElementEntityTypeResource res;
52 protected final TOSCAElementId id;
56 public Response onDelete() {
57 return BackendUtils.delete(this.id);
61 * Used for GUI when accessing the resource as data E.g., for topology
65 public URI getAbsoluteURL() {
66 String URI = Prefs.INSTANCE.getResourcePath();
67 URI = URI + "/" + Utils.getURLforPathInsideRepo(BackendUtils.getPathInsideRepo(this.id));
68 return Utils.createURI(URI);
72 public TOSCAElementId getId() {
78 * @param otherAttributes the other attributes of the node/relationship type
79 * @param id the id of this subresource required for storing the images
81 public GenericVisualAppearanceResource(TopologyGraphElementEntityTypeResource res, Map<QName, String> otherAttributes, VisualAppearanceId id) {
84 this.otherAttributes = otherAttributes;
88 * Determines repository reference to file in repo
90 protected RepositoryFileReference getRepoFileRef(String name) {
91 return new RepositoryFileReference(this.id, name);
94 protected Response getImage(String name, String modified) {
95 RepositoryFileReference target = this.getRepoFileRef(name);
96 return BackendUtils.returnRepoPath(target, modified);
100 * Arbitrary images are supported. There currently is no check for valid
103 protected Response putImage(String name, InputStream uploadedInputStream, MediaType mediaType) {
104 RepositoryFileReference target = this.getRepoFileRef(name);
105 return BackendUtils.putContentToFile(target, uploadedInputStream, mediaType);
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);
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());