
com.adobe.aemds.guide.submitutils.FileRequestParameter Maven / Gradle / Ivy
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2014 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
package com.adobe.aemds.guide.submitutils;
import com.adobe.aemds.guide.service.GuideException;
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;
import javax.jcr.Node;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
/**
* Created with IntelliJ IDEA.
* User: syr
* Date: 1/20/14
* Time: 12:51 PM
* To change this template use File | Settings | File Templates.
*/
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 = fileResource.adaptTo(Node.class);
Node jcrContent = jcrNode.getNode("jcr:content");
InputStream is = jcrContent.getProperty("jcr:data").getBinary().getStream();
this.fileBytes = IOUtils.toByteArray(is);
is.close();
this.contentType = jcrContent.getProperty("jcr:mimeType").getString();
}
} catch (Exception e) {
logger.error("Exception in getting initialising filebytes and content type : " + e.getMessage(), e);
throw new GuideException(e);
}
}
public FileRequestParameter(String fileName, byte[] fileBytes, String contentType) {
this.fileBytes = fileBytes.clone();
this.fileName = fileName;
this.contentType = contentType;
}
public InputStream getInputStream() {
InputStream is = null;
try {
if (fileBytes != null) {
is = new ByteArrayInputStream(fileBytes);
}
} catch (Exception e) {
logger.error("Exception in getting inputstream : " + e.getMessage(), e);
throw new GuideException(e);
}
return is;
}
public String getFileName() {
return fileName;
}
public boolean isFormField() {
return false;
}
// included in the versions>2.7.0 of org.apache.sling.api.*
public String getName(){
return fileName;
}
public String getContentType() {
return contentType;
}
public byte[] get() {
return fileBytes;
}
public String getString() {
return new String(fileBytes);
}
public String getString(String encoding) {
String result = null;
try {
result = new String(fileBytes, encoding);
} catch (UnsupportedEncodingException e) {
logger.error("Exception in encoding : " + e.getMessage(), e);
throw new GuideException(e);
}
return result;
}
public long getSize() {
byte[] bytes = this.get();
return bytes.length;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy