Java Spring User Image Uploader with Mongodb - java

I've created a basic user system, where people can register and login.
Its storing textual data at this time, but I would like to expand the code to allow users to upload images of themselves.
I am using a mongodb, so I can stash the user id as a folder when the user gets created, as they add images of themselves it will go into this folder.
1.How do I dynamically create these folders and where. I've read the folder would normally be stored in the WEB-INF.
Or should/could the images be stored as a blob file in the mongodb?
2.How do I copy/store the images into these folders correctly, in the correct place.
I've tried adapting code from here http://www.roseindia.net/tutorial/spring/spring3/web/spring-3-mvc-fileupload-example.html
Its currently not relating to the user, nor is it organising the images into folders. Eventually I'd like to auto scale/crop the images to produce various thumbnail sizes.
FileUpload.java
package net.fileupload;
import org.springframework.web.multipart.MultipartFile;
public class FileUpload {
MultipartFile file;
public void setFile(MultipartFile file){
this.file=file;
}
public MultipartFile getFile(){
return file;
}
}
FileUploadController.java
package net.fileupload;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.util.*;
import net.fileupload.FileUpload;;
public class FileUploadController extends SimpleFormController{
#Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
FileUpload fileUpload = (FileUpload)command;
MultipartFile multipartFile = fileUpload.getFile();
String fileName="";
// image type of file processing...
System.err.println("-------------------------------------------");
try {
InputStream inputStream = null;
OutputStream outputStream = null;
if (multipartFile.getSize() > 0) {
inputStream = multipartFile.getInputStream();
fileName = request.getRealPath("") + "/images/"+ multipartFile.getOriginalFilename();
System.out.println(fileName);
outputStream = new FileOutputStream(fileName);
int readBytes = 0;
byte[] buffer = new byte[2000000];
while ((readBytes = inputStream.read(buffer, 0, 2000000)) != -1) {
outputStream.write(buffer, 0, readBytes);
}
outputStream.close();
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
// ..........................................
Map model = new HashMap();
model.put("fileName", multipartFile.getOriginalFilename());
model.put("filepath", "images/"+multipartFile.getOriginalFilename());
return new ModelAndView(getSuccessView(), model);
}
}
FileUploadValidator.java
package net.fileupload;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import net.fileupload.FileUpload;
public class FileUploadValidator implements Validator{
#Override
public boolean supports(Class clazz){
return FileUpload.class.isAssignableFrom(clazz);
}
#Override
public void validate(Object target, Errors errors){
FileUpload fileUpload = (FileUpload)target;
if(fileUpload.getFile().getSize()==0){
errors.rejectValue("file", "error.empty.file", "Please Select File.");
}
System.out.println(fileUpload.getFile().getSize());
if(fileUpload.getFile().getSize() > 2000000){
errors.rejectValue("file", "error.empty.file", "File size more than 2000000 bytes ");
}
}
}
Retrieving the images.
In theory the following should return the list of images relevant to a user. May wish to return these images as a json array.
a gallery controller
import java.util.ArrayList;
import java.util.List;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
public class GalleryController{
public static List<DBObject> getGallery(){
//__Prepare response
List<DBObject> images = new ArrayList<DBObject>();
BasicDBObject results = new BasicDBObject();
BasicDBObject result = new BasicDBObject();
result.put("image1", "/images/Bannan.jpg");
result.put("image2", "/images/Koala.jpg");
result.put("image3", "/images/Monkey.jpg");
results.put("images", result);
images.add(results);
return images;
}
}
a listener controller
/*
* Gallery methods
*/
#RequestMapping(method=RequestMethod.GET, value={"/gallery"})
public ModelAndView galleryDisplay(
HttpServletRequest request,
HttpServletResponse response,
#RequestParam(value="mode", required=false) String mode
) {
String message = "search view for "+mode;
List<DBObject> images = GalleryController.getGallery();
System.out.println(images);
String viewPage = "gallery/galleryview";
return new ModelAndView(viewPage, "images", images);
}

My current user controller looks like this.
Would be great if I can hook into the images for the user at this level and provide it for the template.
My current mapping works well with id's as parameters. localhost:8080/user?id=1312321321
/*
* User
*/
#RequestMapping(method=RequestMethod.GET, value={"/user","/user/{id}"})
public ModelAndView profileDisplay(
HttpServletRequest request,
HttpServletResponse response,
#RequestParam(value="id", required=false) String id
) throws UnknownHostException, MongoException {
ServiceSerlvet.appendSesssion(request);
//get search ALL users
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("_id", new ObjectId(id));
List<DBObject> searchResponse =
PersonController.searchUsers(searchQuery);
//System.out.println("response from search user method:
"+searchResponse);
return new ModelAndView("user", "people", searchResponse);
}

Related

Failed to parse the JSON document: Request successfully return 200 response but response body isn't printed in Log file

I got an exception with validating "status": "OK" in the response body of the DeletePlace request. Request is successful with 200 status code but there is no response body in the log file. Console error is pointing to the Utils line#47. Below is the code & screenshot of the error console pointing error to:
enter image description here
package resources;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Properties;
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.filter.log.RequestLoggingFilter;
import io.restassured.filter.log.ResponseLoggingFilter;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
public class Utils {
public static RequestSpecification req;
public RequestSpecification requestSpecification() throws IOException
{
if (req==null)
{
PrintStream log = new PrintStream(new FileOutputStream("logging.txt"));
req = new RequestSpecBuilder().setBaseUri(getGlobalValue("baseUrl")).addQueryParam("key", "qaclick123")
.addFilter(RequestLoggingFilter.logRequestTo(log))
.addFilter(ResponseLoggingFilter.logResponseTo(log))
.setContentType(ContentType.JSON).build();
return req;
}
return req;
}
public static String getGlobalValue(String key) throws IOException
{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("E:\\Eclipse-Workspace\\RestAssuredAPIFramework\\src\\test\\java\\resources\\global.properties");
prop.load(fis);
return prop.getProperty(key);
}
public String getJsonPath(Response response, String key)
{
String resp = response.asString();
JsonPath js = new JsonPath(resp);
return js.get(key).toString(); //Error line
}
}
Please help me out and let me know if any other information is required.
I've tried creating getJsonPath method again and all possible fixes I got from various articles but not able to resolved this. I'm expecting the test to execute without any error.
i recommend you to use org.springframework.core.io.Resource to get your properties, like this :
#Value(value = "classpath:your_file.json")
private Resource resource;
after you got your file, try to mapping with :
public static Map<String, Object> jsonToMap(String json) {
try {
final ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, new TypeReference<>(){});
}catch (Exception e) {
e.printStackTrace();
}
return null;
}

