Search in sources :

Example 51 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project sigla-main by consiglionazionaledellericerche.

the class FatturaPassivaElettronicaService method notificaFatturaAttivaAvvenutaTrasmissioneNonRecapitata.

private void notificaFatturaAttivaAvvenutaTrasmissioneNonRecapitata(Message message) throws ComponentException {
    logger.info("Fatture Elettroniche: Attive: Inizio Avvenuta Trasmissione con impossibilità di recapito.");
    try {
        BodyPart bodyPartZip = estraiBodyPartZipNotificaFatturaAttiva(message);
        if (bodyPartZip != null) {
            logger.info("Fatture Elettroniche: Attive: Estratto Body Part.");
            ZipInputStream zis = new ZipInputStream(bodyPartZip.getInputStream());
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                System.out.println("entry: " + entry.getName() + ", " + entry.getSize());
                if (entry.getName().toLowerCase().endsWith("xml")) {
                    String contentType = new MimetypesFileTypeMap().getContentType(entry.getName());
                    DataHandler dataHandler = createDataHandler(zis, contentType);
                    trasmissioneFattureService.notificaFatturaAttivaAvvenutaTrasmissioneNonRecapitata(userContext, entry.getName(), dataHandler);
                    break;
                }
            }
        }
    } catch (Exception e) {
        throw new ComponentException(e);
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) ZipInputStream(java.util.zip.ZipInputStream) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) ZipEntry(java.util.zip.ZipEntry) ComponentException(it.cnr.jada.comp.ComponentException) DataHandler(javax.activation.DataHandler) RemoteException(java.rmi.RemoteException) EncryptionException(it.cnr.contab.util.StringEncrypter.EncryptionException) XmlMappingException(org.springframework.oxm.XmlMappingException) DetailedRuntimeException(it.cnr.jada.DetailedRuntimeException) EmailException(org.apache.commons.mail.EmailException) ComponentException(it.cnr.jada.comp.ComponentException)

Example 52 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project idempiere by idempiere.

the class MultiFileDownloadDialog method show.

private void show() {
    Vlayout layout = new Vlayout();
    layout.setStyle("padding-top: 10px; padding-bottom: 10px;");
    appendChild(layout);
    MimetypesFileTypeMap mimeMap = new MimetypesFileTypeMap();
    for (File file : files) {
        try {
            AMedia media = new AMedia(file, mimeMap.getContentType(file), null);
            DynamicMediaLink link = new DynamicMediaLink();
            layout.appendChild(link);
            link.setMedia(media);
            link.setLabel(media.getName());
            link.setStyle("margin: 5px;");
        } catch (FileNotFoundException e) {
        }
    }
    this.setClosable(true);
    this.setSizable(true);
    this.setMaximizable(true);
    this.setBorder("normal");
    this.setContentStyle("min-height: 200px; min-width:300px");
    this.setPosition("center");
    if (this.getTitle() == null || this.getTitle().length() == 0) {
        this.setTitle("Downloads");
    }
    this.doHighlighted();
}
Also used : Vlayout(org.zkoss.zul.Vlayout) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) AMedia(org.zkoss.util.media.AMedia) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) DynamicMediaLink(org.adempiere.webui.component.DynamicMediaLink)

Example 53 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project gravitee-api-management by gravitee-io.

the class ThemeServiceImpl method getImage.

private String getImage(String filename) {
    String filepath = themesPath + "/" + filename;
    try {
        byte[] image = Files.readAllBytes(new File(filepath).toPath());
        MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
        return "data:" + fileTypeMap.getContentType(filename) + ";base64," + Base64.getEncoder().encodeToString(image);
    } catch (IOException ex) {
        final String error = "Error while trying to load image from: " + filepath;
        LOGGER.error(error, ex);
        return null;
    }
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) IOException(java.io.IOException) File(java.io.File)

Example 54 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project studio by craftercms.

the class BinaryView method renderMergedOutputModel.

