Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.model.csar.toscametafile / src / main / java / org / eclipse / winery / model / csar / toscametafile / TOSCAMetaFile.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Rene Trefft, Oliver Kopp.
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  *    Rene Trefft - initial API and implementation and/or initial documentation
11  *    Oliver Kopp - support for getMimeType(name)
12  *******************************************************************************/
13 package org.eclipse.winery.model.csar.toscametafile;
14
15 import java.io.Serializable;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20
21 import com.springsource.util.parser.manifest.ManifestContents;
22
23 /**
24  * Provides structured access to the content of a TOSCA meta file.<br />
25  * <br />
26  * Copyright 2013 IAAS University of Stuttgart <br />
27  * <br />
28  * 
29  * @author Rene Trefft - rene.trefft@developers.opentosca.org
30  * 
31  */
32 public class TOSCAMetaFile implements Serializable {
33         
34         private static final long serialVersionUID = 5636441655503533546L;
35         
36         Map<String, String> block0 = new HashMap<String, String>();
37         List<Map<String, String>> fileBlocks = new ArrayList<Map<String, String>>();
38         
39         
40         /**
41          * Creates a new TOSCA meta file.
42          * 
43          * @param manifestContent to create from
44          */
45         public TOSCAMetaFile(ManifestContents manifestContent) {
46                 this.block0 = manifestContent.getMainAttributes();
47                 for (String name : manifestContent.getSectionNames()) {
48                         Map<String, String> fileBlock = new HashMap<String, String>();
49                         fileBlock.put(TOSCAMetaFileAttributes.NAME, name);
50                         fileBlock.putAll(manifestContent.getAttributesForSection(name));
51                         this.fileBlocks.add(fileBlock);
52                 }
53         }
54         
55         /**
56          * 
57          * @return Value of attribute <code>CSAR-Version</code> in block 0.
58          */
59         public String getCSARVersion() {
60                 return this.block0.get(TOSCAMetaFileAttributes.CSAR_VERSION);
61         }
62         
63         /**
64          * 
65          * @return Value of attribute <code>TOSCA-Meta-Version</code> in block 0.
66          */
67         public String getTOSCAMetaVersion() {
68                 return this.block0.get(TOSCAMetaFileAttributes.TOSCA_META_VERSION);
69         }
70         
71         /**
72          * 
73          * @return Value of attribute <code>Created-By</code> in block 0.
74          */
75         public String getCreatedBy() {
76                 return this.block0.get(TOSCAMetaFileAttributes.CREATED_BY);
77         }
78         
79         /**
80          * 
81          * @return Value of attribute <code>Entry-Definitions</code> in block 0
82          *         (contains relative path to the root TOSCA file in the CSAR). If
83          *         attribute is not specified <code>null</code>.
84          */
85         public String getEntryDefinitions() {
86                 return this.block0.get(TOSCAMetaFileAttributes.ENTRY_DEFINITIONS);
87         }
88         
89         /**
90          * 
91          * @return Value of attribute <code>Description</code> in block 0 (contains
92          *         description of CSAR). If attribute is not specified
93          *         <code>null</code>.
94          */
95         public String getDescription() {
96                 return this.block0.get(TOSCAMetaFileAttributes.DESCRIPTION);
97         }
98         
99         /**
100          * 
101          * @return Value of attribute <code>Topology</code> in block 0 (contains
102          *         relative path to topology picture in the CSAR). If attribute is
103          *         not specified <code>null</code>.
104          */
105         public String getTopology() {
106                 return this.block0.get(TOSCAMetaFileAttributes.TOPOLOGY);
107         }
108         
109         /**
110          * 
111          * @return Block 0 (contains meta data about the CSAR itself).
112          */
113         public Map<String, String> getBlock0() {
114                 return this.block0;
115         }
116         
117         /**
118          * 
119          * @return File blocks (block 1 to last block; contains meta data of files
120          *         in the CSAR). Every block is a element
121          *         <code>Map&lt;String, String&gt;</code> in the returned
122          *         <code>List</code>.
123          */
124         public List<Map<String, String>> getFileBlocks() {
125                 return this.fileBlocks;
126         }
127         
128         /**
129          * Returns the mime type for the given name
130          * 
131          * @param name a reference to a file
132          * @return the mime type associated with the given name, null if no mime
133          *         type was found
134          */
135         public String getMimeType(String name) {
136                 if (name == null) {
137                         throw new IllegalArgumentException("name must not be null");
138                 }
139                 for (Map<String, String> map : this.getFileBlocks()) {
140                         String storedName = map.get("Name");
141                         if (name.equals(storedName)) {
142                                 // first hit, check whether content-type is stored
143                                 String contentType = map.get("Content-Type");
144                                 if (contentType != null) {
145                                         // hit - return the found content type
146                                         return contentType;
147                                 }
148                         }
149                 }
150                 // nothing found
151                 return null;
152         }
153         
154 }