{
resp.setContentType("application/json");
resp.setStatus(200);
JsonParser jsonParser = new JsonParser();
JsonElement contentJson = jsonParser.parse(req.getReader());
JsonObject jsonObject = contentJson.getAsJsonObject();
JsonElement headers = jsonObject.get("headers");
JsonObject response = new JsonObject();
String signature;
try
{
// If this is not a multipart upload-related request, Fine Uploader will send a policy document
// as the value of a "policy" property in the request. In that case, we must base-64 encode
// the policy document and then sign it. The will include the base-64 encoded policy and the signed policy document.
if (headers == null)
{
String base64Policy = base64EncodePolicy(contentJson);
signature = sign(base64Policy);
// Validate the policy document to ensure the client hasn't tampered with it.
// If it has been tampered with, set this property on the response and set the status to a non-200 value.
// response.addProperty("invalid", true);
response.addProperty("policy", base64Policy);
}
// If this is a request to sign a multipart upload-related request, we only need to sign the headers,
// which are passed as the value of a "headers" property from Fine Uploader. In this case,
// we only need to return the signed value.
else
{
signature = sign(headers.getAsString());
}
response.addProperty("signature", signature);
resp.getWriter().write(response.toString());
}