aboutsummaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.model.csar.toscametafile/src/main/java/org/eclipse/winery/model/csar/toscametafile/TOSCAMetaFile.java
blob: e795631d85183357edd9f4bff6adbbf254a8a910 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*******************************************************************************
 * Copyright (c) 2013 Rene Trefft, Oliver Kopp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *    Rene Trefft - initial API and implementation and/or initial documentation
 *    Oliver Kopp - support for getMimeType(name)
 *******************************************************************************/
package org.eclipse.winery.model.csar.toscametafile;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.springsource.util.parser.manifest.ManifestContents;

/**
 * Provides structured access to the content of a TOSCA meta file.<br />
 * <br />
 * Copyright 2013 IAAS University of Stuttgart <br />
 * <br />
 * 
 * @author Rene Trefft - rene.trefft@developers.opentosca.org
 * 
 */
public class TOSCAMetaFile implements Serializable {
	
	private static final long serialVersionUID = 5636441655503533546L;
	
	Map<String, String> block0 = new HashMap<String, String>();
	List<Map<String, String>> fileBlocks = new ArrayList<Map<String, String>>();
	
	
	/**
	 * Creates a new TOSCA meta file.
	 * 
	 * @param manifestContent to create from
	 */
	public TOSCAMetaFile(ManifestContents manifestContent) {
		this.block0 = manifestContent.getMainAttributes();
		for (String name : manifestContent.getSectionNames()) {
			Map<String, String> fileBlock = new HashMap<String, String>();
			fileBlock.put(TOSCAMetaFileAttributes.NAME, name);
			fileBlock.putAll(manifestContent.getAttributesForSection(name));
			this.fileBlocks.add(fileBlock);
		}
	}
	
	/**
	 * 
	 * @return Value of attribute <code>CSAR-Version</code> in block 0.
	 */
	public String getCSARVersion() {
		return this.block0.get(TOSCAMetaFileAttributes.CSAR_VERSION);
	}
	
	/**
	 * 
	 * @return Value of attribute <code>TOSCA-Meta-Version</code> in block 0.
	 */
	public String getTOSCAMetaVersion() {
		return this.block0.get(TOSCAMetaFileAttributes.TOSCA_META_VERSION);
	}
	
	/**
	 * 
	 * @return Value of attribute <code>Created-By</code> in block 0.
	 */
	public String getCreatedBy() {
		return this.block0.get(TOSCAMetaFileAttributes.CREATED_BY);
	}
	
	/**
	 * 
	 * @return Value of attribute <code>Entry-Definitions</code> in block 0
	 *         (contains relative path to the root TOSCA file in the CSAR). If
	 *         attribute is not specified <code>null</code>.
	 */
	public String getEntryDefinitions() {
		return this.block0.get(TOSCAMetaFileAttributes.ENTRY_DEFINITIONS);
	}
	
	/**
	 * 
	 * @return Value of attribute <code>Description</code> in block 0 (contains
	 *         description of CSAR). If attribute is not specified
	 *         <code>null</code>.
	 */
	public String getDescription() {
		return this.block0.get(TOSCAMetaFileAttributes.DESCRIPTION);
	}
	
	/**
	 * 
	 * @return Value of attribute <code>Topology</code> in block 0 (contains
	 *         relative path to topology picture in the CSAR). If attribute is
	 *         not specified <code>null</code>.
	 */
	public String getTopology() {
		return this.block0.get(TOSCAMetaFileAttributes.TOPOLOGY);
	}
	
	/**
	 * 
	 * @return Block 0 (contains meta data about the CSAR itself).
	 */
	public Map<String, String> getBlock0() {
		return this.block0;
	}
	
	/**
	 * 
	 * @return File blocks (block 1 to last block; contains meta data of files
	 *         in the CSAR). Every block is a element
	 *         <code>Map&lt;String, String&gt;</code> in the returned
	 *         <code>List</code>.
	 */
	public List<Map<String, String>> getFileBlocks() {
		return this.fileBlocks;
	}
	
	/**
	 * Returns the mime type for the given name
	 * 
	 * @param name a reference to a file
	 * @return the mime type associated with the given name, null if no mime
	 *         type was found
	 */
	public String getMimeType(String name) {
		if (name == null) {
			throw new IllegalArgumentException("name must not be null");
		}
		for (Map<String, String> map : this.getFileBlocks()) {
			String storedName = map.get("Name");
			if (name.equals(storedName)) {
				// first hit, check whether content-type is stored
				String contentType = map.get("Content-Type");
				if (contentType != null) {
					// hit - return the found content type
					return contentType;
				}
			}
		}
		// nothing found
		return null;
	}
	
}