[Java][Spring][AWS S3] What is memory efficient way to return large file (s3) from Spring Controller [duplicate]

I want to expose an API to download a S3 bucket file content as stream to its consumers.
The API URL is like /downloadfile/** which is GET request.
What should be my return type right now I tried with accept header=
application/octet-stream which didn't work.
I don't want to write the content of file to any file and send it. It should be returned as a stream that's it.
Here is the controller pseudo code I wrote till now which is giving me 406 error all the time.
#GetMapping(value = "/downloadfile/**", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE })
public ResponseEntity<Object> downloadFile(HttpServletRequest request) {
//reads the content from S3 bucket and returns a S3ObjectInputStream
S3ObjectInputStream object = null;
object = publishAmazonS3.getObject("12345bucket", "/logs/file1.log").getObjectContent();
return object
}
Any suggestions here on the way of doing this and what I am doing wrong?
I was able to download the file as a stream by using StreamingResponseBody class from Spring.
Here is the code I used:
#GetMapping(value = "/downloadfile/**", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE })
public ResponseEntity<StreamingResponseBody> downloadFile(HttpServletRequest request) {
//reads the content from S3 bucket and returns a S3ObjectInputStream
S3Object object = publishAmazonS3.getObject("12345bucket", "/logs/file1.log");
S3ObjectInputStream finalObject = object.getObjectContent();
final StreamingResponseBody body = outputStream -> {
int numberOfBytesToWrite = 0;
byte[] data = new byte[1024];
while ((numberOfBytesToWrite = finalObject.read(data, 0, data.length)) != -1) {
System.out.println("Writing some bytes..");
outputStream.write(data, 0, numberOfBytesToWrite);
}
finalObject.close();
};
return new ResponseEntity<>(body, HttpStatus.OK);
}
The way to test the streaming is done correctly or not is to have a file of around 400mb to download. Reduce your Xmx to 256mb by passing the in the vm options. Now, compare the download functionality with and without using StreamingResponseBody, you will get OutofMemoryError when using the conventional OutputStreams for writing the content
You can solve with the below example
import java.io.ByteArrayOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.grokonez.s3.services.S3Services;
#RestController
public class DownloadFileController {
#Autowired
S3Services s3Services;
/*
* Download Files
*/
#GetMapping("/api/file/{keyname}")
public ResponseEntity<byte[]> downloadFile(#PathVariable String keyname) {
ByteArrayOutputStream downloadInputStream = s3Services.downloadFile(keyname);
return ResponseEntity.ok().contentType(contentType(keyname))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + keyname + "\"")
.body(downloadInputStream.toByteArray());
}
private MediaType contentType(String keyname) {
String[] arr = keyname.split("\\.");
String type = arr[arr.length - 1];
switch (type) {
case "txt":
return MediaType.TEXT_PLAIN;
case "png":
return MediaType.IMAGE_PNG;
case "jpg":
return MediaType.IMAGE_JPEG;
default:
return MediaType.APPLICATION_OCTET_STREAM;
}
}
}

