default void ensureLogin() throws KevoreeRegistryException, UnirestException { final String accessToken = config().getString("user.access_token"); if (accessToken != null) { if (this.isTokenExpired()) { this.refresh(); } } else { throw new KevoreeRegistryException("Unable to find a valid token"); } }
public RegistryResolver(KevoreeConfig config) { this.client = new KevoreeRegistryClient(config); Log.info("Registry " + this.client.baseUrl()); }
default String baseUrl() { boolean ssl = config().getBoolean("registry.ssl"); String proto = ssl ? "https://": "http://"; int port = config().getInt("registry.port"); if ((port == 443 && ssl) || (port == 80 && !ssl)) { return proto + config().getString("registry.host"); } else { return proto + config().getString("registry.host") + ":" + port; } } }
default RUser getAccount() throws UnirestException, KevoreeRegistryException { // be sure user is logged-in / refresh token if necessary / throws otherwise this.ensureLogin(); // GET account HttpResponse<String> res = Unirest.get(this.baseUrl() + "/api/account") .header("Authorization", "Bearer " + config().getString("user.access_token")) .asString(); if (res.getStatus() == 200) { ObjectMapper mapper = (ObjectMapper) Options.getOption(Option.OBJECT_MAPPER); return mapper.readValue(res.getBody(), RUser.class); } else { throw new KevoreeRegistryException("Invalid access token"); } } }
default HttpResponse<JsonNode> deleteTdef(String namespace, String name, long version) throws UnirestException, KevoreeRegistryException { // be sure user is logged-in / refresh token if necessary / throws otherwise this.ensureLogin(); // DELETE tdef return Unirest.delete(this.baseUrl() + "/api/namespaces/{namespace}/tdefs/{name}/{version}") .header("Authorization", "Bearer " + config().getString("user.access_token")) .routeParam("namespace", namespace) .routeParam("name", name) .routeParam("version", String.valueOf(version)) .asJson(); } }
default HttpResponse<RDeployUnit[]> getAllDus() throws UnirestException { return Unirest.get(this.baseUrl() + "/api/dus") .asObject(RDeployUnit[].class); }
default HttpResponse<RTypeDefinition[]> getAllTdefs() throws UnirestException { return Unirest.get(this.baseUrl() + "/api/tdefs") .asObject(RTypeDefinition[].class); }
private void auth(KevoreeRegistryClient client) throws MojoExecutionException, UnirestException, KevoreeRegistryException { HttpResponse<RAuth> authRes = client.auth(login, password); if (authRes.getStatus() == 401) { throw new MojoExecutionException("You are not logged in"); } else { throw new MojoExecutionException("Something went wrong while authenticating " + login + " (status: " + authRes.getStatusText()+")"); } } }
default HttpResponse<RTypeDefinition> createTdef(String namespace, RTypeDefinition tdef) throws UnirestException, KevoreeRegistryException { // be sure user is logged-in / refresh token if necessary / throws otherwise this.ensureLogin(); // POST tdef return Unirest.post(this.baseUrl() + "/api/namespaces/{namespace}/tdefs") .header("Authorization", "Bearer " + config().getString("user.access_token")) .header("Content-Type", "application/json") .routeParam("namespace", namespace) .body(tdef) .asObject(RTypeDefinition.class); }
default HttpResponse<RDeployUnit[]> getAllDus(String namespace) throws UnirestException { return Unirest.get(this.baseUrl() + "/api/namespaces/{namespace}/dus") .routeParam("namespace", namespace) .asObject(RDeployUnit[].class); }
default HttpResponse<RTypeDefinition[]> getAllTdefs(String namespace) throws UnirestException { return Unirest.get(this.baseUrl() + "/api/namespaces/{namespace}/tdefs") .routeParam("namespace", namespace) .asObject(RTypeDefinition[].class); }
default HttpResponse<RDeployUnit[]> getAllDus(String namespace, String tdefName) throws UnirestException { return Unirest.get(this.baseUrl() + "/api/namespaces/{namespace}/tdefs/{tdefName}/dus") .routeParam("namespace", namespace) .routeParam("tdefName", tdefName) .asObject(RDeployUnit[].class); }
default HttpResponse<RTypeDefinition[]> getAllTdefs(String namespace, String name) throws UnirestException { return Unirest.get(this.baseUrl() + "/api/namespaces/{namespace}/tdefs/{name}") .routeParam("namespace", namespace) .routeParam("name", name) .asObject(RTypeDefinition[].class); }
default HttpResponse<RDeployUnit[]> getAllDus(String namespace, String tdefName, long tdefVersion) throws UnirestException { return Unirest.get(this.baseUrl() + "/api/namespaces/{namespace}/tdefs/{tdefName}/{tdefVersion}/dus") .routeParam("namespace", namespace) .routeParam("tdefName", tdefName) .routeParam("tdefVersion", String.valueOf(tdefVersion)) .asObject(RDeployUnit[].class); }
default HttpResponse<RTypeDefinition> getLatestTdef(String namespace, String name) throws UnirestException { return Unirest.get(this.baseUrl() + "/api/namespaces/{namespace}/tdefs/{name}/latest") .routeParam("namespace", namespace) .routeParam("name", name) .asObject(RTypeDefinition.class); }
default HttpResponse<RDeployUnit[]> getLatestsDus(String namespace, String tdefName, long tdefVersion) throws UnirestException { return Unirest.get(this.baseUrl() + "/api/namespaces/{namespace}/tdefs/{tdefName}/{tdefVersion}/dus") .routeParam("namespace", namespace) .routeParam("tdefName", tdefName) .routeParam("tdefVersion", String.valueOf(tdefVersion)) .queryString("version", "latest") .asObject(RDeployUnit[].class); }
default HttpResponse<RTypeDefinition> getTdef(String namespace, String name, long version) throws UnirestException { return Unirest.get(this.baseUrl() + "/api/namespaces/{namespace}/tdefs/{name}/{version}") .routeParam("namespace", namespace) .routeParam("name", name) .routeParam("version", String.valueOf(version)) .asObject(RTypeDefinition.class); }
default HttpResponse<RDeployUnit[]> getSpecificDus(String namespace, String tdefName, long tdefVersion, Map<String, Object> filters) throws UnirestException { return Unirest.get(this.baseUrl() + "/api/namespaces/{namespace}/tdefs/{tdefName}/{tdefVersion}/specific-dus") .routeParam("namespace", namespace) .routeParam("tdefName", tdefName) .routeParam("tdefVersion", String.valueOf(tdefVersion)) .queryString(filters) .asObject(RDeployUnit[].class); }
default HttpResponse<RDeployUnit> getDu(String namespace, String tdefName, long tdefVersion, String name, String version, String platform) throws UnirestException { return Unirest.get(this.baseUrl() + "/api/namespaces/{namespace}/tdefs/{tdefName}/{tdefVersion}/dus/{name}/{version}/{platform}") .routeParam("namespace", namespace) .routeParam("tdefName", tdefName) .routeParam("tdefVersion", String.valueOf(tdefVersion)) .routeParam("name", name) .routeParam("version", version) .routeParam("platform", platform) .asObject(RDeployUnit.class); }