@Override
@SuppressWarnings("unchecked")
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    OutputStream out = response.getOutputStream();
    Map<String, Object> responseModelMap = (Map<String, Object>) model.get(RestScriptsController.DEFAULT_RESPONSE_BODY_MODEL_ATTR_NAME);
    if (responseModelMap != null) {
        InputStream contentStream = (InputStream) responseModelMap.get(DEFAULT_CONTENT_STREAM_MODEL_ATTR_NAME);
        String contentPath = (String) responseModelMap.get(DEFAULT_CONTENT_PATH_MODEL_ATTR_NAME);
        MimetypesFileTypeMap mimetypesFileTypeMap = new MimetypesFileTypeMap();
        String contentType = mimetypesFileTypeMap.getContentType(contentPath);
        response.setContentType(contentType);
        if (contentStream != null) {
            IOUtils.write(IOUtils.toByteArray(contentStream), out);
        }
        out.flush();
        IOUtils.closeQuietly(contentStream);
        IOUtils.closeQuietly(out);
    }
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) Map(java.util.Map)

Example 55 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project studio by craftercms.

the class AwsUtils method uploadStream.

public static void uploadStream(String inputBucket, String inputKey, AmazonS3 s3Client, int partSize, String filename, InputStream content) throws AwsException {
    List<PartETag> etags = new LinkedList<>();
    InitiateMultipartUploadResult initResult = null;
    try {
        int partNumber = 1;
        long totalBytes = 0;
        MimetypesFileTypeMap mimeMap = new MimetypesFileTypeMap();
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentType(mimeMap.getContentType(filename));
        InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(inputBucket, inputKey, meta);
        initResult = s3Client.initiateMultipartUpload(initRequest);
        byte[] buffer = new byte[partSize];
        int read;
        logger.debug("Starting upload for file '{}'", filename);
        while (0 < (read = IOUtils.read(content, buffer))) {
            totalBytes += read;
            if (logger.isTraceEnabled()) {
                logger.trace("Uploading part {} with size {} - total: {}", partNumber, read, totalBytes);
            }
            ByteArrayInputStream bais = new ByteArrayInputStream(buffer, 0, read);
            UploadPartRequest uploadRequest = new UploadPartRequest().withUploadId(initResult.getUploadId()).withBucketName(inputBucket).withKey(inputKey).withInputStream(bais).withPartNumber(partNumber).withPartSize(read).withLastPart(read < partSize);
            etags.add(s3Client.uploadPart(uploadRequest).getPartETag());
            partNumber++;
        }
        if (totalBytes == 0) {
            // If the file is empty, use the simple upload instead of the multipart
            s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(inputBucket, inputKey, initResult.getUploadId()));
            s3Client.putObject(inputBucket, inputKey, new ByteArrayInputStream(new byte[0]), meta);
        } else {
            CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest(inputBucket, inputKey, initResult.getUploadId(), etags);
            s3Client.completeMultipartUpload(completeRequest);
        }
        logger.debug("Upload completed for file '{}'", filename);
    } catch (Exception e) {
        if (initResult != null) {
            s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(inputBucket, inputKey, initResult.getUploadId()));
        }
        throw new AwsException("Upload of file '" + filename + "' failed", e);
    }
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) LinkedList(java.util.LinkedList) AwsException(org.craftercms.studio.api.v1.exception.AwsException) ByteArrayInputStream(java.io.ByteArrayInputStream) AwsException(org.craftercms.studio.api.v1.exception.AwsException)

Aggregations

MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)63 File (java.io.File)19 IOException (java.io.IOException)15 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 DefaultStreamedContent (org.primefaces.model.DefaultStreamedContent)6 URL (java.net.URL)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 FileNameMap (java.net.FileNameMap)3 HttpURLConnection (java.net.HttpURLConnection)3 MimeType (javax.activation.MimeType)3 MimeTypeParseException (javax.activation.MimeTypeParseException)3 Response (javax.ws.rs.core.Response)3 Before (org.junit.Before)3 Test (org.junit.Test)3 Failure (org.junit.runner.notification.Failure)3 InvalidKeyException (java.security.InvalidKeyException)2