Search in sources :

Example 1 with ResourceNotFoundException

use of com.plivo.api.exceptions.ResourceNotFoundException in project plivo-java by plivo.

the class MediaUploader method getFilesForFilenames.

private RequestBody getFilesForFilenames(String[] fileNames) throws ResourceNotFoundException {
    Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
    for (int i = 0; i < fileNames.length; i++) {
        File tempFile = new File(fileNames[i]);
        boolean exists = tempFile.exists();
        if (!exists)
            throw new ResourceNotFoundException("File missing " + fileNames[i]);
        try {
            System.out.println(tempFile);
            System.out.println(tempFile.toPath());
            // handle for java 8
            String content_type = "";
            if (Files.probeContentType(tempFile.toPath()) != null) {
                content_type = Files.probeContentType(tempFile.toPath());
            } else {
                Path source = Paths.get(fileNames[i]);
                MimetypesFileTypeMap m = new MimetypesFileTypeMap(source.toString());
                content_type = m.getContentType(tempFile);
            }
            builder.addFormDataPart("file", fileNames[i], RequestBody.create(MediaType.parse(content_type), tempFile));
        } catch (IOException e) {
            throw new ResourceNotFoundException("Unable to read file " + fileNames[i]);
        }
    }
    return builder.build();
}
Also used : Path(java.nio.file.Path) Builder(okhttp3.MultipartBody.Builder) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) MultipartBody(okhttp3.MultipartBody) Builder(okhttp3.MultipartBody.Builder) IOException(java.io.IOException) ResourceNotFoundException(com.plivo.api.exceptions.ResourceNotFoundException) File(java.io.File)

Example 2 with ResourceNotFoundException

use of com.plivo.api.exceptions.ResourceNotFoundException in project plivo-java by plivo.

the class ComplianceDocumentCreator method setFile.

public void setFile(String file) throws ResourceNotFoundException {
    this.file = file;
    File tempFile = new File(file);
    boolean exists = tempFile.exists();
    if (!exists)
        throw new ResourceNotFoundException("File missing " + file);
    try {
        System.out.println(tempFile);
        System.out.println(tempFile.toPath());
        // handle for java 8
        String content_type = "";
        if (Files.probeContentType(tempFile.toPath()) != null) {
            content_type = Files.probeContentType(tempFile.toPath());
        } else {
            Path source = Paths.get(file);
            MimetypesFileTypeMap m = new MimetypesFileTypeMap(source.toString());
            content_type = m.getContentType(tempFile);
        }
        this.body.addFormDataPart("file", file, RequestBody.create(MediaType.parse(content_type), tempFile));
    } catch (IOException e) {
        throw new ResourceNotFoundException("Unable to read file " + file);
    }
}
Also used : Path(java.nio.file.Path) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) IOException(java.io.IOException) ResourceNotFoundException(com.plivo.api.exceptions.ResourceNotFoundException) File(java.io.File)

Example 3 with ResourceNotFoundException

use of com.plivo.api.exceptions.ResourceNotFoundException in project plivo-java by plivo.

the class ComplianceDocumentUpdater method setFile.

public ComplianceDocumentUpdater setFile(String file) throws ResourceNotFoundException {
    this.file = file;
    File tempFile = new File(file);
    boolean exists = tempFile.exists();
    if (!exists)
        throw new ResourceNotFoundException("File missing " + file);
    try {
        System.out.println(tempFile);
        System.out.println(tempFile.toPath());
        // handle for java 8
        String content_type = "";
        if (Files.probeContentType(tempFile.toPath()) != null) {
            content_type = Files.probeContentType(tempFile.toPath());
        } else {
            Path source = Paths.get(file);
            MimetypesFileTypeMap m = new MimetypesFileTypeMap(source.toString());
            content_type = m.getContentType(tempFile);
        }
        this.body.addFormDataPart("file", file, RequestBody.create(MediaType.parse(content_type), tempFile));
    } catch (IOException e) {
        throw new ResourceNotFoundException("Unable to read file " + file);
    }
    return this;
}
Also used : Path(java.nio.file.Path) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) IOException(java.io.IOException) ResourceNotFoundException(com.plivo.api.exceptions.ResourceNotFoundException) File(java.io.File)

Aggregations

ResourceNotFoundException (com.plivo.api.exceptions.ResourceNotFoundException)3 File (java.io.File)3 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)3 MultipartBody (okhttp3.MultipartBody)1 Builder (okhttp3.MultipartBody.Builder)1