throws FileUploadException, IOException {
String axis2Repo = ServerConfiguration.getInstance().
getFirstProperty(ServerConfiguration.AXIS2_CONFIG_REPO_LOCATION);
if (CarbonUtils.isURL(axis2Repo)) {
String msg = "You are not permitted to upload jars to URL repository";
throw new FileUploadException(msg);
}
String tmpDir = (String) configurationContext.getProperty(ServerConstants.WORK_DIR);
String uuid = String.valueOf(System.currentTimeMillis() + Math.random());
tmpDir = tmpDir + File.separator + "artifacts" + File.separator + uuid + File.separator;
File tmpDirFile = new File(tmpDir);
if (!tmpDirFile.exists() && !tmpDirFile.mkdirs()) {
log.warn("Could not create " + tmpDirFile.getAbsolutePath());
}
response.setContentType("text/html; charset=utf-8");
ServletRequestContext servletRequestContext = new ServletRequestContext(request);
boolean isMultipart =
ServletFileUpload.isMultipartContent(servletRequestContext);
if (isMultipart) {
PrintWriter out = null;
try {
out = response.getWriter();
// Create a new file upload handler
List items = parseRequest(servletRequestContext);
// Process the uploaded items
for (Iterator iter = items.iterator(); iter.hasNext();) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String fileName = item.getName();
String fileExtension = fileName;
fileExtension = fileExtension.toLowerCase();
String fileNameOnly = getFileName(fileName);
File uploadedFile;
String fieldName = item.getFieldName();
if (fieldName != null && fieldName.equals("jarResource")) {
if (fileExtension.endsWith(".jar")) {
File servicesDir =
new File(tmpDir + File.separator + uploadDirName, "lib");
if (!servicesDir.exists() && !servicesDir.mkdirs()){
log.warn("Could not create " + servicesDir.getAbsolutePath());
}
uploadedFile = new File(servicesDir, fileNameOnly);
item.write(uploadedFile);
}
} else {
File servicesDir = new File(tmpDir, uploadDirName);
if (!servicesDir.exists() && !servicesDir.mkdirs()) {
log.warn("Could not create " + servicesDir.getAbsolutePath());
}
uploadedFile = new File(servicesDir, fileNameOnly);
item.write(uploadedFile);
}
}
}
//First lets filter for jar resources
String repo = configurationContext.getAxisConfiguration().getRepository().getPath();
//Writing the artifacts to the proper location
String parent = repo + File.separator + uploadDirName;
File mainDir = new File(tmpDir + File.separator + uploadDirName);
File libDir = new File(mainDir, "lib");
File[] resourceLibFile =
FileManipulator.getMatchingFiles(libDir.getAbsolutePath(), null, "jar");
for (File src : resourceLibFile) {
File dst = new File(parent, "lib");
String[] files = libDir.list();
for (String file : files) {
copyFile(src, new File(dst, file));
}
}
for (String extension : extensions) {
File[] mainFiles =
FileManipulator.getMatchingFiles(mainDir.getAbsolutePath(), null, extension);
for (File mainFile : mainFiles) {
File dst = new File(parent);
String[] files = mainDir.list();
for (String file : files) {
File f = new File(dst, file);
if (!f.isDirectory()) {
copyFile(mainFile, f);
}
}
}
}
response.sendRedirect(getContextRoot(request) + "/carbon/service-mgt/index.jsp?message=Files have been uploaded "
+ "successfully. This page will be auto refreshed shortly with "
+ "the status of the created " + utilityString + " service"); //TODO: why do we redirect to service-mgt ???
return true;
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
String msg = "File upload failed";
log.error(msg, e);
throw new FileUploadException(msg, e);
} finally {
if (out != null) {
out.close();
}
}