String json;
if(!Arrays.asList(acceptablePrefixes).contains(imagePrefix))
{
logger.error("Invalid image size");
throw new ExecutionException("Invalid image size.");
}
//parse the json input from the url
try
{
json = URLDecoder.decode(urlJson, "UTF-8");
}
catch (UnsupportedEncodingException e)
{
logger.error("Invalid incoming JSON");
throw new ExecutionException("Invalid JSON.");
}
JSONArray jsonArray = JSONArray.fromObject(json);
//get users from url list and create map of users
try
{
List<String> peopleIdsToFetch = new ArrayList<String>();
for (Object jsonItem:jsonArray)
{
String accountId = ((JSONObject) jsonItem).get("id").toString();
String avatarId = ((JSONObject) jsonItem).get("avatarId").toString();
peopleIdsToFetch.add(accountId);
if(avatarId!=null && !avatarId.isEmpty())
{
avatarIdToPeopleMap.put(avatarId, accountId);
}
}
// fetch the people
people = peopleMapper.execute(peopleIdsToFetch);
}
catch (Exception ex)
{
logger.error("Error occurred retrieving the users from the db.", ex);
throw new ExecutionException("error retrieving users");
}
//create new object to be passed back as json and create list of avatarIds to be passed to the db query
JSONArray personProperties = new JSONArray();
List<String> avatarIdList = new ArrayList<String>();
for (PersonModelView currentPersonProperties : people)
{
if(!avatarIdToPeopleMap.containsKey(currentPersonProperties.getAvatarId()))
{
avatarIdList.add(imagePrefix+currentPersonProperties.getAvatarId());
}
}
//get all avatar by avatarId
try
{
if(!avatarIdList.isEmpty())
{
avatars = avatarMapper.execute(avatarIdList);
}
}
catch (Exception ex)
{
logger.error("Error occurred retrieving the users from the db.", ex);
throw new ExecutionException("Error retieving avatars");
}
//loop through and build json object for response
for (PersonModelView currentPersonProperties : people)
{
JSONObject p = new JSONObject();
//if it's not the same send back base64 image and info
if(avatarIdList.contains(imagePrefix+currentPersonProperties.getAvatarId()))
{
p.put("id", currentPersonProperties.getAccountId());
p.put("avatarId", currentPersonProperties.getAvatarId());
for(Map<String, Object>currentAvatar:avatars)
{
String imageId = (String) currentAvatar.get("imageIdentifier");
if((imagePrefix+currentPersonProperties.getAvatarId()).equals(imageId))
{
byte[] baseBytes = Base64.encodeBase64((byte[]) currentAvatar.get("imageBlob"));
String baseString = new String(baseBytes);
p.put("imageBlob", baseString);
}
}
personProperties.add(p);
}
}
//return response
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
response.addHeader("Pragma", "no-cache");
response.setHeader("Content-Type", "application/json");
JsonGenerator jsonGenerator;
try
{
jsonGenerator = jsonFactory.createJsonGenerator(response.getWriter());
jsonObjectMapper.writeValue(jsonGenerator, personProperties);
}
catch (IOException e)
{
logger.error("error creating json", e);
throw new ExecutionException("error creating json format");
}
}