protected List<Resource> buildResourceList(RestApi api) { List<Resource> resourceList = new ArrayList<>(); Resources resources = api.getResources(); resourceList.addAll(resources.getItem()); LOG.debug("Building list of resources. Stack trace: ", new Throwable()); final RateLimiter rl = RateLimiter.create(2); while (resources._isLinkAvailable("next")) { rl.acquire(); resources = resources.getNext(); resourceList.addAll(resources.getItem()); } return resourceList; }
protected Resource createResource(RestApi api, String parentResourceId, String part, List<Resource> resources) { final Optional<Resource> existingResource = getResource(parentResourceId, part, resources); // create resource if doesn't exist if (!existingResource.isPresent()) { LOG.info("Creating resource '" + part + "' on " + parentResourceId); CreateResourceInput input = new CreateResourceInput(); input.setPathPart(part); Resource resource = api.getResourceById(parentResourceId); Resource created = resource.createResource(input); resources.add(created); return created; } else { return existingResource.get(); } }
@Override public String createApi(Raml raml, String name, JSONObject config) { this.config = config; // TODO: What to use as description? final RestApi api = createApi(getApiName(raml, name), null); LOG.info("Created API "+api.getId()); try { final Resource rootResource = getRootResource(api).get(); deleteDefaultModels(api); createModels(api, raml.getSchemas(), false); createResources(api, createResourcePath(api, rootResource, raml.getBasePath()), new HashMap<String, UriParameter>(), raml.getResources(), false); } catch (Throwable t) { LOG.error("Error creating API, rolling back", t); rollback(api); throw t; } return api.getId(); }
private void cleanupMethods (Resource resource, Map<ActionType, Action> actions) { final HashSet<String> methods = new HashSet<>(); for (ActionType action : actions.keySet()) { methods.add(action.toString()); } for (Method m : resource.getResourceMethods().values()) { String httpMethod = m.getHttpMethod().toUpperCase(); if (!methods.contains(httpMethod)) { LOG.info(format("Removing deleted method %s for resource %s", httpMethod, resource.getId())); m.deleteMethod(); } } }
@Override public String createApi(Raml raml, String name, JSONObject config) { this.config = config; // TODO: What to use as description? final RestApi api = createApi(getApiName(raml, name), null); LOG.info("Created API "+api.getId()); try { final Resource rootResource = getRootResource(api).get(); deleteDefaultModels(api); createModels(api, raml.getSchemas(), false); createResources(api, createResourcePath(api, rootResource, raml.getBasePath()), raml.getResources(), false); } catch (Throwable t) { LOG.error("Error creating API, rolling back", t); rollback(api); throw t; } return api.getId(); }
protected Optional<Resource> getRootResource(RestApi api) { for (Resource r : buildResourceList(api)) { if ("/".equals(r.getPath())) { return Optional.of(r); } } return Optional.empty(); }
protected Optional<Resource> getResource(String parentResourceId, String pathPart, List<Resource> resources) { for (Resource r : resources) { if (pathEquals(pathPart, r.getPathPart()) && r.getParentId().equals(parentResourceId)) { return Optional.of(r); } } return Optional.empty(); }
protected Optional<Resource> getResource(RestApi api, String fullPath) { for (Resource r : buildResourceList(api)) { if (r.getPath().equals(fullPath)) { return Optional.of(r); } } return Optional.empty(); }
protected void deleteResource(Resource resource) { if (resource._isLinkAvailable("resource:delete")) { try { resource.deleteResource(); } catch (NotFoundException error) {} } // can't delete root resource }
@Override public void updateApi(String apiId, Raml raml, JSONObject config) { this.config = config; RestApi api = getApi(apiId); Optional<Resource> rootResource = getRootResource(api); createModels(api, raml.getSchemas(), true); createResources(api, createResourcePath(api, rootResource.get(), raml.getBasePath()), new HashMap<String, UriParameter>(), raml.getResources(), true); cleanupResources(api, this.paths); cleanupModels(api, this.models); }
private void createResources(RestApi api, Resource rootResource, Map<String, UriParameter> ancestorRequestParameters, Map<String, org.raml.model.Resource> resources, boolean update) { for (Map.Entry<String, org.raml.model.Resource> entry : resources.entrySet()) { final org.raml.model.Resource resource = entry.getValue(); final Resource parentResource = createResourcePath(api, rootResource, entry.getKey()); Map<String, UriParameter> requestParameters = new HashMap<String, UriParameter>(resource.getUriParameters()); requestParameters.putAll(ancestorRequestParameters); createMethods(api, parentResource, requestParameters, resource.getActions(), update); createResources(api, parentResource, requestParameters, resource.getResources(), update); } }
private void createMethods(RestApi api, Resource resource, Map<String, UriParameter> requestParameters, Map<ActionType, Action> actions, boolean update) { for (Map.Entry<ActionType, Action> entry : actions.entrySet()) { createMethod(api, resource, entry.getKey(), entry.getValue(), requestParameters, update); } if (update) { cleanupMethods(resource, actions); } }
private void createIntegration(Resource resource, Method method, JSONObject config) { if (config == null) { return; } try { final JSONObject integ = config.getJSONObject(resource.getPath()) .getJSONObject(method.getHttpMethod().toLowerCase()) .getJSONObject("integration"); IntegrationType type = IntegrationType.valueOf(integ.getString("type").toUpperCase()); LOG.info("Creating integration with type " + type); PutIntegrationInput input = new PutIntegrationInput() .withType(type) .withUri(integ.getString("uri")) .withCredentials(integ.optString("credentials")) .withHttpMethod(integ.optString("httpMethod")) .withRequestParameters(jsonObjectToHashMapString(integ.optJSONObject("requestParameters"))) .withRequestTemplates(jsonObjectToHashMapString(integ.optJSONObject("requestTemplates"))) .withCacheNamespace(integ.optString("cacheNamespace")) .withCacheKeyParameters(jsonObjectToListString(integ.optJSONArray("cacheKeyParameters"))); Integration integration = method.putIntegration(input); createIntegrationResponses(integration, integ.optJSONObject("responses")); } catch (JSONException e) { LOG.info(format("Skipping integration for method %s of %s: %s", method.getHttpMethod(), resource.getPath(), e)); } }
private String getAuthorizationTypeFromConfig(Resource resource, String method, JSONObject config) { if (config == null) { return "NONE"; } try { return config.getJSONObject(resource.getPath()) .getJSONObject(method.toLowerCase()) .getJSONObject("auth") .getString("type") .toUpperCase(); } catch (JSONException exception) { return "NONE"; } }
@Override public void updateApi(String apiId, Raml raml, JSONObject config) { this.config = config; RestApi api = getApi(apiId); Optional<Resource> rootResource = getRootResource(api); createModels(api, raml.getSchemas(), true); createResources(api, createResourcePath(api, rootResource.get(), raml.getBasePath()), raml.getResources(), true); cleanupResources(api, this.paths); cleanupModels(api, this.models); }
private void createResources(RestApi api, Resource rootResource, Map<String, org.raml.model.Resource> resources, boolean update) { for (Map.Entry<String, org.raml.model.Resource> entry : resources.entrySet()) { final org.raml.model.Resource resource = entry.getValue(); final Resource parentResource = createResourcePath(api, rootResource, entry.getKey()); createMethods(api, parentResource, resource.getActions(), update); createResources(api, parentResource, resource.getResources(), update); } }
private void createMethods(RestApi api, Resource resource, Map<ActionType, Action> actions, boolean update) { for (Map.Entry<ActionType, Action> entry : actions.entrySet()) { createMethod(api, resource, entry.getKey(), entry.getValue(), update); } if (update) { cleanupMethods(resource, actions); } }
private static String getBasePath(RestApi restApi) { String[] basePath = null; for (Resources resources = restApi.getResources(); resources != null; resources = safeGetNext(resources)) { for (Resource resource : resources.getItem()) { Map<String, Method> resourceMethods = resource.getResourceMethods(); if (resourceMethods.isEmpty()) { continue; } String[] resourcePath = resource.getPath().split("/"); if (basePath == null) { basePath = resourcePath; } else { int i=0; for (; i<basePath.length && i<resourcePath.length; i++) { if (!basePath[i].equals(resourcePath[i])) { break; } } basePath = Arrays.copyOf(basePath, i); } } } StringBuilder sb = new StringBuilder(); for (String s : basePath) { if (!s.isEmpty()) { sb.append("/"); sb.append(s); } } return sb.toString(); }
protected boolean methodExists(Resource resource, String httpMethod) { return resource.getResourceMethods().get(httpMethod.toUpperCase()) != null; }
@Before public void setUp() throws Exception { BasicConfigurator.configure(); Injector injector = Guice.createInjector(new SwaggerApiImporterTestModule()); client = injector.getInstance(ApiGateway.class); importer = injector.getInstance(SwaggerApiFileImporter.class); RestApis mockRestApis = mock(RestApis.class); Integration mockIntegration = Mockito.mock(Integration.class); Method mockMethod = Mockito.mock(Method.class); when(mockMethod.getHttpMethod()).thenReturn("GET"); when(mockMethod.putIntegration(any())).thenReturn(mockIntegration); mockChildResource = Mockito.mock(Resource.class); when(mockChildResource.getPath()).thenReturn("/child"); when(mockChildResource.putMethod(any(), any())).thenReturn(mockMethod); mockResource = Mockito.mock(Resource.class); when(mockResource.getPath()).thenReturn("/"); when(mockResource.createResource(any())).thenReturn(mockChildResource); when(mockResource.putMethod(any(), any())).thenReturn(mockMethod); Resources mockResources = mock(Resources.class); when(mockResources.getItem()).thenReturn(Arrays.asList(mockResource)); Model mockModel = Mockito.mock(Model.class); when(mockModel.getName()).thenReturn("test model"); Models mockModels = mock(Models.class); when(mockModels.getItem()).thenReturn(Arrays.asList(mockModel)); mockRestApi = mock(RestApi.class); when(mockRestApi.getResources()).thenReturn(mockResources); when(mockRestApi.getModels()).thenReturn(mockModels); when(mockRestApi.getResourceById(any())).thenReturn(mockResource); when(client.getRestApis()).thenReturn(mockRestApis); when(client.createRestApi(any())).thenReturn(mockRestApi); importer.importApi(getResourcePath(API_GATEWAY)); }
private Resource createResourcePath(RestApi api, Resource resource, String fullPath) { final String[] parts = fullPath.split("/"); Resource parentResource = resource; List<Resource> resources = buildResourceList(api); for (int i = 1; i < parts.length; i++) { parentResource = createResource(api, parentResource.getId(), parts[i], resources); paths.add(parentResource.getPath()); } return parentResource; }