Merge "Inherit from oparent"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / backend / AbstractRepository.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.backend;
13
14 import java.io.BufferedInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.Date;
18
19 import javax.ws.rs.core.MediaType;
20
21 import org.apache.commons.configuration.Configuration;
22 import org.apache.commons.io.IOUtils;
23 import org.eclipse.winery.common.RepositoryFileReference;
24 import org.eclipse.winery.common.ids.GenericId;
25 import org.eclipse.winery.repository.Constants;
26 import org.eclipse.winery.repository.Utils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Provides basic implementations for {@link IRepository}
32  */
33 public abstract class AbstractRepository implements IRepository {
34         
35         private static final Logger logger = LoggerFactory.getLogger(AbstractRepository.class);
36         
37         
38         /**
39          * 
40          * @param ref the file reference to store the mime type for
41          * @return a reference to the file holding the mime type
42          */
43         private RepositoryFileReference getMimeFileRef(RepositoryFileReference ref) {
44                 String fileName = ref.getFileName() + Constants.SUFFIX_MIMETYPE;
45                 RepositoryFileReference mimeFileRef = new RepositoryFileReference(ref.getParent(), fileName);
46                 return mimeFileRef;
47         }
48         
49         /**
50          * {@inheritDoc}
51          * 
52          * This is a simple implementation using the information put by
53          * setMimeType(RepositoryFileReference ref) or determining the mime type
54          * using Utils.getMimeType. If the latter is done, the mime type is
55          * persisted using setMimeType
56          */
57         @Override
58         public String getMimeType(RepositoryFileReference ref) throws IOException {
59                 RepositoryFileReference mimeFileRef = this.getMimeFileRef(ref);
60                 String mimeType;
61                 if (this.exists(mimeFileRef)) {
62                         InputStream is = this.newInputStream(mimeFileRef);
63                         mimeType = IOUtils.toString(is, "UTF-8");
64                         is.close();
65                 } else {
66                         // repository has been manipulated manually,
67                         // create mimetype information
68                         mimeType = null;
69                         try (InputStream is = this.newInputStream(ref);
70                                         BufferedInputStream bis = new BufferedInputStream(is);) {
71                                 mimeType = Utils.getMimeType(bis, ref.getFileName());
72                         }
73                         if (mimeType != null) {
74                                 // successful execution
75                                 this.setMimeType(ref, MediaType.valueOf(mimeType));
76                         } else {
77                                 AbstractRepository.logger.debug("Could not determine mimetype");
78                         }
79                 }
80                 return mimeType;
81         }
82         
83         /**
84          * Stores the mime type of the given file reference in a separate file
85          * 
86          * This method calls putContentToFile(), where the filename is appended with
87          * Constants.SUFFIX_MIMETYPE and a null mime type. The latter indicates that
88          * no "normal" file is stored.
89          * 
90          * @param ref the file reference
91          * @param mediaType the mimeType
92          */
93         protected void setMimeType(RepositoryFileReference ref, MediaType mediaType) throws IOException {
94                 RepositoryFileReference mimeFileRef = this.getMimeFileRef(ref);
95                 this.putContentToFile(mimeFileRef, mediaType.toString(), null);
96         }
97         
98         @Override
99         public Date getConfigurationLastUpdate(GenericId id) {
100                 RepositoryFileReference ref = BackendUtils.getRefOfConfiguration(id);
101                 return this.getLastUpdate(ref);
102         }
103         
104         @Override
105         public Configuration getConfiguration(GenericId id) {
106                 RepositoryFileReference ref = BackendUtils.getRefOfConfiguration(id);
107                 return this.getConfiguration(ref);
108         }
109         
110 }