private void preparedRest() throws Exception { Pattern pattern = Pattern.compile("values(\\s*)\\((.+?)\\)"); Matcher matcher = pattern.matcher(statement.getQuery()); String[] columnsArrys = null; while (matcher.find()) { String columnsStr = matcher.group(2).replace(" ", ""); columnsArrys = columnsStr.split(",(?=([^\']*\'[^\']*\')*[^\']*$)", -1); } for (int i = 0; i < columnsArrys.length; i++) { columnsArrys[i] = columnsArrys[i].substring(1, columnsArrys[i].length() - 1); } if (columnsArrys.length < 3 && columnsArrys.length % 3 != 0) { throw new Exception("invalid rest format"); } Pattern p = Pattern.compile("\\[\\:(.+?)\\]"); for (int i = 0; i < columnsArrys.length / 3; i++) { RestInfo restInfo = new RestInfo(); restInfo.setCmd(RestCmd.valueOf(columnsArrys[i * 3])); restInfo.setUrl(columnsArrys[i * 3 + 1]); restInfo.setBody("".equals(columnsArrys[i * 3 + 2]) ? null : columnsArrys[i * 3 + 2]); List<BaseBindColumnDef> columns = new ArrayList<BaseBindColumnDef>(); String valQuery = columnsArrys[i * 3 + 1]; Matcher m = p.matcher(valQuery); while (m.find()) { String bindName = m.group(1); BaseBindColumnDef bindColumn = new BaseBindColumnDef(); bindColumn.setName(bindName); columns.add(bindColumn); } restInfo.setColumns(columns); restInfos.add(restInfo); } }
private void restCall(Tuple tuple) throws ExecutionException, InterruptedException { Future<Response> fureRet = null; for (RestInfo restInfo : restInfos) { String url = restInfo.getUrl(); String body = restInfo.getBody(); for (BaseBindColumnDef bindColumn : restInfo.getColumns()) { url = url.replace( "[:" + bindColumn.getName() + "]", tuple.getStringByField(bindColumn.getName())); if (body != null) { body = body.replace( "[:" + bindColumn.getName() + "]", tuple.getStringByField(bindColumn.getName())); } } AsyncHttpClient.BoundRequestBuilder boundRequestBuilder = null; if (RestCmd.PUT.equals(restInfo.getCmd())) { boundRequestBuilder = asyncHttpClient.preparePut(url); } else if (RestCmd.GET.equals(restInfo.getCmd())) { boundRequestBuilder = asyncHttpClient.prepareGet(url); } else if (RestCmd.DELETE.equals(restInfo.getCmd())) { boundRequestBuilder = asyncHttpClient.prepareDelete(url); } else if (RestCmd.POST.equals(restInfo.getCmd())) { boundRequestBuilder = asyncHttpClient.preparePost(url); } else { break; } if (body != null) { fureRet = boundRequestBuilder.setBody(body).execute(); } else { fureRet = boundRequestBuilder.execute(); } fureRet.get(); } }
public static void main(String[] args) { // TODO Auto-generated method stub Dummy d = RestInfo.buildProxy(Dummy.class, "http://localhost:8080/api"); d.what().the(); }