Google Cloud Vision OCR Returns "Bad Image Data" on Google Cloud Shell Localhost

tl;dr: How can I get Google Cloud Vision OCR to work on Cloud Shell editor's localhost?
I'm using Google Cloud Shell editor, which contains a web preview feature that serves "local" webservers at URLs like https://8080-dot-10727374-dot-devshell.appspot.com/index.html.
I'm following this tutorial for Cloud Vision OCR. I put that example code into a servlet that uses Blobstore as an image host:
package com.google.servlets;
import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.ServingUrlOptions;
import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.cloud.vision.v1.ImageSource;
import com.google.cloud.vision.v1.TextAnnotation;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* When the user submits the form, Blobstore processes the file upload and then forwards the request
* to this servlet. This servlet can then analyze the image using the Vision API.
*/
#WebServlet("/image-analysis")
public class ImageAnalysisServlet extends HttpServlet {
#Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
// Get the BlobKey that points to the image uploaded by the user.
BlobKey blobKey = getBlobKey(request, "image");
// Get the URL of the image that the user uploaded.
String imageUrl = getUploadedFileUrl(blobKey);
// Extract text from the image
String text = detectDocumentText(imageUrl);
// Output some HTML.
response.setContentType("text/html");
out.println("<p>Here's the image you uploaded:</p>");
out.println("<a href=\"" + imageUrl + "\">");
out.println("<img src=\"" + imageUrl + "\" />");
out.println("</a>");
out.println("<h1>text: " + text + "</h1>");
}
/**
* Returns the BlobKey that points to the file uploaded by the user, or null if the user didn't
* upload a file.
*/
private BlobKey getBlobKey(HttpServletRequest request, String formInputElementName) {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(request);
List<BlobKey> blobKeys = blobs.get("image");
// User submitted form without selecting a file, so we can't get a BlobKey. (dev server)
if (blobKeys == null || blobKeys.isEmpty()) {
return null;
}
// Our form only contains a single file input, so get the first index.
BlobKey blobKey = blobKeys.get(0);
// User submitted form without selecting a file, so the BlobKey is empty. (live server)
BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);
if (blobInfo.getSize() == 0) {
blobstoreService.delete(blobKey);
return null;
}
return blobKey;
}
/** Returns a URL that points to the uploaded file. */
private String getUploadedFileUrl(BlobKey blobKey) {
ImagesService imagesService = ImagesServiceFactory.getImagesService();
ServingUrlOptions options = ServingUrlOptions.Builder.withBlobKey(blobKey);
String url = imagesService.getServingUrl(options);
// GCS's localhost preview is not actually on localhost,
// so make the URL relative to the current domain.
if(url.startsWith("http://localhost:8080/")){
url = url.replace("http://localhost:8080/", "https://8080-dot-10727374-dot-devshell.appspot.com/");
}
return url;
}
private String detectDocumentText(String path) throws IOException {
List<AnnotateImageRequest> requests = new ArrayList<>();
ImageSource imgSource = ImageSource.newBuilder().setImageUri(path).build();
Image img = Image.newBuilder().setSource(imgSource).build();
Feature feat = Feature.newBuilder().setType(Feature.Type.DOCUMENT_TEXT_DETECTION).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
client.close();
// Check to see if any of the responses are errors
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.format("Error: %s%n", res.getError().getMessage());
return "Error: " + res.getError().getMessage();
}
// For full list of available annotations, see http://g.co/cloud/vision/docs
TextAnnotation annotation = res.getFullTextAnnotation();
return annotation.getText();
}
}
catch(Exception e) {
return "ERROR: ImageAnnotatorClient Failed, " + e;
}
// Case where the ImageAnnotatorClient works, but there are no responses from it.
return "Error: No responses";
}
}
When I deploy to a real server using the mvn package appengine:deploy command, this works perfectly:
(Well, as perfectly as can be expected from this test image.)
However, if I deploy to a "local" devserver using the mvn package appengine:run command, then Google Cloud Vision returns a generic "Bad image data" error:
I'm guessing this is because the image URL (https://8080-dot-10727374-dot-devshell.appspot.com/_cloudshellProxy/_ah/img/TjxgeYiHlCkix-XRj94jnw) is not publicly accessible, because it's running on a "fake" localhost that requires me to be logged into my Google account to see.
How can I get Google Cloud Vision OCR to work on Cloud Shell editor's "fake" localhost?
Cloud Vision also supports reading image bytes directly rather than going through a URL. Switching to that allowed me to bypass the requirement to have a publicly accessible URL.
The line that matters is this one:
Image img = Image.newBuilder().setContent(ByteString.copyFrom(bytes)).build();
...where bytes come from what's stored in Blobstore.
Full code for reference:
package com.google.servlets;
import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.ServingUrlOptions;
import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.cloud.vision.v1.ImageSource;
import com.google.cloud.vision.v1.TextAnnotation;
import com.google.protobuf.ByteString;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* When the user submits the form, Blobstore processes the file upload and then forwards the request
* to this servlet. This servlet can then analyze the image using the Vision API.
*/
#WebServlet("/image-analysis")
public class ImageAnalysisServlet extends HttpServlet {
#Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
// Get the BlobKey that points to the image uploaded by the user.
BlobKey blobKey = getBlobKey(request, "image");
// Get the URL of the image that the user uploaded.
String imageUrl = getUploadedFileUrl(blobKey);
byte[] blobBytes = getBlobBytes(blobKey);
// Extract text from the image
String text = detectDocumentText(blobBytes);
// Output some HTML.
response.setContentType("text/html");
out.println("<p>Here's the image you uploaded:</p>");
out.println("<a href=\"" + imageUrl + "\">");
out.println("<img src=\"" + imageUrl + "\" />");
out.println("</a>");
out.println("<h1>text: " + text + "</h1>");
}
private byte[] getBlobBytes(BlobKey blobKey) throws IOException {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
ByteArrayOutputStream outputBytes = new ByteArrayOutputStream();
int fetchSize = BlobstoreService.MAX_BLOB_FETCH_SIZE;
long currentByteIndex = 0;
boolean continueReading = true;
while (continueReading) {
// end index is inclusive, so we have to subtract 1 to get fetchSize bytes
byte[] b =
blobstoreService.fetchData(blobKey, currentByteIndex, currentByteIndex + fetchSize - 1);
outputBytes.write(b);
// if we read fewer bytes than we requested, then we reached the end
if (b.length < fetchSize) {
continueReading = false;
}
currentByteIndex += fetchSize;
}
return outputBytes.toByteArray();
}
/**
* Returns the BlobKey that points to the file uploaded by the user, or null if the user didn't
* upload a file.
*/
private BlobKey getBlobKey(HttpServletRequest request, String formInputElementName) {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(request);
List<BlobKey> blobKeys = blobs.get("image");
// User submitted form without selecting a file, so we can't get a BlobKey. (dev server)
if (blobKeys == null || blobKeys.isEmpty()) {
return null;
}
// Our form only contains a single file input, so get the first index.
BlobKey blobKey = blobKeys.get(0);
// User submitted form without selecting a file, so the BlobKey is empty. (live server)
BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);
if (blobInfo.getSize() == 0) {
blobstoreService.delete(blobKey);
return null;
}
return blobKey;
}
/** Returns a URL that points to the uploaded file. */
private String getUploadedFileUrl(BlobKey blobKey) {
ImagesService imagesService = ImagesServiceFactory.getImagesService();
ServingUrlOptions options = ServingUrlOptions.Builder.withBlobKey(blobKey);
String url = imagesService.getServingUrl(options);
// GCS's localhost preview is not actually on localhost,
// so make the URL relative to the current domain.
if(url.startsWith("http://localhost:8080/")){
url = url.replace("http://localhost:8080/", "https://8080-dot-10727374-dot-devshell.appspot.com/");
}
return url;
}
private String detectDocumentText(byte[] bytes) throws IOException {
List<AnnotateImageRequest> requests = new ArrayList<>();
Image img = Image.newBuilder().setContent(ByteString.copyFrom(bytes)).build();
Feature feat = Feature.newBuilder().setType(Feature.Type.DOCUMENT_TEXT_DETECTION).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
client.close();
// Check to see if any of the responses are errors
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.format("Error: %s%n", res.getError().getMessage());
return "Error: " + res.getError().getMessage();
}
// For full list of available annotations, see http://g.co/cloud/vision/docs
TextAnnotation annotation = res.getFullTextAnnotation();
return annotation.getText();
}
}
catch(Exception e) {
return "ERROR: ImageAnnotatorClient Failed, " + e;
}
// Case where the ImageAnnotatorClient works, but there are no responses from it.
return "Error: No responses";
}
}

