//parse json from request
JSONObject jsonObj = null;
jsonObj = getJsonObject(request);
DbHelper dbHelper = new DbHelper(new File(getServletContext().getRealPath("/")), request);
//is user logged in?
int userId = dbHelper.getUserId();
if (userId < 0) {
response.sendError(HttpServletResponse.SC_FORBIDDEN, "You should be logged in before submitting completions");
return;
}
FetchType type = getTypeFromJson(jsonObj);
String endpoint = getEndpointFromJson(jsonObj);
JSONArray completions = jsonObj.getJSONArray(AutocompleteKeys.REQUEST_COMPLETIONS);
//does endpoint already exist as a public endpoint?? If so, ignore..
try {
dbHelper.getEndpointId(endpoint, EndpointPrivateFlag.PUBLIC);
//hmm, no exception thrown. there is a public version of this endpoint!!!
response.sendError(HttpServletResponse.SC_CONFLICT, "Endpoint " + endpoint + " is stored as -public- endpoint already. Using this endpoint url as a private endpoint is not allowed");
return;
} catch (EndpointIdException e) {
//this is fine ;)
}
//try to get endpoint id for this specific user
int endpointId;
try {
endpointId = dbHelper.getEndpointId(endpoint, EndpointPrivateFlag.OWN);
} catch (EndpointIdException e) {
//endpoint does not exist yet for this user. create a private one!
endpointId = dbHelper.generateIdForEndpoint(endpoint, AccessibilityStatus.INACCESSIBLE);
}
//clear previous fetches for this endpoint!
dbHelper.clearPreviousAutocompletionFetches(endpointId, FetchMethod.QUERY_RESULTS, type);
dbHelper.storeCompletionFetchesFromLocalhost(endpointId, type, FetchMethod.QUERY_RESULTS, completions);
//done!
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
} catch (JSONException e) {