Search in sources :

Example 51 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.

the class FilesResource method onPost.

/**
 * Handles the upload of a <em>single</em> file. Adds the given file to the current artifact template.
 * <p>
 * If the file already exists, is it <em>overridden</em>
 *
 * @return JSON with data required by JQuery-File-Upload (see https://github.com/blueimp/jQuery-File-Upload/wiki/Setup)
 */
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response onPost(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("file") FormDataBodyPart body, @Context UriInfo uriInfo) {
    // existence check not required as instantiation of the resource ensures that the object only exists if the resource exists
    FilesResource.LOGGER.debug("Beginning with file upload");
    String fileName = fileDetail.getFileName();
    if (StringUtils.isEmpty(fileName)) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    RepositoryFileReference ref = this.fileName2fileRef(fileName, false);
    // TODO: instead of fixing the media type, we could overwrite the browser's mediatype by using some user configuration
    BufferedInputStream bis = new BufferedInputStream(uploadedInputStream);
    org.apache.tika.mime.MediaType mediaType = BackendUtils.getFixedMimeType(bis, fileName, org.apache.tika.mime.MediaType.parse(body.getMediaType().toString()));
    Response response = RestUtils.putContentToFile(ref, bis, mediaType);
    if (response.getStatus() == Status.INTERNAL_SERVER_ERROR.getStatusCode()) {
        return response;
    }
    try {
        BackendUtils.synchronizeReferences((ArtifactTemplateId) fileDir.getParent());
    } catch (IOException e) {
        throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
    }
    String URL = RestUtils.getAbsoluteURL(this.fileDir) + Util.URLencode(fileName);
    return Response.created(RestUtils.createURI(URL)).entity(this.getAllFileMetas()).build();
}
Also used : Response(javax.ws.rs.core.Response) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException)

Aggregations

RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)51 IOException (java.io.IOException)15 InputStream (java.io.InputStream)8 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)8 Definitions (org.eclipse.winery.model.tosca.Definitions)8 BufferedInputStream (java.io.BufferedInputStream)6 ArtifactTemplateSourceDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateSourceDirectoryId)6 Test (org.junit.Test)6 Path (java.nio.file.Path)5 JAXBException (javax.xml.bind.JAXBException)5 MediaType (org.apache.tika.mime.MediaType)5 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)5 ZipInputStream (java.util.zip.ZipInputStream)4 Unmarshaller (javax.xml.bind.Unmarshaller)4 QName (javax.xml.namespace.QName)4 XmlId (org.eclipse.winery.common.ids.XmlId)4 GenericImportId (org.eclipse.winery.common.ids.definitions.imports.GenericImportId)4 XSDImportId (org.eclipse.winery.common.ids.definitions.imports.XSDImportId)4 PlanId (org.eclipse.winery.common.ids.elements.PlanId)4 PlansId (org.eclipse.winery.common.ids.elements.PlansId)4