String street1=null, street2=null, city=null, state=null, zip=null, timezone=null, country=null;
String contentHeader=null, filename=null;
InputStream imageStream = null;
boolean doesExist = false;
BodyPartEntity bpe = null;
MultivaluedMap <String, String> mvMap = null;
List<String> headerList = null;
Map<String,List<FormDataBodyPart>> bodyList = multiPart.getFields();
for (String key:bodyList.keySet()){
logger.finer("key is "+ key);
}
//if(multiPart.getField("first_name")!=null) firstName = multiPart.getField("first_name").getValue();
userName = getFieldValue(multiPart, USERNAME, true);
//determine if this is an update or create
personEntity = mf.getPerson(userName);
doesExist = personEntity == null ? false : true;
firstName = getFieldValue(multiPart, FIRSTNAME, true);
lastName = getFieldValue(multiPart, LASTNAME, true);
password = getFieldValue(multiPart, PASSWORD, true);
summary = getFieldValue(multiPart, SUMMARY, false);
telephone = getFieldValue(multiPart,TELEPHONE, false);
email = getFieldValue(multiPart, EMAIL, false);
timezone = getFieldValue(multiPart, TIMEZONE, false);
logger.finer("in POST - multipart first name is "+ firstName);
//address
street1 = getFieldValue(multiPart, STREET1, false);
street2 = getFieldValue(multiPart, STREET2, false);
city = getFieldValue(multiPart, CITY, false);
state = getFieldValue(multiPart, STATE, false);
zip = getFieldValue(multiPart, ZIP, false);
country = getFieldValue(multiPart, COUNTRY, false);
Address address=WebappUtil.handleAddress(null, street1, street2, city, state, zip, country);
//image processing
if(multiPart.getField(IMAGEFILE)!=null){
bpe = ((BodyPartEntity) multiPart.getField(IMAGEFILE).getEntity());
logger.finer("the value of the imageFile body part is "+ multiPart.getField(IMAGEFILE).getHeaders());
mvMap = multiPart.getField(IMAGEFILE).getHeaders();
headerList = (List)mvMap.get("Content-Disposition");
contentHeader = headerList.get(0);
filename = WebappUtil.parseValueFromHeader(contentHeader, "filename");
imageStream = bpe.getInputStream();
} else {
logger.finer("parameter " + IMAGEFILE + " is missing");
}
/* temporary - testing multipart post
File tempFile = new File("/tmp/copyImagefile.jpg");
FileOutputStream fos = new FileOutputStream(tempFile);
BufferedImage bi = ImageIO.read(stream);
ImageIO.write(bi, "jpg", tempFile);
fos.flush();
fos.close();
bpe.cleanup();
*/
HashMap<String,String> fileInfo = saveImageWithThumbnail(userName, imageStream, filename);
//create Person
Person person = new Person(userName, password, firstName, lastName,
summary, email, telephone, fileInfo.get(FILE_LOCATION), fileInfo.get(THUMBNAIL_LOCATION), timezone, address);
mf = (ModelFacade)getServletContext().getAttribute(WebConstants.MF_KEY);
if (!doesExist)
userName = mf.addPerson(person);
else
mf.updatePerson(person);
logger.finer("after adding/updating person " + userName);
//cleanup
imageStream.close();
//delete the temporary file from multipart
bpe.cleanup();
return Response.ok().build();
}