@Override @RouteDocumentation(description = "@PUT") public void handlePut(Operation put) { ExampleServiceState newState = getBody(put); ExampleServiceState currentState = getState(put); // example of structural validation: check if the new state is acceptable if (currentState.name != null && newState.name == null) { put.fail(new IllegalArgumentException("name must be set")); return; } updateCounter(newState, currentState, false); // replace current state, with the body of the request, in one step setState(put, newState); put.complete(); }
protected ExampleServiceState updateState(Operation update) { // A DCP service handler is state-less: Everything it needs is provided as part of the // of the operation. The body and latest state associated with the service are retrieved // below. ExampleServiceState body = getBody(update); ExampleServiceState currentState = getState(update); // use helper that will merge automatically current state, with state supplied in body. // Note the usage option PropertyUsageOption.AUTO_MERGE_IF_NOT_NULL has been set on the // "name" field. boolean hasStateChanged = Utils.mergeWithState(getStateDescription(), currentState, body); updateCounter(body, currentState, hasStateChanged); if (body.documentExpirationTimeMicros != currentState.documentExpirationTimeMicros) { currentState.documentExpirationTimeMicros = body.documentExpirationTimeMicros; } // response has latest, updated state update.setBody(currentState); return currentState; }