Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add delete method to UploadManager
  • Loading branch information
gashcrumb committed Jun 25, 2013
1 parent 0550d47 commit 7589670
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
27 changes: 27 additions & 0 deletions hawtio-core/src/main/java/io/hawt/util/Strings.java
Expand Up @@ -37,4 +37,31 @@ public static boolean isBlank(String text) {
public static boolean isNotBlank(String text) {
return text != null && text.trim().length() > 0;
}

/**
* Strip out any annoying to deal with characters from a string when used as
* a file or directory name
*
* @param name
* @return
*/
public static String sanitize(String name) {
if (isBlank(name)) {
return name;
}
return name.replaceAll("[^0-9a-zA-Z\\+\\.\\(\\)_\\-]","");
}

/**
* Also remove any dots in the directory name
*
* @param name
* @return
*/
public static String sanitizeDirectory(String name) {
if (isBlank(name)) {
return name;
}
return sanitize(name).replace(".", "");
}
}
28 changes: 21 additions & 7 deletions hawtio-web/src/main/java/io/hawt/jmx/UploadManager.java
@@ -1,6 +1,9 @@
package io.hawt.jmx;

import io.hawt.util.Strings;
import io.hawt.web.UploadServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanServer;
Expand All @@ -15,6 +18,8 @@
*/
public class UploadManager implements UploadManagerMBean {

private static final transient Logger LOG = LoggerFactory.getLogger(UploadManager.class);

private ObjectName objectName;
private MBeanServer mBeanServer;

Expand Down Expand Up @@ -58,13 +63,7 @@ public String getUploadDirectory() {

@Override
public List<FileDTO> list(String parent) {
String uploadDir = UploadServlet.UPLOAD_DIRECTORY;

if (parent != null && !parent.equals("")) {
uploadDir = uploadDir + File.separator + parent;
}

File dir = new File(uploadDir);
File dir = new File(getTargetDirectory(parent));
if (!dir.exists()) {
return null;
}
Expand All @@ -77,6 +76,21 @@ public List<FileDTO> list(String parent) {
}
}
return rc;
}

private String getTargetDirectory(String parent) {
parent = Strings.sanitizeDirectory(parent);
if (Strings.isNotBlank(parent)) {
return UploadServlet.UPLOAD_DIRECTORY + File.separator + parent;
}
return UploadServlet.UPLOAD_DIRECTORY;
}

@Override
public boolean delete(String parent, String filename) {
filename = Strings.sanitize(filename);
File targetFile = new File(getTargetDirectory(parent), filename);
LOG.info("Deleting {}", targetFile);
return targetFile.delete();
}
}
2 changes: 2 additions & 0 deletions hawtio-web/src/main/java/io/hawt/jmx/UploadManagerMBean.java
Expand Up @@ -11,4 +11,6 @@ public interface UploadManagerMBean {

List<FileDTO> list(String parent);

boolean delete(String parent, String filename);

}
15 changes: 6 additions & 9 deletions hawtio-web/src/main/java/io/hawt/web/UploadServlet.java
@@ -1,5 +1,6 @@
package io.hawt.web;

import io.hawt.util.Strings;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
Expand Down Expand Up @@ -42,10 +43,6 @@ private static DiskFileItemFactory newDiskFileItemFactory(ServletContext context
return factory;
}

private static String sanitize(String name) {
return name.replaceAll("[^0-9a-zA-Z\\+\\.\\(\\)_\\-]","");
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

Expand Down Expand Up @@ -101,7 +98,7 @@ public void update(long pBytesRead, long pContentLength, int pItems) {
String contentType = item.getContentType();
long sizeInBytes = item.getSize();

fileName = sanitize(fileName);
fileName = Strings.sanitize(fileName);

LOG.info("Got file upload, fieldName: {} fileName: {} contentType: {} size: {}", new Object[]{fieldName, fileName, contentType, sizeInBytes});

Expand All @@ -127,16 +124,16 @@ public void update(long pBytesRead, long pContentLength, int pItems) {
}

if (targetDirectory != null) {
targetDirectory = sanitize(targetDirectory).replace(".", "");
File target = new File(uploadDir.getAbsolutePath() + File.separator + targetDirectory);
targetDirectory = Strings.sanitizeDirectory(targetDirectory);
File target = new File(uploadDir.getAbsolutePath(), targetDirectory);
LOG.info("Putting files in subdirectory: {}", targetDirectory);
if (!target.exists()) {
if (!target.mkdirs()) {
LOG.warn("Failed to create target directory: {}", target); }
LOG.warn("Failed to create target directory: {}", target); }
}

for (File file : files) {
File dest = new File(target.getAbsolutePath() + File.separator + file.getName());
File dest = new File(target.getAbsolutePath(), file.getName());
LOG.info("Renaming {} to {}", file, dest);
if (!file.renameTo(dest)) {
LOG.warn("Failed to rename {} to {}", file, dest);
Expand Down

0 comments on commit 7589670

Please sign in to comment.