//setup and precondition checks
JSONObject requestObject = OrionServlet.readJSONRequest(request);
String name = computeName(request, requestObject);
if (name.length() == 0)
return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "File name not specified.", null));
IFileStore toCreate = dir.getChild(name);
if (!name.equals(toCreate.getName()) || name.contains(":"))
return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Bad file name: " + name, null));
int options = getCreateOptions(request);
boolean destinationExists = toCreate.fetchInfo(EFS.NONE, null).exists();
if (!validateOptions(request, response, toCreate, destinationExists, options))
return true;
//perform the operation
if (performPost(request, response, requestObject, toCreate, options)) {
//write the response
URI location = URIUtil.append(getURI(request), name);
JSONObject result = ServletFileStoreHandler.toJSON(toCreate, toCreate.fetchInfo(EFS.NONE, null), location);
result.append("FileEncoding", System.getProperty("file.encoding"));
OrionServlet.writeJSONResponse(request, response, result);
response.setHeader(ProtocolConstants.HEADER_LOCATION, ServletResourceHandler.resovleOrionURI(request, location).toString());
//response code should indicate if a new resource was actually created or not
response.setStatus(destinationExists ? HttpServletResponse.SC_OK : HttpServletResponse.SC_CREATED);