package art.servers.gost.access.controller; import art.library.interop.InteropParameters; import art.library.interop.InteropResponse; import art.library.interop.serialization.Serialization; import art.library.interop.serialization.SerializationException; import art.library.model.devices.gost.access.types.AccessEnforcement_Detection; import art.library.model.devices.gost.access.types.AccessEnforcement_Detection_State; import art.library.model.devices.gost.access.types.AccessEnforcement_Detection_State_Discarded; import art.library.model.devices.gost.types.atex5.PersonATEX5; import art.library.model.devices.gost.types.atex5.VehicleATEX5; import art.library.model.devices.gost.types.permissions.List_Detections; import art.library.model.devices.user.UserPermission; import art.servers.ServerException; import art.servers.gost.access.Shared; import art.servers.gost.access.reports.ReportAccess; import art.servers.gost.atex5.ATEX5; import art.servers.types.HttpAuthentication; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; public class ListenerImplementation extends art.servers.controller.ListenerImplementation { public InteropResponse sendCommands(InteropParameters parameters) throws SerializationException { String language = (String)parameters.getParameterValue("language"); throw new SerializationException(Shared.getMessage(language, "Not available")); } public InteropResponse listDetections(InteropParameters parameters) throws SerializationException { Shared.println("Listener", "1.listDetections"); String language = (String)parameters.getParameterValue("language"); Shared.println("Listener", "2.listDetections: " + language); try { HttpAuthentication authentication = getHttpAuthentication(parameters, language); Shared.println("Listener", "3.listDetections: " + authentication); List_Detections result = new List_Detections(); result.filter = (parameters.hasParameter("filter") == true) ? (String)parameters.getParameterValue("filter") : null; result.order = (parameters.hasParameter("order") == true) ? (String)parameters.getParameterValue("order") : null; result.offset = (parameters.hasParameter("offset") == true) ? Integer.parseInt((String)parameters.getParameterValue("offset")) : 0; result.limit = (parameters.hasParameter("limit") == true) ? Integer.parseInt((String)parameters.getParameterValue("limit")) : 10000; // Shared.println("Listener", "4.listDetections: " + Serialization.toString(result)); Shared.controllerDetections.listDetections(authentication, language, result); Shared.println("Listener", "5.listDetections: " + result.total); InteropResponse response = new InteropResponse(result, "application/json", false); return response; } catch (SerializationException exception) { Shared.printstack("Listener", exception); throw exception; } catch (Exception exception) { Shared.printstack("Listener", exception); throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception); } } public InteropResponse getDetections(InteropParameters parameters) throws SerializationException { String language = (String)parameters.getParameterValue("language"); try { HttpAuthentication authentication = getHttpAuthentication(parameters, language); List_Detections result = new List_Detections(); result.filter = (parameters.hasParameter("filter") == true) ? (String)parameters.getParameterValue("filter") : null; result.order = (parameters.hasParameter("order") == true) ? (String)parameters.getParameterValue("order") : null; result.offset = (parameters.hasParameter("offset") == true) ? Integer.parseInt((String)parameters.getParameterValue("offset")) : 0; result.limit = (parameters.hasParameter("limit") == true) ? Integer.parseInt((String)parameters.getParameterValue("limit")) : 10000; Shared.controllerDetections.getDetections(authentication, language, result); InteropResponse response = new InteropResponse(result, "application/json", false); return response; } catch (SerializationException exception) { throw exception; } catch (Exception exception) { throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception); } } public InteropResponse getDetection(InteropParameters parameters) throws SerializationException { String language = (String)parameters.getParameterValue("language"); try { HttpAuthentication authentication = getHttpAuthentication(parameters, language); boolean images = true; if (parameters.hasParameter("images") == true) { images = Boolean.parseBoolean((String)parameters.getParameterValue("images")); } Shared.println("Listener", "1.GetDetection"); long identifier = Long.parseLong((String)parameters.getParameterValue("identifier")); Shared.println("Listener", "2.GetDetection: " + identifier); AccessEnforcement_Detection detection = Shared.controllerDetections.getDetection(identifier, images); Shared.println("Listener", "3.GetDetection: " + detection); if (detection == null) throw new SerializationException(Shared.getMessage(language, "Invalid discard detection")); Shared.println("Listener", "4.GetDetection: " + new Date(detection.timestamp).toString()); UserPermission permission = Shared.controllerUserPermissions.getUserPermission(authentication.profile, authentication.username); if (permission.hasPermissionsDevice(detection.device.getIdentifier()) == false) { throw new SerializationException(Shared.getMessage(language, "Not allowed")); } Shared.println("Listener", "5.GetDetection: " + new Date(detection.timestamp).toString()); InteropResponse response = new InteropResponse(detection, "application/json", true); Shared.println("Listener", "6.GetDetection: " + response); return(response); } catch (ServerException exception) { Shared.printstack("Listener", exception); throw new SerializationException(Shared.getMessage(language, exception.getMessage())); } catch (Exception exception) { Shared.printstack("Listener", exception); throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception); } } public InteropResponse setDetection(InteropParameters parameters) throws SerializationException { String language = (String)parameters.getParameterValue("language"); try { HttpAuthentication authentication = getHttpAuthentication(parameters, language); // Get detection AccessEnforcement_Detection detection = Shared.controllerDetections.getDetection(Long.parseLong((String)parameters.getParameterValue("number")), false); AccessEnforcement_Detection_State detectionState = (AccessEnforcement_Detection_State)Serialization.deserialize(AccessEnforcement_Detection_State.class, (String)parameters.getParameterValue("body-content")); // Permissions UserPermission permission = Shared.controllerUserPermissions.getUserPermission(authentication.profile, authentication.username); if ((permission.hasPermissionsDevice(detection.device.getIdentifier()) == false) || (permission.hasPermissionsParameters(Shared.getApplicationName(), parameters) == false)) { throw new SerializationException(Shared.getMessage(language, "Not allowed")); } byte[] selectedImage = parameters.getBodyContent(); return (new InteropResponse(Shared.controllerDetections.setDetection(authentication, detection, detectionState, selectedImage, language))); } catch (ServerException exception) { throw new SerializationException(Shared.getMessage(language, exception.getMessage())); } catch (Exception exception) { throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception); } } public InteropResponse printDetection(InteropParameters parameters) throws SerializationException { String language = (String)parameters.getParameterValue("language"); String format = (String)parameters.getParameterValue("format"); try { HttpAuthentication authentication = getHttpAuthentication(parameters, language); // Get detection AccessEnforcement_Detection detection = Shared.controllerDetections.getDetection(Long.parseLong((String)parameters.getParameterValue("number")), true); // Permissions UserPermission permission = Shared.controllerUserPermissions.getUserPermission(authentication.profile, authentication.username); if ((permission.hasPermissionsDevice(detection.device.getIdentifier()) == false) || (permission.hasPermissionsParameters(Shared.getApplicationName(), parameters) == false)) { throw new SerializationException(Shared.getMessage(language, "Not allowed")); } ReportAccess report = new ReportAccess(language, format, detection); byte[] data = report.generate(); InteropResponse response = new InteropResponse(data); if (format.equalsIgnoreCase("xlsx")) response.mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; if (format.equalsIgnoreCase("pdf")) response.mime = "application/pdf"; response.name = getDetectionName(detection) + "." + format; report.close(); return response; } catch (ServerException exception) { throw new SerializationException(Shared.getMessage(language, exception.getMessage())); } catch (Exception exception) { throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception); } } public InteropResponse getPermission(InteropParameters parameters) throws SerializationException { String language = (String)parameters.getParameterValue("language"); try { HttpAuthentication authentication = getHttpAuthentication(parameters, language); long timestamp = getTimestampSeconds((String)parameters.getParameterValue("timestamp")); String plate = ((String)parameters.getParameterValue("plate")).toUpperCase(); String device = (String)parameters.getParameterValue("device"); // Permissions UserPermission permission = Shared.controllerUserPermissions.getUserPermission(authentication.profile, authentication.username); if ((permission.hasPermissionsDevice(device) == false) || (permission.hasPermissionsParameters(Shared.getApplicationName(), parameters) == false)) { throw new SerializationException(Shared.getMessage(language, "Not allowed")); } if (Shared.isKielce == true) { return (new InteropResponse(new Boolean(true), true)); } else { return (new InteropResponse(new Boolean(Shared.controllerDetections.hasPermission(timestamp, device, plate.replaceAll("\\s+","")) != null), true)); } } catch (ServerException exception) { throw new SerializationException(Shared.getMessage(language, exception.getMessage())); } catch (Exception exception) { throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception); } } public InteropResponse getVehicleATEX5(InteropParameters parameters) throws SerializationException { String language = (String)parameters.getParameterValue("language"); try { HttpAuthentication authentication = getHttpAuthentication(parameters, language); ATEX5 atex5 = new ATEX5(); String plate = (String)parameters.getParameterValue("plate"); if (parameters.hasParameter("timestamp")) { long timestamp = Long.parseLong((String)parameters.getParameterValue("timestamp")); VehicleATEX5 vehicle = atex5.solicitudConsultaVehiculoAtex(plate); if ((vehicle.datosTramites != null) && (vehicle.datosTramites.listaTransferencias != null)) { for (int i=(vehicle.datosTramites.listaTransferencias.transferencia.size()-1); i>=0; i--) { long transferenciaTimestamp = vehicle.datosTramites.listaTransferencias.transferencia.get(i).getFechaTransferencia(); if (timestamp < transferenciaTimestamp) { String doi = vehicle.datosTramites.listaTransferencias.transferencia.get(i).idDocumentoAnterior; PersonATEX5 person = atex5.solicitudConsultaPersonaAtex(doi); vehicle.datosGenerales.titular.datosPersona = person.datosPersona; vehicle.datosGenerales.titular.domicilio = person.datosGenerales.domicilio; vehicle.datosGenerales.titular.domicilioIne = person.datosGenerales.domicilioIne; vehicle.datosGenerales.titular.hasta = transferenciaTimestamp; if (i < (vehicle.datosTramites.listaTransferencias.transferencia.size()-1)) { vehicle.datosGenerales.titular.desde = vehicle.datosTramites.listaTransferencias.transferencia.get(i+1).getFechaTransferencia(); } break; } } } InteropResponse response = new InteropResponse(vehicle, "application/json", false); return response; } else { VehicleATEX5 vehicle = atex5.solicitudConsultaVehiculoAtex(plate); InteropResponse response = new InteropResponse(vehicle, "application/json", false); return response; } } catch (Exception exception) { exception.printStackTrace(); throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception); } } public InteropResponse getDiscardReasons(InteropParameters parameters) throws SerializationException { String language = (String)parameters.getParameterValue("language"); try { HttpAuthentication authentication = getHttpAuthentication(parameters, language); List reasons = AccessEnforcement_Detection_State_Discarded.getDiscardings(); for (AccessEnforcement_Detection_State_Discarded reason : reasons) { reason.description = Shared.getMessage(language, reason.description); } return (new InteropResponse(reasons)); } catch (Exception exception) { throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception); } } // private HttpAuthentication getHttpAuthentication (InteropParameters parameters, String language) throws SerializationException { HttpAuthentication authentication = super.getHttpAuthentication(parameters); UserPermission permission = Shared.controllerUserPermissions.getUserPermission(authentication.profile, authentication.username); if (permission.hasPermissionsParameters(Shared.getApplicationName(), parameters) == false) { throw new SerializationException(Shared.getMessage(language, "Not allowed")); } return authentication; } private String getDetectionName(AccessEnforcement_Detection detection) { // Records try { AccessEnforcement_Detection_State detectionState = detection.getLastState(); if (detectionState.processed != null) { return detectionState.processed.record; } else if (detectionState.violation != null) { return detectionState.violation.record; } } catch (Exception exception) { } return "" + detection.number; } public static long getTimestampSeconds(String parameter) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.parse(parameter).getTime(); } // }