@SuppressWarnings("nls") private GetFieldDataResult processRequest( GetFieldDataRequest request, Map<Integer, Model> modelLookupMap, List<Header> headers) { try { GetFieldDataResult fullResult = new GetFieldDataResult(); for (FieldDataCriteria fieldDataCriteria : request.getFieldDataCriteria()) { for (FieldSelection fieldSelection : fieldDataCriteria.getFieldSelection()) { if (fieldSelection.getFieldIdentifier().getSource() == null) throw new UnsupportedOperationException( "fieldSelection.getField().getFieldIdentifier().getSource()=null not supported"); String sourceName = fieldSelection.getFieldIdentifier().getSource(); if (sourceName == null) throw new UnsupportedOperationException( "fieldSelection.getField().getFieldIdentifier().getSource()=" + sourceName + " not supported"); if (sourceName.equals(FieldSourceEnum.PREDIX_ASSET.name())) { fieldSelection.getFieldIdentifier().setSource("handler/assetGetFieldDataHandler"); processSingleCustomHandler( request, headers, fullResult, fieldDataCriteria, fieldSelection); } else if (sourceName.equals(FieldSourceEnum.PREDIX_TIMESERIES.name())) { fieldSelection.getFieldIdentifier().setSource("handler/timeseriesGetDataHandler"); processSingleCustomHandler( request, headers, fullResult, fieldDataCriteria, fieldSelection); } else if (sourceName.contains("/handler")) { processSingleCustomHandler( request, headers, fullResult, fieldDataCriteria, fieldSelection); } else if (sourceName.startsWith("http://") || sourceName.startsWith("https://")) { processRESTRequest(request, headers, fullResult, fieldDataCriteria, fieldSelection); } else throw new UnsupportedOperationException( "fieldSelection.getField().getFieldIdentifier().getSource()=" + sourceName + " not supported"); } } return fullResult; } catch (IllegalStateException e) { throw new RuntimeException("error in getFieldData", e); } }
@SuppressWarnings("nls") private void processRESTRequest( GetFieldDataRequest request, List<Header> headers, GetFieldDataResult fullResult, FieldDataCriteria fieldDataCriteria, FieldSelection fieldSelection) { GetFieldDataResult singleResult = null; GetFieldDataRequest singleRequest = makeSingleRequest(request, fieldDataCriteria, fieldSelection); String url = fieldSelection.getFieldIdentifier().getSource(); String method = fieldSelection.getFieldIdentifier().getName(); if (url == null || !url.startsWith("http://") || !url.startsWith("https://")) throw new UnsupportedOperationException( "please set the source to the url of REST service e.g. https://myservice.com"); if ("POST".equals(method.toUpperCase())) { EntityBuilder builder = EntityBuilder.create(); builder.setText(this.mapper.toJson(singleRequest)); HttpEntity reqEntity = builder.build(); try (CloseableHttpResponse response = this.restClient.post(url, reqEntity, null, 100, 1000); ) { String res = this.restClient.getResponse(response); singleResult = this.mapper.fromJson(res, GetFieldDataResult.class); } catch (IOException e) { throw new RuntimeException("Error when performing POST to Custom Rest Service : ", e); } } else if ("GET".equals(method.toUpperCase())) { try (CloseableHttpResponse response = this.restClient.get(url, null, 100, 1000); ) { String res = this.restClient.getResponse(response); singleResult = this.mapper.fromJson(res, GetFieldDataResult.class); } catch (IOException e) { throw new RuntimeException("Error when performing GET to Custom Rest Service : ", e); } } fullResult.getFieldData().addAll(singleResult.getFieldData()); }
/** * @param request * @param headers * @param fullResult * @param fieldDataCriteria * @param fieldSelection - */ @SuppressWarnings("nls") private void processSingleCustomHandler( GetFieldDataRequest request, List<Header> headers, GetFieldDataResult fullResult, FieldDataCriteria fieldDataCriteria, FieldSelection fieldSelection) { GetFieldDataRequest singleRequest = makeSingleRequest(request, fieldDataCriteria, fieldSelection); String source = fieldSelection.getFieldIdentifier().getSource(); if (!source.contains("handler/")) throw new UnsupportedOperationException( "please add the name of Spring Bean after handler e.g. handler/sampleHandler"); String beanName = source.substring(source.indexOf("handler/") + 8); beanName = beanName.substring(0, 1).toLowerCase() + beanName.substring(1); GetDataHandler bean = (GetDataHandler) this.context.getBean(beanName); Map<Integer, Model> modelLookupMap = new HashMap<Integer, Model>(); GetFieldDataResult singleResult = bean.getData(singleRequest, modelLookupMap, headers); fullResult.getFieldData().addAll(singleResult.getFieldData()); }