ReportService.java [src/csip] Revision: default Date:
/*
* $Id$
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API and application suite.
*
* 2012-2022, Olaf David and others, OMSLab, Colorado State University.
*
* OMSLab licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
*/
package csip;
import csip.utils.Services;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import org.apache.commons.io.IOUtils;
import org.codehaus.jettison.json.JSONException;
import csip.utils.JSONUtils;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
/**
* Reporting service
*
* @author od
*/
@Path("/r")
public class ReportService {
static final Logger LOG = Config.LOG;
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{suid}")
public String report(@Context UriInfo uriInfo, @PathParam("suid") final String suid) throws JSONException {
LOG.log(Level.INFO, "HTTP/GET {0}", uriInfo.getRequestUri().toString());
try {
// try to find the file.
ModelSession session = Config.getSessionStore().getSession(suid);
if (session == null) {
return JSONUtils.error("invalid suid.").toString();
}
File f = new File(Services.getResultsDir(suid), "report.json");
InputStream is = null;
if (!f.exists() && !session.getNodeIP().equals(Services.LOCAL_IP_ADDR)) {
// where is that session?
// maybe on another vm.
String redirect = Utils.replaceHostinURI(uriInfo.getBaseUri(), session.getNodeIP());
Client client = ClientBuilder.newClient();
WebTarget service = client.target(UriBuilder.fromUri(redirect + "r/" + suid).build());
Response response = service.request(MediaType.APPLICATION_JSON).get();
is = response.readEntity(InputStream.class);
} else if (f.exists() && session.getNodeIP().equals(Services.LOCAL_IP_ADDR)) {
is = new FileInputStream(f);
}
if (is == null) {
return JSONUtils.error("no report").toString();
} else {
return IOUtils.toString(is, "UTF-8");
}
} catch (Exception E) {
throw new WebApplicationException(E);
}
}
}