@Override public String toString(){ // Get a string to represent our internal property state. Multimap<String, String> entries = new CaseInsensitiveKeyMultimap<>(); entries.put("api.mode", getMode()); entries.put("api.webservice.url", getWebServiceUrl()); entries.put("api.direct.repository", getRepository()); entries.put("api.web.useget", String.valueOf(getUseHttpGet())); entries.put("api.web.httpclientbuilderclass", getHttpClientBuilderClass()); entries.put("api.streams.cache", String.valueOf(getStreamsCacheEnabled())); entries.put("api.admin.basedata", String.valueOf(getAdminBaseDataEnabled())); return StringHelper.mapToHtml(entries); }
@After public void after() { if (userContext == null || !apiProperties.isInApiMode(ApiProperties.ApiMode.direct) || !apiProperties.isInRepository(ApiProperties.ApiDirectRepository.mysql)) { return; } cleanupDatabase(); }
@Override protected URIBuilder createBaseUrl(WebApiAction apiAction) { StringBuilder sb = new StringBuilder(getApiProperties().getWebServiceUrl()); if(!sb.toString().endsWith("/")) sb.append("/"); //since we are in policy api, then obviously this is a policy endpoint! sb.append(apiAction.toValue()); String uri = sb.toString(); try { return new URIBuilder(uri); } catch (URISyntaxException e) { getLogger().error("Problem with URI - syntax was invalid: " + uri, e); throw new RuntimeException("Problem with URI - syntax was invalid: " + uri, e); } }
/** * getSession has adminApi validation as well, but I have copy here to throw early. */ private void validateAllowAdministration(){ if ( !apiProperties.getAdminBaseDataEnabled() ) { throw new RuntimeException("Administration Api is currently disabled."); } }
if(apiProperties.getMode()!=null){ if(!apiProperties.getMode().equalsIgnoreCase("none")){ configurableEnvironment.addActiveProfile("api"); if(apiProperties.getMode().equalsIgnoreCase("web")){ configurableEnvironment.addActiveProfile("apiweb"); if(apiProperties.getMode().equalsIgnoreCase("direct")){ configurableEnvironment.addActiveProfile("apidirect"); if(apiProperties.getRepository()!=null){ if(apiProperties.getRepository().equalsIgnoreCase("h2")) { configurableEnvironment.addActiveProfile("h2"); if(apiProperties.getRepository().equalsIgnoreCase("hibernate")) { configurableEnvironment.addActiveProfile("apihibernate"); configurableEnvironment.addActiveProfile("administrationusercontext");
/*** * Helper which takes a single ApiMode, and checks if we are running in that mode. * @param mode * @return */ public boolean isInApiMode(ApiMode mode) { final ApiMode currentMode = ApiMode.forValue(getMode()); return (currentMode == mode); }
/*** * This is an ORd result, so if we are in any of the modes supplied we return true. */ public boolean isInApiMode( Collection<ApiMode> modes ) { for ( ApiProperties.ApiMode mode : modes ) { if (isInApiMode(mode)) return true; } return false; }
HttpClientBuilder getHttpClientBuilder(){ if(httpClientBuilder==null){ synchronized (WebApiBase.class) { if (httpClientBuilder == null) { if (!Strings.isNullOrEmpty(apiProperties.getHttpClientBuilderClass())){ try { Class<?> httpClientBuilderClass = Class.forName(apiProperties.getHttpClientBuilderClass()); httpClientBuilder = (HttpClientBuilder)httpClientBuilderClass.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } } else{ httpClientBuilder = HttpClientBuilder.create();//.setProxy(new HttpHost("localhost", 8888)); } } } } return httpClientBuilder; }
/** * Creates a GET or POST request depending on configuration. * @return Either a GET or POST request. */ private HttpUriRequest createRequest(URIBuilder builder, Collection<NameValuePair> params) throws URISyntaxException, UnsupportedEncodingException { Collection<NameValuePair> p = getApiParams(); params.addAll(p); if(apiProperties.getUseHttpGet()) { for (NameValuePair nvp : params){ builder.addParameter(nvp.getName(), nvp.getValue()); } URI uri = builder.build(); getLogger().trace("CREATING GET: " + URLDecoder.decode(uri.toString(), "UTF-8")); return new HttpGet(uri); } else { URI uri = builder.build(); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, Charset.forName("utf-8")); StringWriter writer = new StringWriter(); try { IOUtils.copy(formEntity.getContent(), writer, "utf-8"); } catch (IOException e) { getLogger().debug("Error when writing POST entity to string: " + e.getMessage(), e); } getLogger().trace("CREATING POST: " + URLDecoder.decode(uri.toString(), "UTF-8") + "?" + URLDecoder.decode(writer.toString(), "UTF-8")); HttpPost post = new HttpPost(uri); post.setEntity(formEntity); return post; } }
public boolean isInRepository( ApiDirectRepository repository ) { final ApiDirectRepository currentMode = ApiDirectRepository.forValue(getRepository()); return (currentMode == repository); }
/** * Regardless of the format of the item, force the information is returned as string! * * @return */ public String getAsString() { // // This should only be used on objects which are not yet stream aware if (!hasStream) { // otherwise its just a string - return it. return getStringValue(); } // the stream needs to be turned into a string. Only possibly used when converting // to interfaces which only contain strings, such as setTerms or the like. // N.B. now adding a configurable cache of the stream, which if converted to a string // once we hold onto this for the duration of the evaluation. if (!apiProperties.getStreamsCacheEnabled()) { return asString(getStreamValue()); } if (haveCachedStream) { return getStringValue(); } // otherwise we have cache enabled, but haven't yet converted it to a string. // get the string from the stream, and indicate we have done this. String strValue = asString(getStreamValue()); setStringValue(strValue); haveCachedStream = true; return strValue; }
@Override protected URIBuilder createBaseUrl(WebApiAction apiAction) { StringBuilder sb = new StringBuilder(getApiProperties().getWebServiceUrl()); if(!sb.toString().endsWith("/")) sb.append("/"); //since we are in classification api, then obviously this is a classification endpoint! sb.append("classification") .append("/") .append(apiAction.toValue()); String uri = sb.toString(); try { return new URIBuilder(uri); } catch (URISyntaxException e) { getLogger().error("Problem with URI - syntax was invalid: " + uri, e); throw new RuntimeException("Problem with URI - syntax was invalid: " + uri, e); } }
private void validateAllowAdministration(boolean allowAdministration){ if ( allowAdministration && !apiProperties.getAdminBaseDataEnabled() ) { throw new RuntimeException("Administration Api is currently disabled."); } }
public IntegrationTestBase() { // in certain cases we now have to shutdown the main ApiApplicationInstance as once a profile has been activated // it doesn't remove itself, it has to be rebuilt. As such a quick request here will ensure we have a new context. // if it has been removed. if (genericApplicationContext == null) { genericApplicationContext = new CorePolicyApplicationContext(); genericApplicationContext.refresh(); } apiProperties = genericApplicationContext.getBean(ApiProperties.class); try { // let our deployment type setup the usercontext correctly. depending on iod/web switch (apiProperties.getMode()) { case "direct": case "web": { // set the real user context now! userContext = genericApplicationContext.getBean(UserContext.class); userContext.setProjectId(UUID.randomUUID().toString()); break; } } } catch (Exception ex) { getLogger().trace("Exception in IntegrationTestBase... ", ex); throw new RuntimeException(ex); } classificationApi = genericApplicationContext.getBean(ClassificationApi.class); }
@Override protected URIBuilder createBaseUrl(WebApiAction apiAction) { StringBuilder sb = new StringBuilder(getApiProperties().getWebServiceUrl()); if(!sb.toString().endsWith("/")) sb.append("/"); //since we are in policy api, then obviously this is a policy endpoint! sb.append("policy") .append("/") .append(apiAction.toValue()); String uri = sb.toString(); try { return new URIBuilder(uri); } catch (URISyntaxException e) { getLogger().error("Problem with URI - syntax was invalid: " + uri, e); throw new RuntimeException("Problem with URI - syntax was invalid: " + uri, e); } }
@Override protected URIBuilder createBaseUrl(WebApiAction apiAction) { StringBuilder sb = new StringBuilder(getApiProperties().getWebServiceUrl()); if(!sb.toString().endsWith("/")) sb.append("/"); //since we are in classification api, then obviously this is a classification endpoint! sb.append("classification") .append("/") .append(apiAction.toValue()); String uri = sb.toString(); try { return new URIBuilder(uri); } catch (URISyntaxException e) { getLogger().error("Problem with URI - syntax was invalid: " + uri, e); throw new RuntimeException("Problem with URI - syntax was invalid: " + uri, e); } }
@Override protected URIBuilder createBaseUrl(WebApiAction apiAction) { StringBuilder sb = new StringBuilder(getApiProperties().getWebServiceUrl()); if (!sb.toString().endsWith("/")) sb.append("/"); //since we are in workflow api, use the workflow api endpoint sb.append("workflow") .append("/") .append(apiAction.toValue()); String uri = sb.toString(); try { return new URIBuilder(uri); } catch (URISyntaxException e) { getLogger().error("Problem with URI - syntax was invalid: " + uri, e); throw new RuntimeException("Problem with URI - syntax was invalid: " + uri, e); } }