|
| 1 | +package de.rwth.idsg.steve.ocpp.soap; |
| 2 | + |
| 3 | +import de.rwth.idsg.steve.ocpp.OcppProtocol; |
| 4 | +import de.rwth.idsg.steve.repository.ChargePointRepository; |
| 5 | +import de.rwth.idsg.steve.repository.OcppServerRepository; |
| 6 | +import de.rwth.idsg.steve.repository.impl.ChargePointRepositoryImpl; |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.apache.cxf.binding.soap.Soap12; |
| 9 | +import org.apache.cxf.binding.soap.SoapFault; |
| 10 | +import org.apache.cxf.interceptor.Fault; |
| 11 | +import org.apache.cxf.message.Message; |
| 12 | +import org.apache.cxf.message.MessageContentsList; |
| 13 | +import org.apache.cxf.phase.AbstractPhaseInterceptor; |
| 14 | +import org.apache.cxf.phase.Phase; |
| 15 | +import org.apache.cxf.service.model.MessageInfo; |
| 16 | +import org.apache.cxf.service.model.MessagePartInfo; |
| 17 | +import org.apache.cxf.ws.addressing.AddressingProperties; |
| 18 | +import org.apache.cxf.ws.addressing.ContextUtils; |
| 19 | +import org.apache.cxf.ws.addressing.EndpointReferenceType; |
| 20 | +import org.springframework.beans.factory.annotation.Autowired; |
| 21 | +import org.springframework.stereotype.Component; |
| 22 | + |
| 23 | +import javax.xml.namespace.QName; |
| 24 | +import java.util.concurrent.ScheduledExecutorService; |
| 25 | + |
| 26 | +import static org.apache.cxf.ws.addressing.JAXWSAConstants.ADDRESSING_PROPERTIES_INBOUND; |
| 27 | + |
| 28 | +/** |
| 29 | + * 1. Checks the registration status of a station for operations other than BootNotification. |
| 30 | + * |
| 31 | + * 2. Intercepts incoming OCPP messages to update the endpoint address ("From" field of the WS-A header) in DB. |
| 32 | + * And the absence of the field is not a deal breaker anymore. But, as a side effect, the user will not be able |
| 33 | + * to send commands to the charging station, since the DB call to list the charge points will filter it out. See |
| 34 | + * {@link ChargePointRepositoryImpl#getChargePointSelect(OcppProtocol)}. |
| 35 | + * |
| 36 | + * @author Sevket Goekay <goekay@dbis.rwth-aachen.de> |
| 37 | + * @since 15.06.2015 |
| 38 | + */ |
| 39 | +@Slf4j |
| 40 | +@Component("MessageHeaderInterceptor") |
| 41 | +public class MessageHeaderInterceptor extends AbstractPhaseInterceptor<Message> { |
| 42 | + |
| 43 | + @Autowired private OcppServerRepository ocppServerRepository; |
| 44 | + @Autowired private ChargePointRepository chargePointRepository; |
| 45 | + @Autowired private ScheduledExecutorService executorService; |
| 46 | + |
| 47 | + private static final String BOOT_OPERATION_NAME = "BootNotification"; |
| 48 | + private static final String CHARGEBOX_ID_HEADER = "ChargeBoxIdentity"; |
| 49 | + |
| 50 | + public MessageHeaderInterceptor() { |
| 51 | + super(Phase.PRE_INVOKE); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void handleMessage(Message message) throws Fault { |
| 56 | + String chargeBoxId = getChargeBoxId(message); |
| 57 | + |
| 58 | + // ------------------------------------------------------------------------- |
| 59 | + // 1. check registration for operations other than BootNotification |
| 60 | + // ------------------------------------------------------------------------- |
| 61 | + |
| 62 | + QName opName = message.getExchange().getBindingOperationInfo().getOperationInfo().getName(); |
| 63 | + |
| 64 | + if (!BOOT_OPERATION_NAME.equals(opName.getLocalPart())) { |
| 65 | + if (!chargePointRepository.isRegistered(chargeBoxId)) { |
| 66 | + throw createAuthFault(opName); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // ------------------------------------------------------------------------- |
| 71 | + // 2. update endpoint |
| 72 | + // ------------------------------------------------------------------------- |
| 73 | + |
| 74 | + executorService.execute(() -> { |
| 75 | + try { |
| 76 | + String endpointAddress = getEndpointAddress(message); |
| 77 | + if (endpointAddress != null) { |
| 78 | + ocppServerRepository.updateEndpointAddress(chargeBoxId, endpointAddress); |
| 79 | + } |
| 80 | + } catch (Exception e) { |
| 81 | + log.error("Exception occurred", e); |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + private String getChargeBoxId(Message message) { |
| 87 | + MessageContentsList lst = MessageContentsList.getContentsList(message); |
| 88 | + if (lst != null) { |
| 89 | + MessageInfo mi = (MessageInfo) message.get("org.apache.cxf.service.model.MessageInfo"); |
| 90 | + for (MessagePartInfo mpi : mi.getMessageParts()) { |
| 91 | + if (CHARGEBOX_ID_HEADER.equals(mpi.getName().getLocalPart())) { |
| 92 | + return (String) lst.get(mpi); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + // should not happen |
| 97 | + throw createSpecFault(message.getExchange().getBindingOperationInfo().getOperationInfo().getName()); |
| 98 | + } |
| 99 | + |
| 100 | + private String getEndpointAddress(Message message) { |
| 101 | + AddressingProperties addressProp = (AddressingProperties) message.get(ADDRESSING_PROPERTIES_INBOUND); |
| 102 | + if (addressProp == null) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + EndpointReferenceType from = addressProp.getFrom(); |
| 107 | + if (ContextUtils.isGenericAddress(from)) { |
| 108 | + return null; |
| 109 | + } else { |
| 110 | + return from.getAddress().getValue(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private static SoapFault createAuthFault(QName qName) { |
| 115 | + // as defined by OCPP spec |
| 116 | + String message = "Sender failed authentication or is not authorized to use the requested operation."; |
| 117 | + SoapFault sf = new SoapFault(message, Soap12.getInstance().getSender()); |
| 118 | + sf.addSubCode(new QName(qName.getNamespaceURI(), "SecurityError")); |
| 119 | + return sf; |
| 120 | + } |
| 121 | + |
| 122 | + private static SoapFault createSpecFault(QName qName) { |
| 123 | + // as defined by OCPP spec |
| 124 | + String message = "Sender's message does not comply with protocol specification."; |
| 125 | + SoapFault sf = new SoapFault(message, Soap12.getInstance().getSender()); |
| 126 | + sf.addSubCode(new QName(qName.getNamespaceURI(), "ProtocolError")); |
| 127 | + return sf; |
| 128 | + } |
| 129 | +} |
0 commit comments