1 /* 2 * This file is part of Waarp Project (named also Waarp or GG). 3 * 4 * Copyright (c) 2019, Waarp SAS, and individual contributors by the @author 5 * tags. See the COPYRIGHT.txt in the distribution for a full listing of 6 * individual contributors. 7 * 8 * All Waarp Project is free software: you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or (at your 11 * option) any later version. 12 * 13 * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY 14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 15 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License along with 18 * Waarp . If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 package org.waarp.openr66.protocol.http.restv2.dbhandlers; 22 23 import com.fasterxml.jackson.databind.node.ObjectNode; 24 import io.cdap.http.AbstractHttpHandler; 25 import io.netty.handler.codec.http.HttpMethod; 26 import io.netty.handler.codec.http.HttpRequest; 27 import org.waarp.common.exception.InvalidArgumentException; 28 import org.waarp.common.json.JsonHandler; 29 import org.waarp.common.utility.ParametersChecker; 30 import org.waarp.gateway.kernel.rest.RestConfiguration.CRUD; 31 import org.waarp.openr66.protocol.http.restv2.errors.RestError; 32 import org.waarp.openr66.protocol.http.restv2.errors.RestErrorException; 33 34 import java.util.ArrayList; 35 import java.util.List; 36 37 import static io.netty.handler.codec.http.HttpMethod.*; 38 import static org.waarp.openr66.protocol.http.restv2.errors.RestErrors.*; 39 40 /** 41 * This abstract class represents a handler for the WaarpR66 REST API. A handler 42 * can handle one or multiple 43 * entry points with multiple methods each. 44 * <p> 45 * To assign an entry point to a handler, use the {@link javax.ws.rs.Path} 46 * annotation with the URI as value. 47 * This annotation can also be used on methods of the handler to create entry 48 * points below the handler's one. 49 * <p> 50 * For each entry point, all the desired HTTP methods must be assigned a 51 * corresponding Java method using the 52 * appropriate {@link javax.ws.rs.HttpMethod}. These methods must all have an 53 * {@link HttpRequest} and a 54 * {@link io.cdap.http.HttpResponder} as their first 2 parameters, which 55 * correspond respectively to the 56 * request and its reply. The methods may also have other parameters marked with 57 * the 58 * {@link javax.ws.rs.PathParam} annotation, meaning that these parameters 59 * correspond to the {id} given in the 60 * URI of the request. Some parameters may also be marked with the {@link 61 * javax.ws.rs.QueryParam} an 62 * annotation, in which case the parameter marked is referring to the query 63 * parameter whose name is given in 64 * the annotation's parameter. A query parameter can be given a default value 65 * with the 66 * {@link javax.ws.rs.DefaultValue} which will be assigned to the parameter when 67 * the request does not contain 68 * a value for it. 69 * <p> 70 * These methods should also be annotated with the {@link javax.ws.rs.Consumes} 71 * annotation to indicate the 72 * expected content type of the request. Finally, each method should also be 73 * annotated with the 74 * {@link RequiredRole} annotation to make the method only accessible to users 75 * with the specified 76 * {@link org.waarp.common.role.RoleDefault.ROLE} or higher. 77 */ 78 public abstract class AbstractRestDbHandler extends AbstractHttpHandler { 79 80 /** 81 * A byte mask defining which HTTP methods are allowed on the handler. 82 */ 83 protected final byte crud; 84 85 /** 86 * Initializes the handler with the given CRUD mask. 87 * 88 * @param crud the CRUD mask for this handler 89 */ 90 protected AbstractRestDbHandler(final byte crud) { 91 this.crud = crud; 92 } 93 94 /** 95 * Checks whether the {@link HttpRequest} given can be made on the handler 96 * in accordance with the handler's 97 * CRUD configuration. 98 * 99 * @param request the HTTP request to check 100 * 101 * @return {@code true} if the request is active, {@code false} otherwise. 102 */ 103 public boolean checkCRUD(final HttpRequest request) { 104 final HttpMethod method = request.method(); 105 if (method.equals(GET)) { 106 return CRUD.READ.isValid(crud); 107 } else if (method.equals(POST)) { 108 return CRUD.CREATE.isValid(crud); 109 } else if (method.equals(DELETE)) { 110 return CRUD.DELETE.isValid(crud); 111 } else if (method.equals(PUT)) { 112 return CRUD.UPDATE.isValid(crud); 113 } else { 114 return method.equals(OPTIONS); 115 } 116 } 117 118 protected final void checkSanity(final String... strings) { 119 try { 120 ParametersChecker.checkSanityString(strings); 121 } catch (final InvalidArgumentException e) { 122 final List<RestError> errors = new ArrayList<RestError>(1); 123 errors.add(ILLEGAL_FIELD_VALUE("argument", "Content Not Allowed")); 124 throw new RestErrorException(errors); 125 } 126 } 127 128 protected final void checkSanity(final ObjectNode objectNode) { 129 try { 130 ParametersChecker.checkSanityString( 131 JsonHandler.writeAsString(objectNode)); 132 } catch (final InvalidArgumentException e) { 133 final List<RestError> errors = new ArrayList<RestError>(1); 134 errors.add(ILLEGAL_FIELD_VALUE("argument", "Content Not Allowed")); 135 throw new RestErrorException(errors); 136 } 137 } 138 }