Map<String, String> paramsMap = initData.getParamsMap();
final HttpSession session = request.getSession();
final User loggedUser = initData.getUser();
JSONObject jsonResponse = new JSONObject();
//Validate the parameters
final String endpointId = paramsMap.get( "endpoint" );
if ( !UtilMethods.isSet( endpointId ) ) {
return Response.status( HttpStatus.SC_BAD_REQUEST ).entity( "Error: endpoint is a required Field.").build();
}
// return if we already have the data
try {
IntegrityUtil integrityUtil = new IntegrityUtil();
if(integrityUtil.doesIntegrityConflictsDataExist(endpointId)) {
jsonResponse.put( "success", true );
jsonResponse.put( "message", "Integrity Checking Initialized..." );
//Setting the process status
setStatus( request, endpointId, ProcessStatus.FINISHED );
return response( jsonResponse.toString(), false );
}
} catch(JSONException e) {
Logger.error(IntegrityResource.class, "Error setting return message in JSON response", e);
return response( "Error setting return message in JSON response" , true );
} catch(Exception e) {
Logger.error(IntegrityResource.class, "Error checking existence of integrity data", e);
return response( "Error checking existence of integrity data" , true );
}
try {
//Setting the process status
setStatus( request, endpointId, ProcessStatus.PROCESSING );
final Client client = getRESTClient();
final PublishingEndPoint endpoint = APILocator.getPublisherEndPointAPI().findEndPointById(endpointId);
final String authToken = PushPublisher.retriveKeyString(PublicEncryptionFactory.decryptString(endpoint.getAuthKey().toString()));
FormDataMultiPart form = new FormDataMultiPart();
form.field("AUTH_TOKEN",authToken);
//Sending bundle to endpoint
String url = endpoint.toURL()+"/api/integrity/generateintegritydata/";
com.dotcms.repackage.com.sun.jersey.api.client.WebResource resource = client.resource(url);
ClientResponse response =
resource.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
if(response.getClientResponseStatus().getStatusCode() == HttpStatus.SC_OK) {
final String integrityDataRequestID = response.getEntity(String.class);
Thread integrityDataRequestChecker = new Thread() {
public void run(){
FormDataMultiPart form = new FormDataMultiPart();
form.field("AUTH_TOKEN",authToken);
form.field("REQUEST_ID",integrityDataRequestID);
String url = endpoint.toURL()+"/api/integrity/getintegritydata/";
com.dotcms.repackage.com.sun.jersey.api.client.WebResource resource = client.resource(url);
boolean processing = true;
while(processing) {
ClientResponse response = resource.type( MediaType.MULTIPART_FORM_DATA ).post( ClientResponse.class, form );
if ( response.getClientResponseStatus() != null && response.getClientResponseStatus().getStatusCode() == HttpStatus.SC_OK ) {
processing = false;
InputStream zipFile = response.getEntityInputStream();
String outputDir = ConfigUtils.getIntegrityPath() + File.separator + endpoint.getId();
try {
IntegrityUtil.unzipFile(zipFile, outputDir);
} catch(Exception e) {
//Special handling if the thread was interrupted
if ( e instanceof InterruptedException ) {
//Setting the process status
setStatus( session, endpointId, ProcessStatus.CANCELED, null );
Logger.debug( IntegrityResource.class, "Requested interruption of the integrity checking process [unzipping Integrity Data] by the user.", e );
throw new RuntimeException( "Requested interruption of the integrity checking process [unzipping Integrity Data] by the user.", e );
}
//Setting the process status
setStatus( session, endpointId, ProcessStatus.ERROR, null );
Logger.error(IntegrityResource.class, "Error while unzipping Integrity Data", e);
throw new RuntimeException("Error while unzipping Integrity Data", e);
}
// set session variable
// call IntegrityChecker
Boolean foldersConflicts = false;
Boolean structuresConflicts = false;
Boolean schemesConflicts = false;
IntegrityUtil integrityUtil = new IntegrityUtil();
try {
HibernateUtil.startTransaction();
foldersConflicts = integrityUtil.checkFoldersIntegrity(endpointId);
structuresConflicts = integrityUtil.checkStructuresIntegrity(endpointId);
schemesConflicts = integrityUtil.checkWorkflowSchemesIntegrity(endpointId);
HibernateUtil.commitTransaction();
} catch(Exception e) {
try {
HibernateUtil.rollbackTransaction();
} catch (DotHibernateException e1) {
Logger.error(IntegrityResource.class, "Error while rolling back transaction", e);
}
//Special handling if the thread was interrupted
if ( e instanceof InterruptedException ) {
//Setting the process status
setStatus( session, endpointId, ProcessStatus.CANCELED, null );
Logger.debug( IntegrityResource.class, "Requested interruption of the integrity checking process by the user.", e );
throw new RuntimeException( "Requested interruption of the integrity checking process by the user.", e );
}
Logger.error(IntegrityResource.class, "Error checking integrity", e);
//Setting the process status
setStatus( session, endpointId, ProcessStatus.ERROR, null );
throw new RuntimeException("Error checking integrity", e);
} finally {
try {
integrityUtil.dropTempTables(endpointId);
} catch (DotDataException e) {
Logger.error(IntegrityResource.class, "Error while deleting temp tables", e);
}
}
if ( !foldersConflicts && !structuresConflicts && !schemesConflicts ) {
String noConflictMessage;
try {
noConflictMessage = LanguageUtil.get( loggedUser.getLocale(), "push_publish_integrity_conflicts_not_found" );
} catch ( LanguageException e ) {
noConflictMessage = "No Integrity Conflicts found";
}
//Setting the process status
setStatus( session, endpointId, ProcessStatus.NO_CONFLICTS, noConflictMessage );
} else {
//Setting the process status
setStatus( session, endpointId, ProcessStatus.FINISHED, null );
}
} else if ( response.getClientResponseStatus() == null && response.getStatus() == HttpStatus.SC_PROCESSING ) {
continue;
} else if ( response.getClientResponseStatus() == null && response.getStatus() == HttpStatus.SC_RESET_CONTENT ) {
processing = false;
//Setting the process status
setStatus( session, endpointId, ProcessStatus.CANCELED, null );
} else {
setStatus( session, endpointId, ProcessStatus.ERROR, null );
Logger.error( this.getClass(), "Response indicating a " + response.getClientResponseStatus().getReasonPhrase() + " (" + response.getClientResponseStatus().getStatusCode() + ") Error trying to retrieve the Integrity data from the Endpoint [" + endpointId + "]." );
processing = false;
}
}
}
};
//Start the integrity check
integrityDataRequestChecker.start();
addThreadToSession( session, integrityDataRequestChecker, endpointId, integrityDataRequestID );
} else if ( response.getClientResponseStatus().getStatusCode() == HttpStatus.SC_UNAUTHORIZED ) {
setStatus( session, endpointId, ProcessStatus.ERROR, null );
Logger.error( this.getClass(), "Response indicating Not Authorized received from Endpoint. Please check Auth Token. Endpoint Id: " + endpointId );
return response( "Response indicating Not Authorized received from Endpoint. Please check Auth Token. Endpoint Id:" + endpointId, true );
} else {
setStatus( session, endpointId, ProcessStatus.ERROR, null );
Logger.error( this.getClass(), "Response indicating a " + response.getClientResponseStatus().getReasonPhrase() + " (" + response.getClientResponseStatus().getStatusCode() + ") Error trying to connect with the Integrity API on the Endpoint. Endpoint Id: " + endpointId );
return response( "Response indicating a " + response.getClientResponseStatus().getReasonPhrase() + " (" + response.getClientResponseStatus().getStatusCode() + ") Error trying to connect with the Integrity API on the Endpoint. Endpoint Id: " + endpointId, true );
}
jsonResponse.put( "success", true );
jsonResponse.put( "message", "Integrity Checking Initialized..." );
} catch(Exception e) {
//Special handling if the thread was interrupted
if ( e instanceof InterruptedException || e.getCause() instanceof InterruptedException ) {
//Setting the process status
setStatus( session, endpointId, ProcessStatus.CANCELED, null );
Logger.debug( IntegrityResource.class, "Requested interruption of the integrity checking process by the user.", e );
return response( "Requested interruption of the integrity checking process by the user for End Point server: [" + endpointId + "]" , true );
}
//Setting the process status
setStatus( session, endpointId, ProcessStatus.ERROR, null );
Logger.error( this.getClass(), "Error initializing the integrity checking process for End Point server: [" + endpointId + "]", e );
return response( "Error initializing the integrity checking process for End Point server: [" + endpointId + "]" , true );
}
return response( jsonResponse.toString(), false );
}