Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / imports / genericimports / GenericImportResource.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.imports.genericimports;
13
14 import java.util.SortedSet;
15
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;
21
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;
30
31 public class GenericImportResource extends AbstractComponentInstanceResource {
32         
33         // The import belonging to this resource
34         protected final TImport theImport;
35         
36         
37         public GenericImportResource(GenericImportId id) {
38                 super(id);
39                 
40                 boolean needsPersistence = false;
41                 
42                 if (this.getDefinitions().getServiceTemplateOrNodeTypeOrNodeTypeImplementation().isEmpty()) {
43                         // super class loaded an existing definitions
44                         
45                         // we have to manually assign our import right
46                         this.theImport = this.getDefinitions().getImport().get(0);
47                         
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;
51                 } else {
52                         // super class created a new import
53                         
54                         // store it locally
55                         this.theImport = (TImport) this.element;
56                         
57                         // undo the side effect of adding it at the wrong place at TDefinitions
58                         this.getDefinitions().getServiceTemplateOrNodeTypeOrNodeTypeImplementation().clear();
59                         
60                         // add import at the right place
61                         this.getDefinitions().getImport().add(this.theImport);
62                         
63                         // Super class has persisted the definitions
64                         // We have to persist the new variant
65                         needsPersistence = true;
66                 }
67                 
68                 if (this.theImport.getLocation() == null) {
69                         // invalid import -- try to synchronize with storage
70                         
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;
81                                         break;
82                                 }
83                         }
84                 }
85                 
86                 if (needsPersistence) {
87                         BackendUtils.persist(this);
88                 }
89         }
90         
91         @Override
92         protected TExtensibleElements createNewElement() {
93                 throw new IllegalStateException("This should not never happen.");
94         }
95         
96         @Override
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());
100         }
101         
102         @GET
103         @Path("{filename}")
104         public Response getFile(@PathParam("filename") String fileName) {
105                 fileName = Util.URLdecode(fileName);
106                 String location;
107                 if ((location = this.getLocation()) == null) {
108                         return Response.status(Status.NOT_FOUND).build();
109                 }
110                 if (!location.equals(fileName)) {
111                         return Response.status(Status.NOT_FOUND).build();
112                 }
113                 RepositoryFileReference ref = new RepositoryFileReference(this.id, location);
114                 return BackendUtils.returnRepoPath(ref, null);
115                 
116         }
117         
118         public String getLocation() {
119                 return this.theImport.getLocation();
120         }
121         
122         /**
123          * @return a name suitable for componentnaming.jspf
124          */
125         public String getName() {
126                 if (this.getLocation() == null) {
127                         return this.id.getXmlId().getDecoded();
128                 } else {
129                         return this.getLocation();
130                 }
131         }
132         
133 }