org.wso2.carbon.mediation.artifactuploader.SynapseArtifactUploaderAdmin Maven / Gradle / Ivy
The newest version!
package org.wso2.carbon.mediation.artifactuploader;
import org.apache.axis2.AxisFault;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.core.AbstractAdmin;
import org.wso2.carbon.mediation.artifactuploader.util.SynapseArtifactUploaderUtil;
import org.wso2.carbon.utils.CarbonUtils;
import javax.activation.DataHandler;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Synapse Artifact Uploader Service class
*/
public class SynapseArtifactUploaderAdmin extends AbstractAdmin {
private static final Log log = LogFactory.getLog(SynapseArtifactUploaderAdmin.class);
private static final String XML_EXTENSION = "xml";
public boolean uploadArtifact(String fileName, DataHandler dataHandler) throws AxisFault {
File artifactDir = new File(getExtensionRepoPath());
File tempDir = new File(CarbonUtils.getCarbonHome() + File.separator + "tmp");
File destinationTempFile = new File(tempDir, fileName);
FileOutputStream fos = null;
String fileExtension = SynapseArtifactUploaderUtil.getFileExtension(fileName);
if (!XML_EXTENSION.equals(fileExtension)) {
throw new AxisFault("Invalid file type: " + fileExtension);
}
try {
if (!SynapseArtifactUploaderUtil.validateFilePath(destinationTempFile, tempDir)) {
throw new AxisFault("Attempt to upload " + destinationTempFile + ". File path is " +
"outside target directory");
}
fos = FileUtils.openOutputStream(destinationTempFile);
dataHandler.writeTo(fos);
} catch (IOException e) {
handleException("File Upload failed", e);
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
log.warn("Unable to close file " + destinationTempFile.getAbsolutePath(), e);
}
}
try {
FileUtils.copyFileToDirectory(destinationTempFile, artifactDir, true);
FileUtils.deleteQuietly(destinationTempFile);
} catch (IOException e) {
handleException("Unable to move file to artifact directory :" + artifactDir.getAbsolutePath(), e);
}
return true;
}
public String[] getArtifacts() {
return SynapseArtifactUploaderUtil.getArtifacts(getExtensionRepoPath());
}
public boolean removeArtifact(String fileName) throws AxisFault {
File destinationFile = new File(getExtensionRepoPath() + File.separator + fileName);
File artifactDir = new File(getExtensionRepoPath());
try {
if (!SynapseArtifactUploaderUtil.validateFilePath(destinationFile, artifactDir)) {
throw new AxisFault("Attempt to delete " + destinationFile + ". File path is " +
"outside target directory");
}
} catch (IOException e) {
handleException("File Delete failed", e);
}
File artifactFile = new File(getExtensionRepoPath() + File.separator + fileName);
if (artifactFile.exists() && artifactFile.isFile()) {
return artifactFile.delete();
} else {
handleException("Artifact " + artifactFile.getAbsolutePath() + " not found");
}
return false;
}
private String getExtensionRepoPath() {
return getAxisConfig().getRepository().getFile() + File.separator + "extensions";
}
private void handleException(String msg, Throwable e) throws AxisFault {
log.error(msg, e);
throw new AxisFault(msg, e);
}
private void handleException(String msg) throws AxisFault {
log.error(msg);
throw new AxisFault(msg);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy