use of org.apache.stanbol.entityhub.ldpath.query.LDPathFieldQueryImpl in project stanbol by apache.
the class JerseyUtils method createFieldQueryForFindRequest.
// /**
// * Returns the {@link FieldQuery} based on the JSON formatted String (in case
// * of "application/x-www-form-urlencoded" requests) or file (in case of
// * "multipart/form-data" requests).<p>
// * @param query the string containing the JSON serialised FieldQuery or
// * <code>null</code> in case of a "multipart/form-data" request
// * @param file the temporary file holding the data parsed by the request to
// * the web server in case of a "multipart/form-data" request or <code>null</code>
// * in case of the "application/x-www-form-urlencoded" request.
// * @return the FieldQuery parsed from the string provided by one of the two
// * parameters
// * @throws WebApplicationException if both parameter are <code>null</code> or
// * if the string provided by both parameters could not be used to parse a
// * {@link FieldQuery} instance.
// */
// public static FieldQuery parseFieldQuery(String query, File file) throws WebApplicationException {
// if(query == null && file == null) {
// throw new WebApplicationException(new IllegalArgumentException("Query Requests MUST define the \"query\" parameter"), Response.Status.BAD_REQUEST);
// }
// FieldQuery fieldQuery = null;
// JSONException exception = null;
// if(query != null){
// try {
// fieldQuery = JSONToFieldQuery.fromJSON(queryFactory,query);
// } catch (JSONException e) {
// log.warn("unable to parse FieldQuery from \"application/x-www-form-urlencoded\" encoded query string "+query,e);
// fieldQuery = null;
// exception = e;
// }
// } //else no query via application/x-www-form-urlencoded parsed
// if(fieldQuery == null && file != null){
// try {
// query = FileUtils.readFileToString(file);
// fieldQuery = JSONToFieldQuery.fromJSON(queryFactory,query);
// } catch (IOException e) {
// throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
// } catch (JSONException e) {
// log.warn("unable to parse FieldQuery from \"multipart/form-data\" encoded query string "+query,e);
// exception = e;
// }
// }//fieldquery already initialised or no query via multipart/form-data parsed
// if(fieldQuery == null){
// throw new WebApplicationException(new IllegalArgumentException("Unable to parse FieldQuery form the parsed query String:"+query, exception),Response.Status.BAD_REQUEST);
// }
// return fieldQuery;
// }
/**
* Creates an {@link FieldQuery} for parameters parsed by the /find requests
* supported by the /symbol, /sites and {siteId} RESTful endpoints.
* TODO: This has to be refactored to use "EntityQuery" as soon as Multiple
* query types are implemented for the Entityhub.
* @param name the name pattern to search entities for (required)
* @param field the field used to search for entities (required)
* @param language the language of the parsed name pattern (optional)
* @param limit the maximum number of result (optional)
* @param offset the offset of the first result (optional)
* @return the {@link FieldQuery} representing the parsed parameter
* @throws WebApplicationException in case the parsed name pattern is invalid.
* The validation of this required parameter provided by the Request is done
* by this method.
* @throws IllegalArgumentException in case the parsed field is invalid. Callers
* of this method need to ensure that this parameter is set to an valid value.
*/
public static FieldQuery createFieldQueryForFindRequest(String name, String field, String language, Integer limit, Integer offset, String ldpath) throws WebApplicationException, IllegalArgumentException {
if (name == null || name.trim().isEmpty()) {
// as an bad Requested sent by the user
throw new WebApplicationException(new IllegalArgumentException("The parsed \"name\" pattern to search entities for MUST NOT be NULL nor EMPTY"), Response.Status.BAD_REQUEST);
} else {
name = name.trim();
}
if (field == null || field.trim().isEmpty()) {
// valid data.
throw new IllegalArgumentException("The parsed search \"field\" MUST NOT be NULL nor EMPTY");
} else {
field = field.trim();
}
log.debug("Process Find Request:");
log.debug(" > name : " + name);
log.debug(" > field : " + field);
log.debug(" > lang : " + language);
log.debug(" > limit : " + limit);
log.debug(" > offset: " + offset);
log.debug(" > ldpath: " + ldpath);
FieldQuery query;
if (ldpath != null && !ldpath.isEmpty()) {
// STANBOL-417
query = new LDPathFieldQueryImpl();
((LDPathFieldQueryImpl) query).setLDPathSelect(ldpath);
} else {
// if no LDPath is parsed select the default field
query = queryFactory.createFieldQuery();
Collection<String> selectedFields = new ArrayList<String>();
// select also the field used to find entities
selectedFields.add(field);
query.addSelectedFields(selectedFields);
}
if (language == null || language.trim().isEmpty()) {
query.setConstraint(field, new TextConstraint(name, PatternType.wildcard, false));
} else {
query.setConstraint(field, new TextConstraint(name, PatternType.wildcard, false, language));
}
if (limit != null && limit > 0) {
query.setLimit(limit);
}
if (offset != null && offset > 0) {
query.setOffset(offset);
}
return query;
}
use of org.apache.stanbol.entityhub.ldpath.query.LDPathFieldQueryImpl in project stanbol by apache.
the class FieldQueryReader method fromJSON.
/**
* @param queryFactory
* @param jsonQueryString
* @param acceptedMediaType used to add the accept header to Error responses
* @return
* @throws JSONException
* @throws WebApplicationException
*/
public static FieldQuery fromJSON(String jsonQueryString, MediaType acceptedMediaType, NamespacePrefixService nsPrefixService) throws JSONException, WebApplicationException {
if (jsonQueryString == null) {
throw new IllegalArgumentException("The parsed JSON object MUST NOT be NULL!");
}
JSONObject jQuery = new JSONObject(jsonQueryString);
FieldQuery query;
if (jQuery.has("ldpath")) {
// STANBOL-417: support for using LDPath as select
LDPathFieldQueryImpl ldPathQuery = new LDPathFieldQueryImpl();
ldPathQuery.setLDPathSelect(jQuery.getString("ldpath"));
query = ldPathQuery;
} else {
query = new FieldQueryImpl();
}
if (!jQuery.has("constraints")) {
StringBuilder message = new StringBuilder();
message.append("The parsed Field Query MUST contain at least a single 'constraints'\n");
message.append("Parsed Query:\n");
message.append(jQuery.toString(4));
log.warn(message.toString());
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(message.toString()).header(HttpHeaders.ACCEPT, acceptedMediaType.toString()).build());
}
JSONArray constraints = jQuery.getJSONArray("constraints");
// collect all parsing Errors to report a complete set of all errors
boolean parsingError = false;
StringBuilder parsingErrorMessages = new StringBuilder();
parsingErrorMessages.append("Constraint parsing Errors:\n");
for (int i = 0; i < constraints.length(); i++) {
JSONObject jConstraint = constraints.getJSONObject(i);
if (jConstraint.has("field")) {
String field = jConstraint.getString("field");
// check if there is already a constraint for that field
if (field == null || field.isEmpty()) {
parsingErrorMessages.append('\n');
parsingErrorMessages.append("Each Field Query Constraint MUST define a value for 'field'\n");
parsingErrorMessages.append("Parsed Constraint:\n");
parsingErrorMessages.append(jConstraint.toString(4));
parsingErrorMessages.append('\n');
parsingError = true;
continue;
}
String fieldUri = nsPrefixService.getFullName(field);
if (fieldUri == null) {
parsingErrorMessages.append('\n');
parsingErrorMessages.append("The 'field' '").append(field).append("uses an unknown namespace prefix '");
parsingErrorMessages.append(NamespaceMappingUtils.getPrefix(field)).append("'\n");
parsingErrorMessages.append("Parsed Constraint:\n");
parsingErrorMessages.append(jConstraint.toString(4));
parsingErrorMessages.append('\n');
parsingError = true;
continue;
} else if (query.isConstrained(fieldUri)) {
parsingErrorMessages.append('\n');
parsingErrorMessages.append("The parsed Query defines multiple constraints fr the field '").append(fieldUri).append("'!\n");
parsingErrorMessages.append("FieldQuery allows only a single Constraint for a field\n");
parsingErrorMessages.append("Parsed Constraints:\n");
parsingErrorMessages.append(constraints.toString(4));
parsingErrorMessages.append('\n');
parsingError = true;
continue;
} else {
try {
query.setConstraint(fieldUri, parseConstraint(jConstraint, nsPrefixService));
} catch (IllegalArgumentException e) {
parsingErrorMessages.append('\n');
parsingErrorMessages.append(e.getMessage());
parsingErrorMessages.append('\n');
parsingError = true;
continue;
}
}
} else {
// empty field
parsingErrorMessages.append('\n');
parsingErrorMessages.append("Constraints MUST define a value for 'field'\n");
parsingErrorMessages.append("Parsed Constraint:\n");
parsingErrorMessages.append(jConstraint.toString(4));
parsingErrorMessages.append('\n');
parsingError = true;
continue;
}
}
if (parsingError) {
String message = parsingErrorMessages.toString();
log.warn(message);
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(message).header(HttpHeaders.ACCEPT, acceptedMediaType.toString()).build());
}
// parse selected fields
JSONArray selected = jQuery.optJSONArray("selected");
if (selected != null) {
for (int i = 0; i < selected.length(); i++) {
String selectedField = selected.getString(i);
selectedField = nsPrefixService.getFullName(selectedField);
if (selectedField != null && !selectedField.isEmpty()) {
query.addSelectedField(selectedField);
}
}
}
// parse limit and offset
if (jQuery.has("limit") && !jQuery.isNull("limit")) {
try {
query.setLimit(jQuery.getInt("limit"));
} catch (JSONException e) {
parsingErrorMessages.append('\n');
parsingErrorMessages.append("Property \"limit\" MUST BE a valid integer number!\n");
parsingErrorMessages.append("Parsed Value:");
parsingErrorMessages.append(jQuery.get("init"));
parsingErrorMessages.append('\n');
parsingError = true;
}
}
if (jQuery.has("offset") && !jQuery.isNull("offset")) {
try {
query.setOffset(jQuery.getInt("offset"));
} catch (JSONException e) {
parsingErrorMessages.append('\n');
parsingErrorMessages.append("Property \"offset\" MUST BE a valid integer number!\n");
parsingErrorMessages.append("Parsed Value:");
parsingErrorMessages.append(jQuery.get("init"));
parsingErrorMessages.append('\n');
parsingError = true;
}
}
return query;
}
Aggregations