private Format getStructuredQueryFormat(HandleImplementation baseHandle) { Format payloadFormat = baseHandle.getFormat(); if (payloadFormat == Format.UNKNOWN) { payloadFormat = null; } else if (payloadFormat != Format.XML && payloadFormat != Format.JSON) { throw new IllegalArgumentException( "Cannot perform raw search for format "+payloadFormat.name()); } return payloadFormat; }
public void init(RawQueryDefinition rawQuery, QueryOptionsWriteHandle options, String qtext, String sparql) { this.rawQuery = rawQuery; this.options = options; this.qtext = qtext; this.sparql = sparql; // if a query has been supplied, it's either in JSON or in XML if (rawQuery != null) { this.format = HandleAccessor.as(rawQuery.getHandle()).getFormat(); } else { if (options != null) { this.format = HandleAccessor.as(options).getFormat(); } else { // there's only qtext -- we choose format. this.format = Format.JSON; } } if ( format != Format.XML && format != Format.JSON ) { throw new IllegalArgumentException("Format of rawQuery must be XML or JSON"); } }
private void setRdfXmlOrJsonMimetype(TriplesReadHandle handle) { HandleImplementation baseHandle = HandleAccessor.as(handle); if ( baseHandle.getFormat() == Format.JSON ) { if ( Format.JSON.getDefaultMimetype().equals(baseHandle.getMimetype()) ) { baseHandle.setMimetype(RDFMimeTypes.RDFJSON); } } else if ( baseHandle.getFormat() == Format.XML ) { if ( Format.XML.getDefaultMimetype().equals(baseHandle.getMimetype())) { baseHandle.setMimetype(RDFMimeTypes.RDFXML); } } }
@SuppressWarnings("rawtypes") @Override public void writeOptions(String name, QueryOptionsWriteHandle queryOptionsHandle) throws ResourceNotFoundException, ResourceNotResendableException, ForbiddenUserException, FailedRequestException { HandleImplementation queryOptionsBase = HandleAccessor.checkHandle( queryOptionsHandle, "query options"); if (queryOptionsBase == null) throw new IllegalArgumentException("Could not write null options: " + name); Format queryOptionsFormat = queryOptionsBase.getFormat(); switch (queryOptionsFormat) { case UNKNOWN: queryOptionsFormat = Format.XML; break; case JSON: case XML: break; default: throw new UnsupportedOperationException( "Only JSON and XML query options are possible."); } String mimetype = queryOptionsFormat.getDefaultMimetype(); services.putValue(requestLogger, QUERY_OPTIONS_BASE, name, mimetype, queryOptionsBase); }
@SuppressWarnings("rawtypes") private void checkContentFormat(HandleImplementation contentBase) { if (contentBase == null) return; if (contentFormat != null && contentFormat != Format.UNKNOWN) { Format currFormat = contentBase.getFormat(); if (currFormat != contentFormat) { contentBase.setFormat(contentFormat); if (currFormat != Format.UNKNOWN) contentBase.setMimetype(contentFormat.getDefaultMimetype()); } } } protected RequestParameters mergeTransformParameters(ServerTransform transform, RequestParameters extraParams) {
@SuppressWarnings("rawtypes") private void checkContentFormat(HandleImplementation contentBase) { if (contentBase == null) return; if (contentFormat != null && contentFormat != Format.UNKNOWN) { Format currFormat = contentBase.getFormat(); if (currFormat != contentFormat) { contentBase.setFormat(contentFormat); if (currFormat != Format.UNKNOWN) contentBase.setMimetype(contentFormat.getDefaultMimetype()); } } }
@Override public <T extends StructureReadHandle> T listServices(T listHandle, boolean refresh) { if (listHandle == null) throw new IllegalArgumentException("null handle for listing resource services"); if (logger.isInfoEnabled()) logger.info("Reading resource services list"); HandleImplementation listBase = HandleAccessor.checkHandle(listHandle, "resource"); Format listFormat = listBase.getFormat(); if (!(Format.JSON == listFormat || Format.XML == listFormat)) throw new IllegalArgumentException( "list handle for unsupported format: "+listFormat.getClass().getName()); RequestParameters extraParams = null; if (!refresh) { extraParams = new RequestParameters(); extraParams.put("refresh", "false"); } listBase.receiveContent( services.getValues(requestLogger, "config/resources", extraParams, listFormat.getDefaultMimetype(), listBase.receiveAs()) ); return listHandle; }
@Override public <T extends StructureReadHandle> T listTransforms(T listHandle, boolean refresh) throws ForbiddenUserException, FailedRequestException { if (listHandle == null) throw new IllegalArgumentException("Reading transform list with null handle"); if (logger.isInfoEnabled()) logger.info("Reading transform list"); HandleImplementation listBase = HandleAccessor.checkHandle(listHandle, "transform"); Format listFormat = listBase.getFormat(); if (!(Format.JSON == listFormat || Format.XML == listFormat)) throw new IllegalArgumentException( "list handle for unsupported format: "+listFormat.getClass().getName()); RequestParameters extraParams = null; if (!refresh) { extraParams = new RequestParameters(); extraParams.put("refresh", "false"); } listBase.receiveContent( services.getValues(requestLogger, "config/transforms", extraParams, listFormat.getDefaultMimetype(), listBase.receiveAs()) ); return listHandle; }
@SuppressWarnings("rawtypes") @Override public <T extends RuleListReadHandle> T match(StructureWriteHandle document, String[] candidateRules, T ruleListHandle, ServerTransform transform) { HandleImplementation searchBase = HandleAccessor.checkHandle(document, "match"); Format searchFormat = searchBase.getFormat(); switch(searchFormat) { case UNKNOWN: searchFormat = Format.XML; break; case JSON: case XML: break; default: throw new UnsupportedOperationException("Only XML and JSON queries can filter for rule matches."); } String mimeType = searchFormat.getDefaultMimetype(); HandleAccessor.receiveContent(ruleListHandle, services.match(document, candidateRules, mimeType, transform)); return ruleListHandle; }
@Override public <T extends StructureReadHandle> T listServices(T listHandle, boolean refresh) { if (listHandle == null) throw new IllegalArgumentException("null handle for listing resource services"); if (logger.isInfoEnabled()) logger.info("Reading resource services list"); HandleImplementation listBase = HandleAccessor.checkHandle(listHandle, "resource"); Format listFormat = listBase.getFormat(); if (!(Format.JSON == listFormat || Format.XML == listFormat)) throw new IllegalArgumentException( "list handle for unsupported format: "+listFormat); RequestParameters extraParams = null; if (!refresh) { extraParams = new RequestParameters(); extraParams.put("refresh", "false"); } listBase.receiveContent( services.getValues(requestLogger, "config/resources", extraParams, listFormat.getDefaultMimetype(), listBase.receiveAs()) ); return listHandle; }
@SuppressWarnings("rawtypes") @Override public <T extends RuleListReadHandle> T match(StructureWriteHandle document, String[] candidateRules, T ruleListHandle, ServerTransform transform) { HandleImplementation searchBase = HandleAccessor.checkHandle(document, "match"); Format searchFormat = searchBase.getFormat(); switch(searchFormat) { case UNKNOWN: searchFormat = Format.XML; break; case JSON: case XML: break; default: throw new UnsupportedOperationException("Only XML and JSON queries can filter for rule matches."); } String mimeType = searchFormat.getDefaultMimetype(); HandleAccessor.receiveContent(ruleListHandle, services.match(document, candidateRules, mimeType, transform)); return ruleListHandle; }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public <T extends RuleReadHandle> T readRule(String ruleName, T ruleHandle) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException { if (ruleName == null) { throw new IllegalArgumentException("Cannot read null rule name"); } HandleImplementation ruleBase = HandleAccessor.checkHandle(ruleHandle, "rule"); Format ruleFormat = ruleBase.getFormat(); switch (ruleFormat) { case UNKNOWN: ruleFormat = Format.XML; break; case JSON: case XML: break; default: throw new UnsupportedOperationException( "Only JSON and XML rules are possible."); } String mimetype = ruleFormat.getDefaultMimetype(); ruleBase.receiveContent(services.getValue(requestLogger, RULES_BASE, ruleName, false, mimetype, ruleBase.receiveAs())); return ruleHandle; }
private RequestBody makeRequestBody(AbstractWriteHandle document) { if (document == null) { return new EmptyRequestBody(); } HandleImplementation handleBase = HandleAccessor.as(document); Format format = handleBase.getFormat(); String mimetype = (format == Format.BINARY) ? "application/x-unknown-content-type" : handleBase.getMimetype(); MediaType mediaType = MediaType.parse(mimetype); return (document instanceof OutputStreamSender) ? new StreamingOutputImpl((OutputStreamSender) document, null, mediaType) : new ObjectRequestBody(HandleAccessor.sendContent(document), mediaType); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public <T extends QueryOptionsListReadHandle> T optionsList(T optionsHandle) throws ForbiddenUserException, FailedRequestException { HandleImplementation optionsBase = HandleAccessor.checkHandle(optionsHandle, "optionslist"); Format optionsFormat = optionsBase.getFormat(); switch(optionsFormat) { case UNKNOWN: optionsFormat = Format.XML; break; case JSON: case XML: break; default: throw new UnsupportedOperationException("Only XML and JSON options list results are possible."); } String mimetype = optionsFormat.getDefaultMimetype(); optionsBase.receiveContent(services.optionsList(optionsBase.receiveAs(), mimetype, null)); return optionsHandle; } }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public <T extends QueryOptionsListReadHandle> T optionsList(T optionsHandle) throws ForbiddenUserException, FailedRequestException { HandleImplementation optionsBase = HandleAccessor.checkHandle(optionsHandle, "optionslist"); Format optionsFormat = optionsBase.getFormat(); switch(optionsFormat) { case UNKNOWN: optionsFormat = Format.XML; break; case JSON: case XML: break; default: throw new UnsupportedOperationException("Only XML and JSON options list results are possible."); } String mimetype = optionsFormat.getDefaultMimetype(); optionsBase.receiveContent(services.optionsList(optionsBase.receiveAs(), mimetype, null)); return optionsHandle; } }
@SuppressWarnings("unchecked") public <T extends StructureReadHandle> T convertOrValidate(RawQueryByExampleDefinition query, T convertedHandle, String view) { RequestParameters params = new RequestParameters(); params.add("view", view); @SuppressWarnings("rawtypes") HandleImplementation convertedBase = HandleAccessor.checkHandle(convertedHandle, "convert"); Format convertedFormat = convertedBase.getFormat(); if(convertedFormat == Format.UNKNOWN ) { @SuppressWarnings("rawtypes") HandleImplementation queryBase = HandleAccessor.checkHandle(query.getHandle(), "validate"); if (queryBase.getFormat() == Format.UNKNOWN) { convertedBase.setFormat(Format.XML); } } String optionsName = query.getOptionsName(); if (optionsName != null && optionsName.length() > 0) { params.add("options", optionsName); } services.postResource(requestLogger, "qbe", null, params, query.getHandle(), convertedHandle); return convertedHandle; }
@SuppressWarnings("unchecked") @Override public <T extends ValuesListReadHandle> T valuesList(ValuesListDefinition valdef, T valuesHandle, Transaction transaction) { @SuppressWarnings("rawtypes") HandleImplementation valuesBase = HandleAccessor.checkHandle(valuesHandle, "valueslist"); Format valuesFormat = valuesBase.getFormat(); switch(valuesFormat) { case UNKNOWN: valuesFormat = Format.XML; break; case JSON: case XML: break; default: throw new UnsupportedOperationException("Only XML and JSON values list results are possible."); } String mimetype = valuesFormat.getDefaultMimetype(); String tid = transaction == null ? null : transaction.getTransactionId(); valuesBase.receiveContent(services.valuesList(valuesBase.receiveAs(), valdef, mimetype, tid)); return valuesHandle; }
@SuppressWarnings("unchecked") @Override public <T extends QueryOptionsListReadHandle> T optionsList(T optionsHandle, Transaction transaction) { @SuppressWarnings("rawtypes") HandleImplementation optionsBase = HandleAccessor.checkHandle(optionsHandle, "optionslist"); Format optionsFormat = optionsBase.getFormat(); switch(optionsFormat) { case UNKNOWN: optionsFormat = Format.XML; break; case JSON: case XML: break; default: throw new UnsupportedOperationException("Only XML and JSON options list results are possible."); } String mimetype = optionsFormat.getDefaultMimetype(); String tid = transaction == null ? null : transaction.getTransactionId(); optionsBase.receiveContent(services.optionsList(optionsBase.receiveAs(), mimetype, tid)); return optionsHandle; }
@SuppressWarnings("unchecked") @Override public <T extends QueryOptionsListReadHandle> T optionsList(T optionsHandle, Transaction transaction) { @SuppressWarnings("rawtypes") HandleImplementation optionsBase = HandleAccessor.checkHandle(optionsHandle, "optionslist"); Format optionsFormat = optionsBase.getFormat(); switch(optionsFormat) { case UNKNOWN: optionsFormat = Format.XML; break; case JSON: case XML: break; default: throw new UnsupportedOperationException("Only XML and JSON options list results are possible."); } String mimetype = optionsFormat.getDefaultMimetype(); String tid = transaction == null ? null : transaction.getTransactionId(); optionsBase.receiveContent(services.optionsList(optionsBase.receiveAs(), mimetype, transaction)); return optionsHandle; }
@SuppressWarnings("unchecked") @Override public <T extends ValuesListReadHandle> T valuesList(ValuesListDefinition valdef, T valuesHandle, Transaction transaction) { @SuppressWarnings("rawtypes") HandleImplementation valuesBase = HandleAccessor.checkHandle(valuesHandle, "valueslist"); Format valuesFormat = valuesBase.getFormat(); switch(valuesFormat) { case UNKNOWN: valuesFormat = Format.XML; break; case JSON: case XML: break; default: throw new UnsupportedOperationException("Only XML and JSON values list results are possible."); } String mimetype = valuesFormat.getDefaultMimetype(); String tid = transaction == null ? null : transaction.getTransactionId(); valuesBase.receiveContent(services.valuesList(valuesBase.receiveAs(), valdef, mimetype, transaction)); return valuesHandle; }