FileRequestParameter.java 3.34 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  org.apache.commons.io.IOUtils
 *  org.apache.sling.api.request.RequestParameter
 *  org.apache.sling.api.resource.Resource
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.guide.submitutils;

import com.adobe.aemds.guide.service.GuideException;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileRequestParameter
implements RequestParameter {
    private String fileName;
    private String contentType;
    private byte[] fileBytes;
    private Logger logger = LoggerFactory.getLogger(FileRequestParameter.class);

    public FileRequestParameter(String fileName, Resource fileResource) {
        this.fileName = fileName;
        try {
            if (fileResource != null) {
                Node jcrNode = (Node)fileResource.adaptTo(Node.class);
                Node jcrContent = jcrNode.getNode("jcr:content");
                InputStream is = jcrContent.getProperty("jcr:data").getBinary().getStream();
                this.fileBytes = IOUtils.toByteArray((InputStream)is);
                is.close();
                this.contentType = jcrContent.getProperty("jcr:mimeType").getString();
            }
        }
        catch (Exception e) {
            this.logger.error("Exception in getting initialising filebytes and content type : " + e.getMessage(), (Throwable)e);
            throw new GuideException(e);
        }
    }

    public FileRequestParameter(String fileName, byte[] fileBytes, String contentType) {
        this.fileBytes = (byte[])fileBytes.clone();
        this.fileName = fileName;
        this.contentType = contentType;
    }

    public InputStream getInputStream() {
        ByteArrayInputStream is = null;
        try {
            if (this.fileBytes != null) {
                is = new ByteArrayInputStream(this.fileBytes);
            }
        }
        catch (Exception e) {
            this.logger.error("Exception in getting inputstream : " + e.getMessage(), (Throwable)e);
            throw new GuideException(e);
        }
        return is;
    }

    public String getFileName() {
        return this.fileName;
    }

    public boolean isFormField() {
        return false;
    }

    public String getName() {
        return this.fileName;
    }

    public String getContentType() {
        return this.contentType;
    }

    public byte[] get() {
        return this.fileBytes;
    }

    public String getString() {
        return new String(this.fileBytes);
    }

    public String getString(String encoding) {
        String result = null;
        try {
            result = new String(this.fileBytes, encoding);
        }
        catch (UnsupportedEncodingException e) {
            this.logger.error("Exception in encoding : " + e.getMessage(), (Throwable)e);
            throw new GuideException(e);
        }
        return result;
    }

    public long getSize() {
        byte[] bytes = this.get();
        return bytes.length;
    }
}