Restrict allowed httpMethods using enum

user2454455

I am creating connectors for REST API methods. Some methods has the same method name but performs different HTTP methods.

For example, createEntity( HttpMethod httpMethod, CreateEntity model ) can perform POST and GET only. What I want is to have an error when httpMethod is supplied with PUT or DELETE. The allowed httpMethod will depend every method. How can I do this validation inside CreateEntity.groovy?

Here is the HttpMethod.groovy

public enum HttpMethod {
  POST,
  DELETE,
  GET,
  PUT,
  HEAD,
  TRACE,
  CONNECT,
  PATCH
}

CreateEntity.groovy

private String field1;
private String field2;
//write here that only POST and GET are allowed

Connector.groovy

def createEntityQuery ( HttpMethod httpMethod, CreateEntityQuery model ) {
   //perform operations
}

this must be valid:

createEntity( HttpMethod.POST, new EntityQuery([ field1 : "field1", field2 : "field2" ]))
createEntity( HttpMethod.GET, new EntityQuery([ field1 : "field1", field2 : "field2" ]))

this should throw an error:

createEntity( HttpMethod.PUT, new EntityQuery([ field1 : "field1", field2 : "field2" ]))
createEntity( HttpMethod.DELETE, new EntityQuery([ field1 : "field1", field2 : "field2" ]))
Opal

Is that what you're looking for:

public enum HttpMethod {
  POST,
  DELETE,
  GET,
  PUT,
  HEAD,
  TRACE,
  CONNECT,
  PATCH
}

class EntityQuery {
    String field1
    String field2
}

def createEntity(HttpMethod h, EntityQuery q) {
    if(!(h in [HttpMethod.POST, HttpMethod.GET])) {
      throw new Exception('Only POST and GET methods allowed')
    }
}

createEntity(HttpMethod.PUT, new EntityQuery())

?

However it'd be better to expose public methods like createPostEntity or createGetEntity which will be accepting only EntityQuery as an argument and HttpMethod will be handled internally.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Restrict allowed httpMethods using enum

From Dev

Restrict user input to certain value by using enum

From Dev

Restrict user input to certain value by using enum

From Dev

enum using string constants NSLinguisticTagNoun not allowed

From Dev

Restrict or extend an enum in dart

From Dev

What are the allowed types for an enum (class)?

From Dev

Restrict function parameters to certain enum values

From Dev

Why is field referencing not allowed in an enum (or is this a compiler bug?)

From Dev

How to restrict the amount of numbers allowed in the html number input (live)?

From Dev

Restrict SSH login to local network: VPN connection not allowed

From Dev

using keyword with class not allowed?

From Dev

Restrict the output using the perl debugger

From Dev

Restrict IO usage using cgroups

From Dev

Restrict product using date in magento

From Dev

Using regex to restrict user input

From Dev

Protected Variables aren't allowed by default in Checkstyle, but what about in an enum?

From Dev

Why is initialization of enum class temporaries with arbitrary values allowed?

From Dev

Custom enum attribute gives error String types not allowed

From Dev

405 Method not allowed using Restler

From Dev

When is using whitespace for readability NOT allowed?

From Dev

Why is this allowed when using constexpr?

From Dev

R roll using dates not allowed?

From Dev

Using Foolproof RequiredIf with an Enum

From Dev

Using Enum values

From Java

Using an enum as a dictionary key

From Dev

Using Automapper with Flags Enum

From Dev

Using an enum in a POCO class

From Dev

Mocking an enum using Mockito?

From Dev

Enum generation using JavaPoet

Related Related

HotTag

Archive