Skip to content
Snippets Groups Projects
Verified Commit 0053d0bc authored by Alexander-Krause's avatar Alexander-Krause
Browse files

wip

parent 0a7b4532
No related branches found
No related tags found
No related merge requests found
......@@ -33,7 +33,10 @@ public class AgentRepository {
public Agent lookupAgent(final Agent agent) {
synchronized (AGENTS) {
return AGENTS.stream().filter(Objects::nonNull).filter(a -> a.equals(agent)).findFirst().orElse(null);
return AGENTS.stream().filter(Objects::nonNull).filter(a -> a.getId() == agent.getId()).findFirst()
.orElse(null);
// return AGENTS.stream().filter(Objects::nonNull).filter(a ->
// a.equals(agent)).findFirst().orElse(null);
}
}
......
......@@ -18,6 +18,7 @@ import org.slf4j.LoggerFactory;
import com.github.jasminb.jsonapi.JSONAPIDocument;
import com.github.jasminb.jsonapi.ResourceConverter;
import com.github.jasminb.jsonapi.SerializationSettings;
import com.github.jasminb.jsonapi.exceptions.DocumentSerializationException;
import net.explorviz.discovery.model.Agent;
......@@ -55,97 +56,20 @@ public class ProcezzResource {
this.clientService.registerProviderReader(new JSONAPIProvider<>(converter));
this.clientService.registerProviderWriter(new JSONAPIProvider<>(converter));
}
@PATCH
@Path("/procezz")
@Consumes(MEDIA_TYPE)
public Response updateProcess(final Procezz procezz) {
Agent agent = procezz.getAgent();
agent = this.agentRepository.lookupAgentById(agent.getId());
if (agent == null) {
LOGGER.error("ERROR: {}", ERROR_NODATA_DETAIL);
final String errorObject = ErrorObjectHelper.getInstance().createErrorObjectString(
HTTP_STATUS_UNPROCESSABLE_ENTITY, ERROR_CONNECTION_TITLE, ERROR_CONNECTION_DETAIL);
return Response.status(422).entity(errorObject).build();
}
final String ipAndPort = agent.getIP() + ":" + agent.getPort();
final String url = "http://" + ipAndPort + "/procezz";
// See RFC5789 page 4 for appropriate status codes
Response httpResponse;
try {
httpResponse = this.clientService.doPatch(procezz, url);
} catch (final ProcessingException e) {
LOGGER.warn("No connection to {}. Error: {}", url, e.toString());
final ErrorObjectHelper errHelper = ErrorObjectHelper.getInstance();
agent.setErrorObject(errHelper.createErrorObject(ERROR_CONNECTION_TITLE, ERROR_CONNECTION_DETAIL));
return Response.status(422).entity(agent).build();
}
final int httpStatus = httpResponse.getStatus();
final Procezz updatedProcezz = httpResponse.readEntity(Procezz.class);
this.agentRepository.updateInternalProcezz(updatedProcezz);
return Response.status(httpStatus).entity(updatedProcezz).build();
return forwardPatchRequest(procezz, "/procezz");
}
@PATCH
@Path("/procezz/restart")
@Consumes(MEDIA_TYPE)
public Response restartProcezz(final Procezz procezz) {
Agent agent = procezz.getAgent();
agent = this.agentRepository.lookupAgentById(agent.getId());
if (agent == null) {
LOGGER.error("ERROR: {}", ERROR_NODATA_DETAIL);
final String errorObject = ErrorObjectHelper.getInstance()
.createErrorObjectString(HTTP_STATUS_UNPROCESSABLE_ENTITY, ERROR_NODATA_TITLE, ERROR_NODATA_DETAIL);
return Response.status(422).entity(errorObject).build();
}
final String ipAndPort = agent.getIP() + ":" + agent.getPort();
final String url = "http://" + ipAndPort + "/procezz/restart";
// See RFC5789 page 4 for appropriate status codes
Response httpResponse;
try {
httpResponse = this.clientService.doPatch(procezz, url);
} catch (final ProcessingException e) {
LOGGER.warn("No connection to {}. Error: {}", url, e.toString());
final ErrorObjectHelper errHelper = ErrorObjectHelper.getInstance();
agent.setErrorObject(errHelper.createErrorObject(ERROR_CONNECTION_TITLE, ERROR_CONNECTION_DETAIL));
return Response.status(422).entity(agent).build();
}
final int httpStatus = httpResponse.getStatus();
if (httpStatus == Response.Status.OK.getStatusCode()) {
final Procezz updatedProcezz = httpResponse.readEntity(Procezz.class);
// update internal process
this.agentRepository.updateInternalProcezz(updatedProcezz);
// return updated (possibly restarted) procezz to frontend
return Response.status(httpStatus).entity(updatedProcezz).build();
}
LOGGER.error("Agent returned unexpected HTTP status code: {}", httpStatus);
return Response.status(httpStatus).entity(httpResponse.readEntity(String.class)).build();
return forwardPatchRequest(procezz, "/procezz/restart");
}
@POST
......@@ -192,6 +116,53 @@ public class ProcezzResource {
return Response.ok(this.converter.writeDocumentCollection(new JSONAPIDocument<List<Procezz>>(listToBeReturned)))
.type("application/vnd.api+json").build();
}
private Response forwardPatchRequest(final Procezz procezz, final String urlPath) {
// See RFC5789 page 4 for appropriate status codes
Agent agent = procezz.getAgent();
agent = this.agentRepository.lookupAgentById(agent.getId());
// Internal server error (no agent found)
if (agent == null) {
LOGGER.error("ERROR: {}", ERROR_NODATA_DETAIL);
final String errorObject = ErrorObjectHelper.getInstance()
.createErrorObjectString(HTTP_STATUS_UNPROCESSABLE_ENTITY, ERROR_NODATA_TITLE, ERROR_NODATA_DETAIL);
return Response.status(422).entity(errorObject).build();
}
final String ipAndPort = agent.getIP() + ":" + agent.getPort();
final String url = "http://" + ipAndPort + urlPath;
Response httpResponse;
try {
httpResponse = this.clientService.doPatch(procezz, url);
} catch (final ProcessingException e) {
LOGGER.warn("No connection to {}. Error: {}", url, e.toString());
final ErrorObjectHelper errHelper = ErrorObjectHelper.getInstance();
agent.setErrorObject(errHelper.createErrorObjectString(ERROR_CONNECTION_TITLE, ERROR_CONNECTION_DETAIL));
return Response.status(422).entity(agent).build();
}
final int httpStatus = httpResponse.getStatus();
final Procezz updatedProcezz = httpResponse.readEntity(Procezz.class);
this.agentRepository.updateInternalProcezz(updatedProcezz);
final SerializationSettings serializationSettings = new SerializationSettings.Builder()
.excludedRelationships("agent").build();
try {
return Response.status(httpStatus)
.entity(this.converter.writeDocument(new JSONAPIDocument<>(updatedProcezz), serializationSettings))
.type("application/vnd.api+json").build();
} catch (final DocumentSerializationException e) {
return Response.status(httpStatus).entity(updatedProcezz).build();
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment