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
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.repository.backend;
14 import java.io.BufferedInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.Date;
19 import javax.ws.rs.core.MediaType;
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;
31 * Provides basic implementations for {@link IRepository}
33 public abstract class AbstractRepository implements IRepository {
35 private static final Logger logger = LoggerFactory.getLogger(AbstractRepository.class);
40 * @param ref the file reference to store the mime type for
41 * @return a reference to the file holding the mime type
43 private RepositoryFileReference getMimeFileRef(RepositoryFileReference ref) {
44 String fileName = ref.getFileName() + Constants.SUFFIX_MIMETYPE;
45 RepositoryFileReference mimeFileRef = new RepositoryFileReference(ref.getParent(), fileName);
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
58 public String getMimeType(RepositoryFileReference ref) throws IOException {
59 RepositoryFileReference mimeFileRef = this.getMimeFileRef(ref);
61 if (this.exists(mimeFileRef)) {
62 InputStream is = this.newInputStream(mimeFileRef);
63 mimeType = IOUtils.toString(is, "UTF-8");
66 // repository has been manipulated manually,
67 // create mimetype information
69 try (InputStream is = this.newInputStream(ref);
70 BufferedInputStream bis = new BufferedInputStream(is);) {
71 mimeType = Utils.getMimeType(bis, ref.getFileName());
73 if (mimeType != null) {
74 // successful execution
75 this.setMimeType(ref, MediaType.valueOf(mimeType));
77 AbstractRepository.logger.debug("Could not determine mimetype");
84 * Stores the mime type of the given file reference in a separate file
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.
90 * @param ref the file reference
91 * @param mediaType the mimeType
93 protected void setMimeType(RepositoryFileReference ref, MediaType mediaType) throws IOException {
94 RepositoryFileReference mimeFileRef = this.getMimeFileRef(ref);
95 this.putContentToFile(mimeFileRef, mediaType.toString(), null);
99 public Date getConfigurationLastUpdate(GenericId id) {
100 RepositoryFileReference ref = BackendUtils.getRefOfConfiguration(id);
101 return this.getLastUpdate(ref);
105 public Configuration getConfiguration(GenericId id) {
106 RepositoryFileReference ref = BackendUtils.getRefOfConfiguration(id);
107 return this.getConfiguration(ref);