use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class AbstractRepository method getMimeType.
/**
* 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;
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class AbstractRepository method setMimeType.
/**
* Stores the mime type of the given file reference in a separate file
* <p>
* 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);
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class AbstractRepository method getDefinitions.
@Override
public Definitions getDefinitions(DefinitionsChildId id) {
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(id);
if (!exists(ref)) {
return BackendUtils.createWrapperDefinitionsAndInitialEmptyElement(this, id);
}
try {
InputStream is = RepositoryFactory.getRepository().newInputStream(ref);
Unmarshaller u = JAXBSupport.createUnmarshaller();
return (Definitions) u.unmarshal(is);
} catch (Exception e) {
LOGGER.error("Could not read content from file " + ref, e);
throw new IllegalStateException(e);
}
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class WriterUtils method storeTypes.
public static void storeTypes(Path path, String namespace, String id) {
LOGGER.debug("Store type: {}", id);
try {
MediaType mediaType = MediaTypes.MEDIATYPE_XSD;
TImport.Builder builder = new TImport.Builder(Namespaces.XML_NS);
builder.setNamespace(namespace);
builder.setLocation(id + ".xsd");
GenericImportId rid = new XSDImportId(namespace, id, false);
TDefinitions definitions = BackendUtils.createWrapperDefinitions(rid);
definitions.getImport().add(builder.build());
CsarImporter.storeDefinitions(rid, definitions);
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(rid);
List<File> files = Files.list(path).filter(Files::isRegularFile).map(Path::toFile).collect(Collectors.toList());
for (File file : files) {
BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file));
RepositoryFileReference fileRef = new RepositoryFileReference(ref.getParent(), file.getName());
RepositoryFactory.getRepository().putContentToFile(fileRef, stream, mediaType);
}
} catch (IllegalArgumentException | IOException e) {
throw new IllegalStateException(e);
}
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class GenericImportResource method getFile.
@GET
@Path("{filename}")
public Response getFile(@PathParam("filename") @NonNull final String encodedFileName) {
Objects.requireNonNull(encodedFileName);
final Optional<TImport> theImport = ImportUtils.getTheImport((GenericImportId) id);
if (!theImport.isPresent()) {
return Response.status(Status.NOT_FOUND).build();
}
@Nullable final String location = theImport.get().getLocation();
@NonNull String fileName = Util.URLdecode(encodedFileName);
if (!fileName.equals(location)) {
LOGGER.debug("Filename mismatch %s vs %s", fileName, location);
return Response.status(Status.NOT_FOUND).build();
}
RepositoryFileReference ref = new RepositoryFileReference(this.id, location);
return RestUtils.returnRepoPath(ref, null);
}
Aggregations