What could be causing the code to function correctly locally but only partially on the staging environment?

44 Views Asked by At

I am using tika and poi dependencies to detect file mime types and storing those files accordingly.


import java.io.File;
import java.io.IOException;

import org.apache.tika.Tika;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class FileTypeUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(FileTypeUtils.class);
    
    private FileTypeUtils() {
    }

    public static String getFileType(File inputFile) throws Exception {

        final Tika tika = new Tika();
        try {
            return tika.detect(inputFile);
        } catch (IOException e) {
            LOGGER.error("*** getFileTypeViaTika2 - Error message: " + e.getMessage());
            LOGGER.error("*** getFileTypeViaTika2 - Error trace"+e);
            throw new Exception("File type unknown");
        }
        
    }
}

this is the method caller

    public void saveMediaFile(String url, WhatsAppMessageContentTypeValidation whatsAppMessageContentTypeValidation) {
        String fileName = UUID.randomUUID().toString();
        try {
            File file = new File(Pl4WhatsAppConstants.mediaFilePath + fileName.replace(" ", "-"));
            if (url != null && url.contains("https")) {
                URL fileUrl = new URL(url.replace(" ", "-"));
                SSLContext sslContext = SSLContext.getInstance("SSL");

                // set up a TrustManager that trusts everything
                sslContext.init(null, new TrustManager[] { new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        LOGGER.info("getAcceptedIssuers =============");
                        return null;
                    }

                    public void checkClientTrusted(X509Certificate[] certs, String authType) {
                        LOGGER.info("checkClientTrusted =============");
                    }

                    public void checkServerTrusted(X509Certificate[] certs, String authType) {
                        LOGGER.info("checkServerTrusted =============");
                    }
                } }, new SecureRandom());
                // Apache HttpClient version >4.2 should use BasicClientConnectionManager
                HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
                HttpsURLConnection connection = (HttpsURLConnection) fileUrl.openConnection();
                FileUtils.copyURLToFile(connection.getURL(), file, 30000, 30000);
            } else {
                URL fileUrl = new URL(url.replace(" ", "-"));
                FileUtils.copyURLToFile(fileUrl, file, 30000, 30000);
            }
            String contentTypeString = FileTypeUtils.getFileType(file);
            LOGGER.info("Content Mime type {}", contentTypeString);
            whatsAppMessageContentTypeValidation.setVendorMimeType(contentTypeString);
            whatsAppMessageContentTypeValidation.setFileName(fileName);
            whatsAppMessageContentTypeValidation.setMediaType(getMediaType(contentTypeString));
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
            whatsAppMessageContentTypeValidation
                    .setValidationError(Pl4WhatsAppRequestValidations.CANNOT_RETRIEVE_MEDIA_TO_SEND);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            whatsAppMessageContentTypeValidation
                    .setValidationError(Pl4WhatsAppRequestValidations.CANNOT_DETERMINE_MEDIA_TYPE);
        }
    }

    private Pl4WhatsAppMediaType getMediaType(String fileType) {
        if (fileType.contains("image")) {
            return Pl4WhatsAppMediaType.IMAGE;
        } else if (fileType.contains("video")) {
            return Pl4WhatsAppMediaType.VIDEO;
        } else {
            return Pl4WhatsAppMediaType.DOCUMENT;
        }
    }

these are my maven dependencies

<dependency>
            <groupId>org.apache.tika</groupId>
            <artifactId>tika-core</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tika</groupId>
            <artifactId>tika-parsers</artifactId>
            <version>1.28.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.2</version>
        </dependency>

locally, all the expected file types(doc,docx, ppt, pptx, pdf, jpg, mp3, mp4, txt..) are going through. but on the staging environment docx, pptx, xlsx are not going through.

on stage docx, pptx and xlsx are detected as "application/x-tika-ooxml" but locally they are detected as "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, respectively.

I tried copying the jars from stage and ran them locally via cmd, and send the request via postman the problem was replicated. then I used the jars generated locally and I got the expected results (all the files going through).

So I would like to know about the causes of such behavor.

0

There are 0 best solutions below