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.imports.genericimports;
14 import java.util.SortedSet;
16 import javax.ws.rs.GET;
17 import javax.ws.rs.Path;
18 import javax.ws.rs.PathParam;
19 import javax.ws.rs.core.Response;
20 import javax.ws.rs.core.Response.Status;
22 import org.eclipse.winery.common.RepositoryFileReference;
23 import org.eclipse.winery.common.Util;
24 import org.eclipse.winery.common.ids.definitions.imports.GenericImportId;
25 import org.eclipse.winery.model.tosca.TExtensibleElements;
26 import org.eclipse.winery.model.tosca.TImport;
27 import org.eclipse.winery.repository.backend.BackendUtils;
28 import org.eclipse.winery.repository.backend.Repository;
29 import org.eclipse.winery.repository.resources.AbstractComponentInstanceResource;
31 public class GenericImportResource extends AbstractComponentInstanceResource {
33 // The import belonging to this resource
34 protected final TImport theImport;
37 public GenericImportResource(GenericImportId id) {
40 boolean needsPersistence = false;
42 if (this.getDefinitions().getServiceTemplateOrNodeTypeOrNodeTypeImplementation().isEmpty()) {
43 // super class loaded an existing definitions
45 // we have to manually assign our import right
46 this.theImport = this.getDefinitions().getImport().get(0);
48 // element is not assigned as there are no service templates/...
49 // we assign the value to be sure that no NPEs occur
50 this.element = this.theImport;
52 // super class created a new import
55 this.theImport = (TImport) this.element;
57 // undo the side effect of adding it at the wrong place at TDefinitions
58 this.getDefinitions().getServiceTemplateOrNodeTypeOrNodeTypeImplementation().clear();
60 // add import at the right place
61 this.getDefinitions().getImport().add(this.theImport);
63 // Super class has persisted the definitions
64 // We have to persist the new variant
65 needsPersistence = true;
68 if (this.theImport.getLocation() == null) {
69 // invalid import -- try to synchronize with storage
71 SortedSet<RepositoryFileReference> containedFiles = Repository.INSTANCE.getContainedFiles(id);
72 // there is also a .definitions contained
73 // we are only interested in the non-.definitions
74 for (RepositoryFileReference ref : containedFiles) {
75 if (!ref.getFileName().endsWith(".definitions")) {
76 // associated file found
77 // set the filename of the import to the found xsd
78 // TODO: no more validity checks are done currently. In the case of XSD: targetNamespace matches, not more than one xsd
79 this.theImport.setLocation(ref.getFileName());
80 needsPersistence = true;
86 if (needsPersistence) {
87 BackendUtils.persist(this);
92 protected TExtensibleElements createNewElement() {
93 throw new IllegalStateException("This should not never happen.");
97 protected void copyIdToFields() {
98 // this.theImport cannot be used as this method is called by the super constructor
99 ((TImport) this.element).setNamespace(this.id.getNamespace().getDecoded());
104 public Response getFile(@PathParam("filename") String fileName) {
105 fileName = Util.URLdecode(fileName);
107 if ((location = this.getLocation()) == null) {
108 return Response.status(Status.NOT_FOUND).build();
110 if (!location.equals(fileName)) {
111 return Response.status(Status.NOT_FOUND).build();
113 RepositoryFileReference ref = new RepositoryFileReference(this.id, location);
114 return BackendUtils.returnRepoPath(ref, null);
118 public String getLocation() {
119 return this.theImport.getLocation();
123 * @return a name suitable for componentnaming.jspf
125 public String getName() {
126 if (this.getLocation() == null) {
127 return this.id.getXmlId().getDecoded();
129 return this.getLocation();