/
AbstractRepository.java
129 lines (116 loc) · 4.41 KB
/
AbstractRepository.java
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
/*******************************************************************************
* Copyright (c) 2012-2013 University of Stuttgart.
* 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:
* Oliver Kopp - initial API and implementation
*******************************************************************************/
package org.eclipse.winery.repository.backend;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Optional;
import javax.xml.bind.Unmarshaller;
import org.eclipse.winery.common.RepositoryFileReference;
import org.eclipse.winery.common.ids.GenericId;
import org.eclipse.winery.common.ids.definitions.TOSCAComponentId;
import org.eclipse.winery.model.tosca.Definitions;
import org.eclipse.winery.repository.Constants;
import org.eclipse.winery.repository.JAXBSupport;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.apache.tika.mime.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Provides basic implementations for {@link IRepository}
*/
public abstract class AbstractRepository implements IRepository {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractRepository.class);
/**
*
* @param ref the file reference to store the mime type for
* @return a reference to the file holding the mime type
*/
private RepositoryFileReference getMimeFileRef(RepositoryFileReference ref) {
String fileName = ref.getFileName() + Constants.SUFFIX_MIMETYPE;
return new RepositoryFileReference(ref.getParent(), fileName);
}
/**
* This is a simple implementation using the information put by
* setMimeType(RepositoryFileReference ref) or determining the mime type
* using Utils.getMimeType. If the latter is done, the mime type is
* persisted using setMimeType
*/
@Override
public String getMimeType(RepositoryFileReference ref) throws IOException {
RepositoryFileReference mimeFileRef = this.getMimeFileRef(ref);
String mimeType;
if (this.exists(mimeFileRef)) {
InputStream is = this.newInputStream(mimeFileRef);
mimeType = IOUtils.toString(is, "UTF-8");
is.close();
} else {
// repository has been manipulated manually,
// create mimetype information
MediaType mediaType;
try (InputStream is = this.newInputStream(ref);
BufferedInputStream bis = new BufferedInputStream(is)) {
mediaType = BackendUtils.getMimeType(bis, ref.getFileName());
}
if (mediaType != null) {
// successful execution
this.setMimeType(ref, mediaType);
mimeType = mediaType.toString();
} else {
AbstractRepository.LOGGER.debug("Could not determine mimetype");
mimeType = null;
}
}
return mimeType;
}
/**
* Stores the mime type of the given file reference in a separate file
*
* This method calls putContentToFile(), where the filename is appended with
* Constants.SUFFIX_MIMETYPE and a null mime type. The latter indicates that
* no "normal" file is stored.
*
* @param ref the file reference
* @param mediaType the mimeType
*/
protected void setMimeType(RepositoryFileReference ref, MediaType mediaType) throws IOException {
RepositoryFileReference mimeFileRef = this.getMimeFileRef(ref);
this.putContentToFile(mimeFileRef, mediaType.toString(), null);
}
@Override
public Date getConfigurationLastUpdate(GenericId id) {
RepositoryFileReference ref = BackendUtils.getRefOfConfiguration(id);
return this.getLastUpdate(ref);
}
@Override
public Configuration getConfiguration(GenericId id) {
RepositoryFileReference ref = BackendUtils.getRefOfConfiguration(id);
return this.getConfiguration(ref);
}
@Override
public Optional<Definitions> getDefinitions(TOSCAComponentId id) {
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(id);
if (!exists(ref)) {
return Optional.empty();
}
try {
InputStream is = Repository.INSTANCE.newInputStream(ref);
Unmarshaller u = JAXBSupport.createUnmarshaller();
return Optional.of((Definitions) u.unmarshal(is));
} catch (Exception e) {
LOGGER.error("Could not read content from file " + ref, e);
throw new IllegalStateException(e);
}
}
}