ArrayList issue in HTTP Servlet

I'm using Floodlight REST API in order to monitor a created virtual network in mininet. My goal is to display an arraylist of all the switches, hosts and statistics for the switches on a web browser using Apache Tomcat web server and HTTP Servlet. The application successfully displays all the switches and hosts, but fails when I'm adding the statistics for the switches.
When I'm mapping JSON string to java objects, the server returns the error in this line:
ArrayList<Switch> queues = mapper.readValue(queueJson, new TypeReference<ArrayList<Switch>>() {
});
The error is:
HTTP status 500 - can not deserialize instance of java.util.arraylist out of start_object token
I have testet it without the switch statistics (Queues) part (with only hosts and devices) and everything works fine, but when I'm adding the queues ArrayList, it returns the above mentioned error.
How can I solve this issue ?. My code is shown below. Thanks in advance
package core;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.restlet.data.MediaType;
import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;
import pojos.Device;
import pojos.Switch;
#WebServlet("/PrintInfo")
public class PrintInfo extends HttpServlet {
private static final long serialVersionUID = 1L;
public PrintInfo() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// create ClientResource object
// List at the switches in the network
ClientResource cResourceSwitches = new ClientResource("http://127.0.0.1:8080/wm/core/controller/switches/json");
StringWriter sWriterSwitches = new StringWriter();
// List all the devices (hosts) in the network
ClientResource cResourceDevices = new ClientResource("http://127.0.0.1:8080/wm/device/");
StringWriter sWriterDevices = new StringWriter();
// List the statistics of the switches in the network
ClientResource cResourceQueues = new ClientResource("http://127.0.0.1:8080/wm/core/switch/all/queue/json");
StringWriter sWriterQueues = new StringWriter();
// get JSON data about switches; the data is put in a string writer
try {
// Getting data from Floodlight as a JSON string
cResourceSwitches.get(MediaType.APPLICATION_JSON).write(sWriterSwitches);
cResourceDevices.get(MediaType.APPLICATION_JSON).write(sWriterDevices);
cResourceQueues.get(MediaType.APPLICATION_JSON).write(sWriterQueues);
} catch (ResourceException e) {
request.setAttribute("error", "Connection with FLoodLight failed!");
request.getRequestDispatcher("WEB-INF/connectionError.jsp").forward(request, response);
return;
}
// put data from string writer into a string object
String switchesJson = sWriterSwitches.toString();
String devicesJson = sWriterDevices.toString();
String queueJson = sWriterQueues.toString();
// map JSON data to Java objects
// ObjectMapper converts between JSON - Java
ObjectMapper mapper = new ObjectMapper();
ArrayList<Switch> switches = mapper.readValue(switchesJson, new TypeReference<ArrayList<Switch>>() {
});
ArrayList<Device> devices = mapper.readValue(devicesJson, new TypeReference<ArrayList<Device>>() {
});
ArrayList<Switch> queues = mapper.readValue(queueJson, new TypeReference<ArrayList<Switch>>() {
});
// put objects in the request so we can use them later in the JSP
request.setAttribute("switches", switches);
request.setAttribute("devices", devices);
request.setAttribute("queues", queues);
// redirect to the jsp
request.getRequestDispatcher("WEB-INF/showInfo.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
}
}
Solved. The Switch class in "ArrayList" can't be used for switch statistics. A new class has to be implemented, which returns the values in
http://127.0.0.1:8080/wm/core/switch/all/queue/json
URI.

