Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / entitytypes / artifacttypes / ArtifactTypesResource.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.entitytypes.artifacttypes;
13
14 import java.util.SortedSet;
15
16 import javax.ws.rs.GET;
17 import javax.ws.rs.Produces;
18 import javax.ws.rs.QueryParam;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.Response;
21 import javax.ws.rs.core.Response.Status;
22
23 import org.eclipse.winery.common.ids.definitions.ArtifactTypeId;
24 import org.eclipse.winery.repository.backend.Repository;
25 import org.eclipse.winery.repository.resources.AbstractComponentsResource;
26
27 public class ArtifactTypesResource extends AbstractComponentsResource<ArtifactTypeResource> {
28         
29         // This cannot be used as the INSTANCE is per startup of the whole
30         // application
31         // We could do some checking for the number of ArtifactTypeResources or
32         // timestamp,
33         //
34         // private final HashMap<String, ArtifactTypeResource> fileExtensionMapping
35         // = new ArtifactTypesResource().getFileExtensionMapping();
36         
37         /**
38          * @return a mapping from file extension to artifact type resources
39          */
40         // public HashMap<String, ArtifactTypeResource> getFileExtensionMapping() {
41         // HashMap<String, ArtifactTypeResource> res = new HashMap<String,
42         // ArtifactTypeResource>();
43         // for (ArtifactTypeResource at : this.getArtifactTypeResources()) {
44         // if (at.getAssociatedFileExtension() != null) {
45         // res.put(at.getAssociatedFileExtension(), at);
46         // }
47         // }
48         // return res;
49         // }
50         
51         @GET
52         // should be "QName", but that MIME type is not available. XLink is too
53         // complicated for our setup
54         @Produces(MediaType.TEXT_PLAIN)
55         public Response getArtifactTypeQNameForExtension(@QueryParam("extension") String extension) {
56                 if (extension == null) {
57                         return Response.status(Status.NOT_ACCEPTABLE).build();
58                 }
59                 ArtifactTypeResource artifactType = this.getArtifactTypeForExtension(extension);
60                 Response res;
61                 if (artifactType == null) {
62                         res = Response.status(Status.NOT_FOUND).build();
63                 } else {
64                         res = Response.ok().entity(artifactType.getId().getQName().toString()).build();
65                 }
66                 return res;
67         }
68         
69         /**
70          * Returns the first matching ArtifactTypeResource for the given file
71          * extension. Returns null if no such ArtifactType can be found
72          * 
73          * The case of the extension is ignored.
74          * 
75          * This is more a DAO method
76          */
77         public ArtifactTypeResource getArtifactTypeForExtension(String extension) {
78                 SortedSet<ArtifactTypeId> allArtifactTypeIds = Repository.INSTANCE.getAllTOSCAComponentIds(ArtifactTypeId.class);
79                 ArtifactTypeResource res = null;
80                 for (ArtifactTypeId id : allArtifactTypeIds) {
81                         ArtifactTypeResource r = new ArtifactTypeResource(id);
82                         if (extension.equalsIgnoreCase(r.getAssociatedFileExtension())) {
83                                 res = r;
84                                 break;
85                         }
86                 }
87                 return res;
88         }
89         
90 }