Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hooking up a simple image uploader service.
- Loading branch information
Showing
16 changed files
with
729 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<html> | ||
<body> | ||
|
||
<form accept-charset="UTF-8" | ||
action="http://local.commons-user.com:8080/image/file/images/3" | ||
class="html-form" enctype="multipart/form-data" id="base64_decoder" | ||
method="post"><div style="margin:0;padding:0;display:inline"><input | ||
name="utf8" type="hidden" value="✓"> | ||
|
||
<div class="html-form-header"> | ||
<p> | ||
Please enter data to decode. | ||
</p> | ||
<p class="mandatory-message"> | ||
<span class="mandatory-sign">*</span> denotes mandatory field. | ||
</p> | ||
|
||
</div> | ||
|
||
|
||
<fieldset> | ||
<div class="html-form-section"> | ||
<h2>Data information</h2> | ||
<p>Please enter data to text area or select file with data.</p> | ||
</div> | ||
|
||
<ul> | ||
<li> | ||
<label for="base64_decoder_base64dump">Base64 data to | ||
decode<span class="mandatory-sign">*</span></label> | ||
<textarea cols="40" id="base64_decoder_base64dump" | ||
name="base64_decoder[base64dump]" rows="20"></textarea> | ||
<input id="base64_decoder_file" name="base64_decoder[file]" type="file"> | ||
<div class="form-hint">Enter data or select file to decode</div> | ||
|
||
</li> | ||
<li> | ||
<input name="base64_decoder[save_to_file]" type="hidden" | ||
value="0"><input id="base64_decoder_save_to_file" | ||
name="base64_decoder[save_to_file]" type="checkbox" value="1"> | ||
<label for="base64_decoder_save_to_file">Save result to file</label> | ||
<div class="form-hint">Save result to file rather than output | ||
to browser</div> | ||
|
||
</li> | ||
</ul> | ||
</fieldset> | ||
<fieldset> | ||
|
||
<input name="commit" type="submit" value="Decode"> | ||
</fieldset> | ||
</form> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
commons-user-service/src/main/java/com/neosavvy/ImageConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.neosavvy; | ||
|
||
import com.yammer.dropwizard.config.Configuration; | ||
import org.codehaus.jackson.annotate.JsonProperty; | ||
import org.hibernate.validator.constraints.NotEmpty; | ||
|
||
public class ImageConfiguration extends Configuration { | ||
|
||
@NotEmpty | ||
@JsonProperty | ||
private String imageDirectory = "things"; | ||
|
||
} |
140 changes: 140 additions & 0 deletions
140
commons-user-service/src/main/java/com/neosavvy/ImageResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package com.neosavvy; | ||
|
||
import com.neosavvy.model.ResourceNotFoundException; | ||
import com.neosavvy.model.StorageBucket; | ||
import com.neosavvy.model.StorageBucketType; | ||
import com.neosavvy.utils.FileUtils; | ||
import com.neosavvy.utils.HttpUtils; | ||
import com.neosavvy.utils.StringUtils; | ||
import org.apache.commons.fileupload.FileItemIterator; | ||
import org.apache.commons.fileupload.FileItemStream; | ||
import org.apache.commons.fileupload.servlet.ServletFileUpload; | ||
import org.apache.commons.fileupload.util.Streams; | ||
|
||
import javax.ws.rs.*; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Request; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* User: waparrish | ||
* Date: 4/24/12 | ||
* Time: 12:53 PM | ||
* To change this template use File | Settings | File Templates. | ||
*/ | ||
@Path("/image") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public class ImageResource { | ||
|
||
private List<StorageBucket> buckets; | ||
|
||
public List<StorageBucket> getBuckets() { | ||
|
||
if( buckets == null ) | ||
{ | ||
buckets = new ArrayList<StorageBucket>(); | ||
buckets.add(new StorageBucket( | ||
"images" | ||
, StorageBucketType.STANDARD | ||
, new File("/tmp/images/") | ||
)); | ||
buckets.add(new StorageBucket( | ||
"temp" | ||
, StorageBucketType.TEMPORARY | ||
, new File("/tmp/tempFiles") | ||
)); | ||
} | ||
|
||
return buckets; | ||
} | ||
|
||
@POST | ||
@Path(value="/file/{bucket:.*?}/{key:.*}") | ||
@Encoded | ||
@Consumes(MediaType.MULTIPART_FORM_DATA) | ||
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML}) | ||
public void uploadFile(@Context Request request, @PathParam("bucket")String bucket, @PathParam("key")String key) throws Exception | ||
{ | ||
|
||
String owner = null; | ||
String fileNameOverride = null; | ||
FileItemIterator iter = new ServletFileUpload().getItemIterator(HttpUtils.getHttpRequest()); | ||
|
||
while (iter.hasNext()) { | ||
FileItemStream item = iter.next(); | ||
if (item.isFormField() && item.getFieldName().equals("owner")) { | ||
owner = Streams.asString(item.openStream()); | ||
} | ||
else if (item.isFormField() && item.getFieldName().equals("fileNameOverride")) { | ||
fileNameOverride = Streams.asString(item.openStream()); | ||
} | ||
else if (!item.isFormField()){ | ||
saveFile(bucket, key, (!StringUtils.isEmpty(fileNameOverride) ? fileNameOverride : item.getName()), | ||
parseContentType(item.getContentType()), item.openStream(), owner); | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
public void saveFile(String bucket, String key, String fileName, String contentType, InputStream data, String owner) | ||
throws IOException, ResourceNotFoundException { | ||
|
||
File bucketDir = lookupBucketDirectory(bucket); | ||
|
||
|
||
File parentDir = bucketDir; | ||
File file = new File(parentDir, key); | ||
|
||
if (!parentDir.exists()) { | ||
parentDir.mkdirs(); | ||
} | ||
|
||
FileUtils.writeFile(data, file); | ||
} | ||
|
||
private File lookupBucketDirectory(String bucketName) throws ResourceNotFoundException { | ||
StorageBucket bucket = getBucket(bucketName); | ||
|
||
if (bucket == null) { | ||
throw new ResourceNotFoundException("The bucket " + bucketName + " is not a known bucket."); | ||
} | ||
|
||
if (!bucket.getDirectory().exists()) { | ||
throw new ResourceNotFoundException("The directory for bucket " + bucketName + " does not exist"); | ||
} | ||
|
||
return bucket.getDirectory(); | ||
} | ||
|
||
public StorageBucket getBucket(String name) { | ||
for (StorageBucket bucket : getBuckets()) { | ||
if (bucket.getName().equals(name)) { | ||
return bucket; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private String parseContentType(String contentType) { | ||
if (!StringUtils.isEmpty(contentType)) { | ||
int delimiter = contentType.indexOf(';'); | ||
|
||
if (delimiter != -1) { | ||
return contentType.substring(0, delimiter); | ||
} | ||
|
||
} | ||
|
||
return contentType; | ||
} | ||
|
||
|
||
} |
31 changes: 31 additions & 0 deletions
31
commons-user-service/src/main/java/com/neosavvy/ImageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.neosavvy; | ||
|
||
import com.neosavvy.utils.HttpServletRequestFilter; | ||
import com.yammer.dropwizard.Service; | ||
import com.yammer.dropwizard.config.Environment; | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* User: waparrish | ||
* Date: 4/20/12 | ||
* Time: 2:38 PM | ||
* To change this template use File | Settings | File Templates. | ||
*/ | ||
public class ImageService extends Service<ImageConfiguration> { | ||
|
||
public static void main(String[] args) throws Exception | ||
{ | ||
new ImageService().run(args); | ||
} | ||
|
||
private ImageService() { | ||
super("image"); | ||
} | ||
|
||
@Override | ||
protected void initialize(ImageConfiguration configuration, Environment environment) throws Exception { | ||
environment.addResource(new ImageResource()); | ||
|
||
environment.addFilter(new HttpServletRequestFilter(), "/*"); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
commons-user-service/src/main/java/com/neosavvy/model/ResourceNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.neosavvy.model; | ||
|
||
/** | ||
* This class is a derivative work of Fineline via Tommy Odom. | ||
* The code herein cannot be distributed without first establishing prior written consent | ||
* from tommy.odom@gmail.com | ||
*/ | ||
public class ResourceNotFoundException extends Exception | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
public ResourceNotFoundException() { | ||
} | ||
|
||
public ResourceNotFoundException(String message) { | ||
super(message); | ||
} | ||
|
||
public ResourceNotFoundException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public ResourceNotFoundException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
} |
Oops, something went wrong.