Upload multiple files from Android to AppEngine in 1 request

I understand that I can upload 1 file at a time to AppEngine using multipart/form POST requests. AppEngine also supports uploading multiple files but you have to do some hokey JSP stuff for it to work.
I have an app that requires me to upload some form data, 2 images and 3 fields of text. Is this possible to do via AppEngine? I've been trying to find information on this but it's tough nothing works with the flexibility I need. I will be storing the data in the blob store/data store.
I need a Java solution.
This is the signature of my POST method:
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
public void post(
#Context HttpServletRequest request,
#Context HttpServletResponse response)
throws FileUploadException, IOException {}
Copy and paste of the Java Servlet if you really need it. Above is the question and relevant servlet snippets.
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.apache.commons.fileupload.FileItemHeaders;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileReadChannel;
import com.google.appengine.api.files.FileService;
import com.google.appengine.api.files.FileServiceFactory;
import com.google.appengine.api.files.FileWriteChannel;
#Path("/upload")
public class FileUploadServlet {
private BlobstoreService blobstoreService = BlobstoreServiceFactory
.getBlobstoreService();
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
public void post(#Context HttpServletRequest request,
#Context HttpServletResponse response) throws FileUploadException,
IOException {
final ServletFileUpload upload = new ServletFileUpload();
final FileItemIterator fileIter = upload.getItemIterator(request);
while (fileIter.hasNext()) {
final FileItemStream item = fileIter.next();
String name = item.getName();
String fieldName = item.getFieldName();
String contentType = item.getContentType();
Log.d("Name = " + name);
Log.d("Field-Name = " + fieldName);
Log.d("Content-Type = " + contentType);
FileItemHeaders headers = item.getHeaders();
if(headers != null) {
Iterator<String> it = (Iterator<String>)headers.getHeaderNames();
while(it.hasNext()) {
String h = it.next();
Log.d(h + " = " + headers.getHeader(h));
}
}
if (item.isFormField()) {
// Nothing
} else {
RawImageData data = new RawImageData();
data.load(item.openStream());
// RawImageData reads the stream and stores it into a large byte[] called data.imageData
ByteBuffer bb = ByteBuffer.wrap(data.imageData);
FileService fs = FileServiceFactory.getFileService();
AppEngineFile file = fs.createNewBlobFile(contentType);
FileWriteChannel write = fs.openWriteChannel(file, true);
write.write(bb);
write.closeFinally();
String path = file.getFullPath();
Log.d(path);
// Later, read from the file using the file API
boolean lock = false; // Let other people read at the same time
FileReadChannel readChannel = fs.openReadChannel(file,
false);
// CRASHES WITH java.nio.charset.IllegalCharsetNameException: image/jpeg
// contentType = "image/jpeg"
// Again, different standard Java ways of reading from the
// channel.
BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, contentType));
readChannel.close();
}
}
response.setContentType("text/html");
response.getOutputStream().write("success".getBytes());
}
}
Full Exception:
WARNING: /api/upload
java.nio.charset.IllegalCharsetNameException: image/jpeg
at java.nio.charset.Charset.checkName(Charset.java:284)
at java.nio.charset.Charset.lookup2(Charset.java:458)
at java.nio.charset.Charset.lookup(Charset.java:437)
at java.nio.charset.Charset.forName(Charset.java:502)
at java.nio.channels.Channels.newReader(Channels.java:381)
at com.futonredemption.starstarstar.FileUploadServlet.post(FileUploadServlet.java:96)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
blah blah blah
You can create you own multipart file upload handler, then save files via Blobstore FileService API.

Categories

Resources