Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propertyconstraintexception is unchecked #65

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -30,51 +30,15 @@ of this software and associated documentation files (the "Software"), to deal
/**
* Exception used when validating fields.
*/
public class PropertyConstraintException extends Exception {
private final String fieldKey;
private final Object fieldValue;
private final String message;
public class PropertyConstraintException extends IllegalArgumentException {

@Override
public String getMessage() {
return message;
}

/**
* @return value of the failing field.
*/
public Object getFieldValue() {
return fieldValue;
}
private static final String EXCEPTION_MESSAGE_TEMPLATE = "Validation failed: [%s]. Current Value: [%s]";

/**
* @return name of the failing field.
*/
public String getFieldKey() {
return fieldKey;
public PropertyConstraintException(Object currentFieldValue, String errorMessage) {
super(createValidationMessage(currentFieldValue, errorMessage));
}

public PropertyConstraintException(String fieldKey, Object fieldValue) {
this(fieldKey, fieldValue, null);
private static String createValidationMessage(Object fieldValue, String errorMessage) {
return String.format(EXCEPTION_MESSAGE_TEMPLATE, errorMessage, fieldValue);
}

public PropertyConstraintException(String fieldKey, Object fieldValue, String message) {

this.fieldKey = fieldKey;
this.fieldValue = fieldValue;
this.message = message;
}

@Override
public String toString() {
String output;
if (fieldValue != null) {
output = String.format("Field %s %s with value %s", fieldKey, message, fieldValue);
}
else {
output = String.format("Field %s has invalid value %s", fieldKey, fieldValue);
}
return output;
}

}
25 changes: 11 additions & 14 deletions ocpp-common/src/main/java/eu/chargetime/ocpp/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void sendConfirmation(String uniqueId, String action, Confirmation confir
private Optional<Class<? extends Confirmation>> getConfirmationType(String uniqueId) throws UnsupportedFeatureException {
Optional<Request> requestOptional = queue.restoreRequest(uniqueId);

if(requestOptional.isPresent()) {
if (requestOptional.isPresent()) {
Optional<Feature> featureOptional = featureRepository.findFeature(requestOptional.get());
if (featureOptional.isPresent()) {
return Optional.of(featureOptional.get().getConfirmationType());
Expand Down Expand Up @@ -151,7 +151,6 @@ public void accept(SessionEvents eventHandler) {

private class CommunicatorEventHandler implements CommunicatorEvents {
private static final String OCCURENCE_CONSTRAINT_VIOLATION = "Payload for Action is syntactically correct but at least one of the fields violates occurence constraints";
private static final String FIELD_CONSTRAINT_VIOLATION = "Field %s violates constraints with value: \"%s\". %s";
private static final String INTERNAL_ERROR = "An internal error occurred and the receiver was not able to process the requested Action successfully";
private static final String UNABLE_TO_PROCESS = "Unable to process action";

Expand All @@ -160,7 +159,7 @@ public void onCallResult(String id, String action, Object payload) {
try {
Optional<Class<? extends Confirmation>> confirmationTypeOptional = getConfirmationType(id);

if(confirmationTypeOptional.isPresent()) {
if (confirmationTypeOptional.isPresent()) {
Confirmation confirmation = communicator.unpackPayload(payload, confirmationTypeOptional.get());
if (confirmation.validate()) {
events.handleConfirmation(id, confirmation);
Expand All @@ -172,9 +171,8 @@ public void onCallResult(String id, String action, Object payload) {
communicator.sendCallError(id, action, "InternalError", INTERNAL_ERROR);
}
} catch (PropertyConstraintException ex) {
String message = String.format(FIELD_CONSTRAINT_VIOLATION, ex.getFieldKey(), ex.getFieldValue(), ex.getMessage());
logger.warn(message, ex);
communicator.sendCallError(id, action, "TypeConstraintViolation", message);
logger.warn(ex.getMessage(), ex);
communicator.sendCallError(id, action, "TypeConstraintViolation", ex.getMessage());
} catch (UnsupportedFeatureException ex) {
logger.warn(INTERNAL_ERROR, ex);
communicator.sendCallError(id, action, "InternalError", INTERNAL_ERROR);
Expand All @@ -185,7 +183,7 @@ public void onCallResult(String id, String action, Object payload) {
}

@Override
synchronized public void onCall(String id, String action, Object payload) {
public synchronized void onCall(String id, String action, Object payload) {
Optional<Feature> featureOptional = featureRepository.findFeature(action);
if (!featureOptional.isPresent()) {
communicator.sendCallError(id, action, "NotImplemented", "Requested Action is not known by receiver");
Expand All @@ -199,9 +197,8 @@ synchronized public void onCall(String id, String action, Object payload) {
communicator.sendCallError(id, action, "OccurenceConstraintViolation", OCCURENCE_CONSTRAINT_VIOLATION);
}
} catch (PropertyConstraintException ex) {
String message = String.format(FIELD_CONSTRAINT_VIOLATION, ex.getFieldKey(), ex.getFieldValue(), ex.getMessage());
logger.warn(message, ex);
communicator.sendCallError(id, action, "TypeConstraintViolation", message);
logger.warn(ex.getMessage(), ex);
communicator.sendCallError(id, action, "TypeConstraintViolation", ex.getMessage());
} catch (Exception ex) {
logger.warn(UNABLE_TO_PROCESS, ex);
communicator.sendCallError(id, action, "FormationViolation", UNABLE_TO_PROCESS);
Expand Down Expand Up @@ -242,9 +239,9 @@ public int hashCode() {
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("sessionId", sessionId)
.add("queue", queue)
.add("featureRepository", featureRepository)
.toString();
.add("sessionId", sessionId)
.add("queue", queue)
.add("featureRepository", featureRepository)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
package eu.chargetime.ocpp.feature.profile;

import eu.chargetime.ocpp.PropertyConstraintException;
import eu.chargetime.ocpp.feature.*;
import eu.chargetime.ocpp.feature.AuthorizeFeature;
import eu.chargetime.ocpp.feature.BootNotificationFeature;
import eu.chargetime.ocpp.feature.ChangeAvailabilityFeature;
import eu.chargetime.ocpp.feature.ChangeConfigurationFeature;
import eu.chargetime.ocpp.feature.ClearCacheFeature;
import eu.chargetime.ocpp.feature.DataTransferFeature;
import eu.chargetime.ocpp.feature.Feature;
import eu.chargetime.ocpp.feature.GetConfigurationFeature;
import eu.chargetime.ocpp.feature.HeartbeatFeature;
import eu.chargetime.ocpp.feature.MeterValuesFeature;
import eu.chargetime.ocpp.feature.RemoteStartTransactionFeature;
import eu.chargetime.ocpp.feature.RemoteStopTransactionFeature;
import eu.chargetime.ocpp.feature.ResetFeature;
import eu.chargetime.ocpp.feature.StartTransactionFeature;
import eu.chargetime.ocpp.feature.StatusNotificationFeature;
import eu.chargetime.ocpp.feature.StopTransactionFeature;
import eu.chargetime.ocpp.feature.UnlockConnectorFeature;
import eu.chargetime.ocpp.model.Confirmation;
import eu.chargetime.ocpp.model.Request;
import eu.chargetime.ocpp.model.core.*;
import eu.chargetime.ocpp.model.core.AuthorizeRequest;
import eu.chargetime.ocpp.model.core.BootNotificationRequest;
import eu.chargetime.ocpp.model.core.ChangeAvailabilityRequest;
import eu.chargetime.ocpp.model.core.ChangeConfigurationRequest;
import eu.chargetime.ocpp.model.core.ChargePointErrorCode;
import eu.chargetime.ocpp.model.core.ChargePointStatus;
import eu.chargetime.ocpp.model.core.ClearCacheRequest;
import eu.chargetime.ocpp.model.core.DataTransferRequest;
import eu.chargetime.ocpp.model.core.GetConfigurationRequest;
import eu.chargetime.ocpp.model.core.HeartbeatRequest;
import eu.chargetime.ocpp.model.core.MeterValue;
import eu.chargetime.ocpp.model.core.MeterValuesRequest;
import eu.chargetime.ocpp.model.core.RemoteStartTransactionRequest;
import eu.chargetime.ocpp.model.core.RemoteStopTransactionRequest;
import eu.chargetime.ocpp.model.core.ResetRequest;
import eu.chargetime.ocpp.model.core.SampledValue;
import eu.chargetime.ocpp.model.core.StartTransactionRequest;
import eu.chargetime.ocpp.model.core.StatusNotificationRequest;
import eu.chargetime.ocpp.model.core.StopTransactionRequest;
import eu.chargetime.ocpp.model.core.UnlockConnectorRequest;

import java.util.ArrayList;
import java.util.Calendar;
Expand Down Expand Up @@ -42,15 +76,15 @@ of this software and associated documentation files (the "Software"), to deal
* <p>
* Contains methods to create outgoing client requests.
*/
public class ClientCoreProfile implements Profile
{
ClientCoreEventHandler eventHandler;
public class ClientCoreProfile implements Profile {

private ClientCoreEventHandler eventHandler;
ArrayList<Feature> features;

/**
* Set up handler for client core feature requests.

* @param handler call back methods for client events
*
* @param handler call back methods for client events
*/
public ClientCoreProfile(ClientCoreEventHandler handler) {
features = new ArrayList<>();
Expand All @@ -77,21 +111,20 @@ public ClientCoreProfile(ClientCoreEventHandler handler) {
/**
* Create a client {@link AuthorizeRequest} with required values.
*
* @param idToken required identification token.
* @param idToken required identification token.
* @return an instance of {@link AuthorizeRequest}.
* @throws PropertyConstraintException cast if requires fields isn't filled out correctly.
* @see AuthorizeRequest
* @see AuthorizeFeature
*/
public AuthorizeRequest createAuthorizeRequest(String idToken) throws PropertyConstraintException {
public AuthorizeRequest createAuthorizeRequest(String idToken) {
return new AuthorizeRequest(idToken);
}

/**
* Create a client {@link BootNotificationRequest} with required values.
*
* @param vendor required. Vendor name.
* @param model required. Charge box model.
* @param vendor required. Vendor name.
* @param model required. Charge box model.
* @return an instance of {@link BootNotificationRequest}
* @see BootNotificationRequest
* @see BootNotificationFeature
Expand All @@ -103,7 +136,7 @@ public BootNotificationRequest createBootNotificationRequest(String vendor, Stri
/**
* Create a client {@link DataTransferRequest} with required values.
*
* @param vendorId required. Vendor identification.
* @param vendorId required. Vendor identification.
* @return an instance of {@link DataTransferRequest}.
* @see DataTransferRequest
* @see DataTransferFeature
Expand All @@ -126,15 +159,14 @@ public HeartbeatRequest createHeartbeatRequest() {
/**
* Create a client {@link MeterValuesRequest} with one {@link SampledValue} and one {@link MeterValue}
*
* @param connectorId required. Identification of connector.
* @param timestamp required. Time of sample.
* @param value required. Value of sample.
* @param connectorId required. Identification of connector.
* @param timestamp required. Time of sample.
* @param value required. Value of sample.
* @return an instance of {@link MeterValuesRequest}.
* @throws PropertyConstraintException thrown if a required field isn't filled out correctly.
* @see MeterValuesRequest
* @see MeterValuesFeature
*/
public MeterValuesRequest createMeterValuesRequest(Integer connectorId, Calendar timestamp, String value) throws PropertyConstraintException {
public MeterValuesRequest createMeterValuesRequest(Integer connectorId, Calendar timestamp, String value) {
SampledValue sampledValue = new SampledValue();
sampledValue.setValue(value);
return createMeterValuesRequest(connectorId, timestamp, sampledValue);
Expand All @@ -147,11 +179,10 @@ public MeterValuesRequest createMeterValuesRequest(Integer connectorId, Calendar
* @param timestamp required. Time of sample.
* @param sampledValues required. Params list of {@link SampledValue}s
* @return an instance of {@link MeterValuesRequest}
* @throws PropertyConstraintException thrown if a required field isn't filled out correctly.
* @see MeterValuesRequest
* @see MeterValuesFeature
*/
public MeterValuesRequest createMeterValuesRequest(Integer connectorId, Calendar timestamp, SampledValue... sampledValues) throws PropertyConstraintException {
public MeterValuesRequest createMeterValuesRequest(Integer connectorId, Calendar timestamp, SampledValue... sampledValues) {
MeterValue meterValue = new MeterValue();
meterValue.setTimestamp(timestamp);
meterValue.setSampledValue(sampledValues);
Expand All @@ -161,14 +192,13 @@ public MeterValuesRequest createMeterValuesRequest(Integer connectorId, Calendar
/**
* Create a client {@link MeterValuesRequest} with some {@link MeterValue}s.
*
* @param connectorId required. Identification of connector.
* @param meterValues required. Params list of {@link MeterValue}s
* @param connectorId required. Identification of connector.
* @param meterValues required. Params list of {@link MeterValue}s
* @return an instance of {@link MeterValuesRequest}
* @throws PropertyConstraintException thrown if a required field isn't filled out correctly.
* @see MeterValuesRequest
* @see MeterValuesFeature
*/
public MeterValuesRequest createMeterValuesRequest(Integer connectorId, MeterValue... meterValues) throws PropertyConstraintException {
public MeterValuesRequest createMeterValuesRequest(Integer connectorId, MeterValue... meterValues) {
MeterValuesRequest request = new MeterValuesRequest();
request.setConnectorId(connectorId);
request.setMeterValue(meterValues);
Expand All @@ -178,16 +208,15 @@ public MeterValuesRequest createMeterValuesRequest(Integer connectorId, MeterVal
/**
* Create a client {@link StartTransactionRequest} with required values.
*
* @param connectorId required. Identification of the connector.
* @param idTag required. Authorization identification tag.
* @param meterStart required. The initial value of the meter.
* @param timestamp required. Time of start.
* @param connectorId required. Identification of the connector.
* @param idTag required. Authorization identification tag.
* @param meterStart required. The initial value of the meter.
* @param timestamp required. Time of start.
* @return an instance of {@link StartTransactionRequest}.
* @throws PropertyConstraintException thrown if a required field isn't filled out correctly.
* @see StartTransactionRequest
* @see StartTransactionFeature
*/
public StartTransactionRequest createStartTransactionRequest(Integer connectorId, String idTag, Integer meterStart, Calendar timestamp) throws PropertyConstraintException {
public StartTransactionRequest createStartTransactionRequest(Integer connectorId, String idTag, Integer meterStart, Calendar timestamp) {
StartTransactionRequest request = new StartTransactionRequest();
request.setConnectorId(connectorId);
request.setIdTag(idTag);
Expand All @@ -199,15 +228,14 @@ public StartTransactionRequest createStartTransactionRequest(Integer connectorId
/**
* Create a client {@link StatusNotificationRequest} with required values.
*
* @param connectorId required. Identification of the connector.
* @param errorCode required. {@link ChargePointErrorCode} of the connector.
* @param status required. {@link ChargePointStatus} of the connector.
* @param connectorId required. Identification of the connector.
* @param errorCode required. {@link ChargePointErrorCode} of the connector.
* @param status required. {@link ChargePointStatus} of the connector.
* @return an instance of {@link StatusNotificationRequest}.
* @throws PropertyConstraintException thrown if a required field isn't filled out correctly.
* @see StatusNotificationRequest
* @see StatusNotificationFeature
*/
public StatusNotificationRequest createStatusNotificationRequest(Integer connectorId, ChargePointErrorCode errorCode, ChargePointStatus status) throws PropertyConstraintException {
public StatusNotificationRequest createStatusNotificationRequest(Integer connectorId, ChargePointErrorCode errorCode, ChargePointStatus status) {
StatusNotificationRequest request = new StatusNotificationRequest();
request.setConnectorId(connectorId);
request.setErrorCode(errorCode);
Expand All @@ -218,9 +246,9 @@ public StatusNotificationRequest createStatusNotificationRequest(Integer connect
/**
* Create a client {@link StopTransactionRequest} with required values.
*
* @param meterStop required. The final value of the meter.
* @param timestamp required. Time of stop.
* @param transactionId required. The identification of the transaction.
* @param meterStop required. The final value of the meter.
* @param timestamp required. Time of stop.
* @param transactionId required. The identification of the transaction.
* @return an instance of {@link StopTransactionRequest}.
*/
public StopTransactionRequest createStopTransactionRequest(int meterStop, Calendar timestamp, int transactionId) {
Expand Down Expand Up @@ -249,7 +277,7 @@ public Confirmation handleRequest(UUID sessionIndex, Request request) {
} else if (request instanceof ClearCacheRequest) {
result = eventHandler.handleClearCacheRequest((ClearCacheRequest) request);
} else if (request instanceof DataTransferRequest) {
result = eventHandler.handleDataTransferRequest((DataTransferRequest)request);
result = eventHandler.handleDataTransferRequest((DataTransferRequest) request);
} else if (request instanceof RemoteStartTransactionRequest) {
result = eventHandler.handleRemoteStartTransactionRequest((RemoteStartTransactionRequest) request);
} else if (request instanceof RemoteStopTransactionRequest) {
Expand Down
Loading