use of de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.Resource in project IPK-BrAPI-Validator by plantbreeding.
the class AdminResource method generalTest.
/**
* Run the default test on all public resources
*
* @return Empty response with status code
*/
@GET
@Path("/testallpublic")
public Response generalTest(@Context HttpHeaders headers, @QueryParam("version") @DefaultValue("") String version) {
if (Config.get("advancedMode") == null) {
return Response.status(Status.NOT_FOUND).build();
}
LOGGER.debug("New GET /admin/testallpublic call.");
try {
// Check auth header
if (!auth(headers)) {
String e = JsonMessageManager.jsonMessage(401, "unauthorized", 4002);
return Response.status(Status.UNAUTHORIZED).entity(e).build();
}
if (!version.equals("v1.0") && !version.equals("v1.1")) {
String jsonError = JsonMessageManager.jsonMessage(400, "Missing or invalid version parameter", 4202);
return Response.status(Status.BAD_REQUEST).encoding(jsonError).build();
}
ObjectMapper mapper = new ObjectMapper();
InputStream inJson = TestCollection.class.getResourceAsStream("/collections/CompleteBrapiTest." + version + ".json");
TestCollection tc = mapper.readValue(inJson, TestCollection.class);
List<Resource> publicResources = ResourceService.getAllPublicEndpoints();
publicResources.forEach(resource -> {
try {
RunnerService.TestEndpointWithCallAndSaveReport(resource, tc);
} catch (SQLException | JsonProcessingException e) {
e.printStackTrace();
}
});
System.out.println("Done");
return Response.ok().build();
} catch (SQLException | IOException e) {
e.printStackTrace();
String e1 = JsonMessageManager.jsonMessage(500, "internal server error", 5003);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e1).build();
}
}
use of de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.Resource in project IPK-BrAPI-Validator by plantbreeding.
the class AdminResource method updateProviders.
/**
* Update database information using public json document.
*
* @return Empty response with status code
*/
@POST
@Path("/updateproviders")
public Response updateProviders(@Context HttpHeaders headers) {
if (Config.get("advancedMode") == null) {
return Response.status(Status.NOT_FOUND).build();
}
LOGGER.debug("New GET /admin/testallpublic call.");
int resourcesUpdated = 0;
try {
// Check auth header
if (!auth(headers)) {
String e = JsonMessageManager.jsonMessage(401, "unauthorized", 4002);
return Response.status(Status.UNAUTHORIZED).entity(e).build();
}
ObjectMapper mapper = new ObjectMapper();
// Required because some resource properties are arrays and some objects.
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
// Download Json
URL url = new URL(Config.get("resourceJsonUrl"));
URLConnection connection;
if (!Config.get("http.proxyHost").equals("")) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(Config.get("http.proxyHost"), Integer.parseInt(Config.get("http.proxyPort"))));
connection = url.openConnection(proxy);
} else {
connection = url.openConnection();
}
connection.connect();
InputStream is = connection.getInputStream();
JsonNode node = mapper.readTree(is).at("/brapi-providers/category");
Dao<Provider, UUID> providerDao = DataSourceManager.getDao(Provider.class);
Dao<Resource, UUID> resourceDao = DataSourceManager.getDao(Resource.class);
// Iterate through providers.
for (int i = 0; i < node.size(); i++) {
Provider providerJson = mapper.treeToValue(node.get(i), Provider.class);
// Check if provider existed (by name)
Provider providerDb = providerDao.queryBuilder().where().eq(Provider.NAME_FIELD_NAME, providerJson.getName()).queryForFirst();
if (providerDb == null) {
// Not found in DB, generate
providerDao.create(providerJson);
providerDao.refresh(providerJson);
Iterator<Resource> it = providerJson.getResources().iterator();
while (it.hasNext()) {
Resource res = it.next();
res.setProvider(providerJson);
res.setPublic(true);
resourceDao.create(res);
resourcesUpdated++;
}
} else {
// Found in DB, update.
providerDb.setDescription(providerJson.getDescription());
providerDb.setLogo(providerJson.getLogo());
Iterator<Resource> it = providerJson.getResources().iterator();
while (it.hasNext()) {
Resource res = it.next();
// Check resources in DB
Resource resDb = resourceDao.queryBuilder().where().eq(Resource.URL_FIELD_NAME, res.getUrl()).and().eq(Resource.PROVIDER_FIELD_NAME, providerDb.getId()).queryForFirst();
if (resDb == null) {
// Not found, create
res.setProvider(providerDb);
res.setPublic(true);
resourceDao.create(res);
resourcesUpdated++;
} else {
// Found, generate
resDb.setCertificate(res.getCertificate());
resDb.setName(res.getName());
resDb.setLogo(res.getLogo());
resDb.setPublic(true);
resourceDao.update(resDb);
resourcesUpdated++;
}
}
providerDao.update(providerDb);
}
}
String response = JsonMessageManager.jsonMessage(200, "" + resourcesUpdated + " resources updated.", 2000);
return Response.ok().entity(response).build();
} catch (IOException | SQLException e) {
e.printStackTrace();
String e1 = JsonMessageManager.jsonMessage(500, "internal server error", 5003);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e1).build();
}
}
use of de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.Resource in project IPK-BrAPI-Validator by plantbreeding.
the class ContinuousIntegrationResource method createEndpoint.
/**
* Register new resource
*
* @param endp Json containing the resource's url.
* @return Json message.
*/
@POST
@Path("/resources")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createEndpoint(@Context HttpHeaders headers, Resource res) {
if (Config.get("advancedMode") == null) {
return Response.status(Status.NOT_FOUND).build();
}
LOGGER.debug("New POST /ci/resources call.");
Dao<Resource, UUID> endpointDao = DataSourceManager.getDao(Resource.class);
try {
// Check if the record exists in the database already.
Resource e = ResourceService.getEndpointWithEmailAndUrlAndFreq(res.getEmail(), res.getUrl(), res.getFrequency());
if (res.getEmail() == null) {
String e2 = JsonMessageManager.jsonMessage(400, "Invalid email", 4100);
return Response.status(Status.BAD_REQUEST).entity(e2).build();
} else if (e != null && e.isConfirmed()) {
String e2 = JsonMessageManager.jsonMessage(400, "Url already in use", 4101);
return Response.status(Status.BAD_REQUEST).entity(e2).build();
} else if (e != null && !e.isConfirmed()) {
EmailManager em = new EmailManager(res);
em.sendConfirmation();
String e2 = JsonMessageManager.jsonMessage(200, "We'll resend you a confirmation email.", 2100);
return Response.status(Status.ACCEPTED).entity(e2).build();
} else {
res.setPublic(false);
endpointDao.create(res);
EmailManager em = new EmailManager(res);
em.sendConfirmation();
return Response.status(Status.ACCEPTED).entity(JsonMessageManager.jsonMessage(200, "We'll send you a confirmation email.", 2101)).build();
}
} catch (SQLException e) {
e.printStackTrace();
String e1 = JsonMessageManager.jsonMessage(500, "Internal server error", 5100);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e1).build();
}
}
use of de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.Resource in project IPK-BrAPI-Validator by plantbreeding.
the class SingleTestResource method callTest.
/**
* Run a call test
*
* @param url Url of the BrAPI server. Example: https://test.brapi.org/brapi/v1/
* @return Response with json report
*/
@GET
@Path("/call")
@Produces(MediaType.APPLICATION_JSON)
public Response callTest(@QueryParam("url") String url, @QueryParam("brapiversion") @DefaultValue("") String version) {
LOGGER.debug("New GET /call call.");
try {
if (url.equals("")) {
String jsonError = JsonMessageManager.jsonMessage(400, "Missing or invalid url parameter", 4202);
return Response.status(Status.BAD_REQUEST).encoding(jsonError).build();
}
if (!version.equals("v1.0") && !version.equals("v1.1")) {
String jsonError = JsonMessageManager.jsonMessage(400, "Missing or invalid version parameter", 4202);
return Response.status(Status.BAD_REQUEST).encoding(jsonError).build();
}
String collectionResource;
ObjectMapper mapper = new ObjectMapper();
collectionResource = "/collections/CompleteBrapiTest." + version + ".json";
InputStream inJson = TestCollection.class.getResourceAsStream(collectionResource);
TestCollection tc = mapper.readValue(inJson, TestCollection.class);
Resource res = new Resource(url);
TestSuiteReport testSuiteReport = RunnerService.testEndpointWithCall(res, tc);
TestReport report = new TestReport(res, mapper.writeValueAsString(testSuiteReport));
return Response.ok().entity(report).build();
} catch (IllegalArgumentException e) {
e.printStackTrace();
String e1 = JsonMessageManager.jsonMessage(400, "invalid URL: port must be 80", 5201);
return Response.status(Status.BAD_REQUEST).entity(e1).build();
} catch (IOException e) {
// Thrown by .getResourceAsStream(""). Most probably because of missing file or wrong config structure.
e.printStackTrace();
String e1 = JsonMessageManager.jsonMessage(500, "internal server error", 5201);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e1).build();
}
}
Aggregations