public static Operator getOperator(String requestedToken) throws SyntaxException { for(Operator op : values()) { if(op.token.equalsIgnoreCase(requestedToken)) { return op; } } throw new SyntaxException("Unidentified token"); } }
public static List<OrderParam> getOrderParams(String orderBy) throws SyntaxException { if(StringUtils.stringNotEmpty(orderBy)) { Matcher orderMatcher = ORDERBY_PATTERN.matcher(orderBy); List<OrderParam> orderParams = new ArrayList<>(); while(orderMatcher.find()) { String ascendingField = orderMatcher.group(2); String descendingField = orderMatcher.group(4); String noSortDirectionField = orderMatcher.group(6); if(StringUtils.stringNotEmpty(ascendingField)) { orderParams.add(new OrderParam(ascendingField, OrderParam.DIRECTION.ASC)); } else if(StringUtils.stringNotEmpty(descendingField)) { orderParams.add(new OrderParam(descendingField, OrderParam.DIRECTION.DESC)); } else if(StringUtils.stringNotEmpty(noSortDirectionField)) { orderParams.add(new OrderParam(noSortDirectionField, OrderParam.DIRECTION.ASC)); } else { throw new SyntaxException("Unexpected order by syntax: " + orderBy); } } return orderParams; } else { return Collections.emptyList(); } } }
throw new SyntaxException("Invalid path syntax: " + pathElement); throw new SyntaxException("The path element contains invalid block open or close brackets: " + pathElement); } else { inputElement = new InputElement(simpleElementName); throw new SyntaxException("Invalid element syntax: " + pathElement); pathElements.add(inputElement); } else { throw new SyntaxException("Invalid path syntax: " + pathElement);
@RequestMapping(value = "/Instances({instanceId})/Bags({bagName})/Entities({entityId})", produces = "application/json", method = DELETE) public RestEntity removeEntry(@PathVariable String instanceId, @PathVariable String bagName, @PathVariable String entityId, HttpServletRequest request) throws RestException { if(stringNotEmpty(instanceId) && stringNotEmpty(bagName) && stringNotEmpty(entityId)) { try { LOG.debug("Doing remove of entity with id: {}", entityId); StorageService storageService = storageServiceFactory.getOrCreateStorageService(instanceId, bagName); storageService.removeEntity(getRequestContext(request), entityId); return null; } catch(JasDBStorageException e) { throw new RestException("Unable to remove entity: " + e.getMessage()); } } else { throw new SyntaxException("Cannot remove entity, invalid parameters for delete operation"); } }
private InputCondition blockStart(InputCondition previous) throws SyntaxException { TokenType previousTokenType = previous.getTokenType(); if(isBlockOperation(previousTokenType)) { BlockOperation blockOperation = (BlockOperation) previous; InputCondition blockResult = parse(true); blockOperation.addInputCondition(blockResult); return blockOperation; } else { throw new SyntaxException("Invalid Query syntax, block started without operator"); } }
private InputCondition decodeFieldCondition(String token) throws SyntaxException { TokenType currentTokenType = TokenType.getTokenType(token); if(currentTokenType == TokenType.LITERAL) { String operatorToken = scanner.nextToken(); if(operatorToken != null) { return decodeFieldOperatorCondition(token, operatorToken); } else { return new FieldCondition(FieldCondition.ID_PARAM, token); } } else { throw new SyntaxException("Unexpected token type: " + token); } }
return parentCondition; } else { throw new SyntaxException("Unexpected block end detected"); return parentCondition; } else { throw new SyntaxException("Unexpected end of input, expected block close");
private InputCondition decodeFieldOperatorCondition(String fieldToken, String operatorToken) throws SyntaxException { TokenType operatorTokenType = TokenType.getTokenType(operatorToken); if(operatorTokenType.isFieldOperator()) { String valueToken = scanner.nextToken(); TokenType valueTokenType = TokenType.getTokenType(valueToken); if(valueTokenType == TokenType.EQUALS) { valueToken = scanner.nextToken(); valueTokenType = TokenType.getTokenType(valueToken); if(valueToken != null && valueTokenType == TokenType.LITERAL) { operatorToken = operatorToken + TokenType.EQUALS.getToken(); return new FieldCondition(fieldToken, Operator.getOperator(operatorToken), decodeValue(valueToken)); } else { throw new SyntaxException("Unexpected token: " + valueToken); } } else if(valueToken != null && valueTokenType == TokenType.LITERAL) { return new FieldCondition(fieldToken, Operator.getOperator(operatorToken), decodeValue(valueToken)); } else if(valueToken == null) { throw new SyntaxException("No field value specified"); } else { throw new SyntaxException("Unexpected token: " + valueTokenType); } } else { throw new SyntaxException("Unexpected token: " + operatorToken); } }
private InputCondition handleBlockOperation(TokenType type, InputCondition previous) throws SyntaxException { if(previous != null) { TokenType previousTokenType = previous.getTokenType(); if(previousTokenType == TokenType.LITERAL) { FieldCondition condition = (FieldCondition) previous; BlockOperation blockOperation = createBlockOperation(type); blockOperation.addInputCondition(condition); return blockOperation; } else if (isBlockOperation(previousTokenType)) { BlockOperation previousBlockOperation = (BlockOperation) previous; if(previousBlockOperation.getTokenType() == type) { return previousBlockOperation; } else { BlockOperation blockOperation = createBlockOperation(type); blockOperation.addInputCondition(previousBlockOperation); return blockOperation; } } else { throw new SyntaxException("Invalid Query syntax, invalid token before operator: " + type); } } else { throw new SyntaxException("Invalid Query syntax, cannot start with operator: " + type); } }
private InputCondition handleLiteral(String token, InputCondition parent) throws SyntaxException { if(parent != null) { TokenType previousTokenType = parent.getTokenType(); if(isBlockOperation(previousTokenType)) { BlockOperation blockOperation = (BlockOperation) parent; blockOperation.addInputCondition(decodeFieldCondition(token)); return blockOperation; } else { throw new SyntaxException("Invalid Query syntax, token: " + token + " unexpected"); } } else { return decodeFieldCondition(token); } }
private RestEntity doModificationOperation(String instanceId, String bagName, String rawData, RequestContext context, OPERATION_TYPE type) throws RestException { if(stringNotEmpty(instanceId) && stringNotEmpty(bagName)) { LOG.debug("Raw entity data received: {}", rawData); StreamedEntity streamedEntity = ENTITY_HANDLER.deserialize(StreamedEntity.class, rawData); Entity storeEntity = streamedEntity.getEntity(); try { StorageService storageService = storageServiceFactory.getOrCreateStorageService(instanceId, bagName); if(type == OPERATION_TYPE.UPDATE) { LOG.debug("Updating entity with id: {}", storeEntity.getInternalId()); storageService.persistEntity(context, storeEntity); } else if(type == OPERATION_TYPE.INSERT) { LOG.debug("Inserting new entity into bag: {}", bagName); storageService.insertEntity(context, storeEntity); } return new StreamedEntity(storeEntity); } catch(JasDBStorageException e) { throw new RestException("Unable to store entity: " + e.getMessage()); } } else { throw new SyntaxException("Cannot store entity, no Bag specified"); } }