001/* 002 * Stallion Core: A Modern Web Framework 003 * 004 * Copyright (C) 2015 - 2016 Stallion Software LLC. 005 * 006 * This program is free software: you can redistribute it and/or modify it under the terms of the 007 * GNU General Public License as published by the Free Software Foundation, either version 2 of 008 * the License, or (at your option) any later version. This program is distributed in the hope that 009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 011 * License for more details. You should have received a copy of the GNU General Public License 012 * along with this program. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>. 013 * 014 * 015 * 016 */ 017 018package io.stallion.restfulEndpoints; 019 020import io.stallion.users.Role; 021import org.apache.commons.lang3.builder.EqualsBuilder; 022import org.apache.commons.lang3.builder.HashCodeBuilder; 023 024import java.util.ArrayList; 025import java.util.List; 026 027import static io.stallion.utils.Literals.empty; 028 029/** 030 * Represents a RESTful endpoint, for handling incoming HTTP requests based 031 * on the path and HTTP Method. 032 * 033 */ 034public class RestEndpointBase { 035 private String route = ""; 036 private List<RequestArg> args = new ArrayList<>(); 037 private Role role = null; 038 private Boolean checkXSRF = null; 039 private String method = ""; 040 private String _produces = ""; 041 private String _consumes = ""; 042 private Class jsonViewClass; 043 private String scope; 044 045 public RestEndpointBase() { 046 047 } 048 049 public String getRoute() { 050 return route; 051 } 052 053 public RestEndpointBase setRoute(String route) { 054 this.route = route; 055 return this; 056 } 057 058 public List<RequestArg> getArgs() { 059 return args; 060 } 061 062 public RestEndpointBase setArgs(List<RequestArg> args) { 063 this.args = args; 064 return this; 065 } 066 067 public Role getRole() { 068 return role; 069 } 070 071 public RestEndpointBase setRole(String role) { 072 this.role = Enum.valueOf(Role.class, role); 073 return this; 074 } 075 076 public RestEndpointBase setRole(Role role) { 077 this.role = role; 078 return this; 079 } 080 081 /** 082 * If checkXSRF, is not null, return that. 083 * Otherwise: 084 * - don't check HTML endpoints 085 * - do check non-GET endpoints 086 * - do check if requires Role of member or higher 087 * @return 088 */ 089 public boolean shouldCheckXSRF() { 090 if (getCheckXSRF() != null) { 091 return getCheckXSRF(); 092 } 093 if (!"GET".equals(getMethod().toUpperCase())) { 094 return true; 095 } 096 if (getProduces().equals("text/html")) { 097 return false; 098 } 099 if (getRole().getValue() >= Role.MEMBER.getValue()) { 100 return true; 101 } 102 return false; 103 } 104 105 public Boolean getCheckXSRF() { 106 return checkXSRF; 107 } 108 109 public RestEndpointBase setCheckXSRF(Boolean checkXSRF) { 110 this.checkXSRF = checkXSRF; 111 return this; 112 } 113 114 public String getMethod() { 115 return method; 116 } 117 118 public RestEndpointBase setMethod(String method) { 119 this.method = method; 120 return this; 121 } 122 123 124 public String getProduces() { 125 return _produces; 126 } 127 128 public RestEndpointBase setProduces(String _produces) { 129 this._produces = _produces; 130 return this; 131 } 132 133 public String getConsumes() { 134 return _consumes; 135 } 136 137 public RestEndpointBase setConsumes(String _consumes) { 138 this._consumes = _consumes; 139 return this; 140 } 141 142 public RestEndpointBase produces(String _produces) { 143 setProduces(_produces); 144 return this; 145 } 146 147 public RestEndpointBase consumes(String _consumes) { 148 setConsumes(_consumes); 149 return this; 150 } 151 152 153 @Override 154 public int hashCode() { 155 return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers 156 // if deriving: appendSuper(super.hashCode()). 157 append(getRoute()). 158 append(getMethod()). 159 toHashCode(); 160 } 161 162 @Override 163 public boolean equals(Object obj) { 164 if (!(obj instanceof RestEndpointBase)) 165 return false; 166 if (obj == this) 167 return true; 168 169 RestEndpointBase otherEndpoint = (RestEndpointBase) obj; 170 return new EqualsBuilder(). 171 // if deriving: appendSuper(super.equals(obj)). 172 append(getRoute(), otherEndpoint.getRoute()). 173 append(getMethod(), otherEndpoint.getMethod()). 174 isEquals(); 175 } 176 177 public Class getJsonViewClass() { 178 return jsonViewClass; 179 } 180 181 public void setJsonViewClass(Class jsonViewClass) { 182 this.jsonViewClass = jsonViewClass; 183 } 184 185 public String getScope() { 186 return scope; 187 } 188 189 public RestEndpointBase setScope(String scope) { 190 this.scope = scope; 191 return this; 192 } 193 194 public boolean isScoped() { 195 return !empty(scope); 196 } 197} 198 199 200