001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest.annotation; 014 015import static org.apache.juneau.BeanContext.*; 016import static org.apache.juneau.internal.ArrayUtils.*; 017import static org.apache.juneau.internal.ClassUtils.*; 018import static org.apache.juneau.internal.StringUtils.*; 019import static org.apache.juneau.rest.RestContext.*; 020import static org.apache.juneau.rest.RestMethodContext.*; 021import static org.apache.juneau.rest.util.RestUtils.*; 022import static org.apache.juneau.html.HtmlDocSerializer.*; 023import java.util.*; 024import java.util.logging.*; 025 026import org.apache.juneau.*; 027import org.apache.juneau.internal.*; 028import org.apache.juneau.reflect.*; 029import org.apache.juneau.rest.*; 030import org.apache.juneau.rest.annotation.AnnotationUtils; 031import org.apache.juneau.rest.util.*; 032import org.apache.juneau.rest.widget.*; 033import org.apache.juneau.svl.*; 034 035/** 036 * Applies {@link RestMethod} annotations to a {@link PropertyStoreBuilder}. 037 */ 038public class RestMethodConfigApply extends ConfigApply<RestMethod> { 039 040 /** 041 * Constructor. 042 * 043 * @param c The annotation class. 044 * @param r The resolver for resolving values in annotations. 045 */ 046 public RestMethodConfigApply(Class<RestMethod> c, VarResolverSession r) { 047 super(c, r); 048 } 049 050 @SuppressWarnings("deprecation") 051 @Override 052 public void apply(AnnotationInfo<RestMethod> ai, PropertyStoreBuilder psb) { 053 RestMethod a = ai.getAnnotation(); 054 MethodInfo mi = ai.getMethodOn(); 055 String sig = mi == null ? "Unknown" : mi.getSignature(); 056 String s = null; 057 058 for (Property p1 : a.properties()) { 059 psb.set(p1.name(), string(p1.value())); // >>> DEPRECATED - Remove in 9.0 <<< 060 psb.addTo(REST_properties, string(p1.name()), string(p1.value())); 061 } 062 063 for (String p1 : a.flags()) { 064 psb.set(p1, true); // >>> DEPRECATED - Remove in 9.0 <<< 065 psb.addTo(REST_properties, string(p1), true); 066 } 067 068 if (a.serializers().length > 0) 069 psb.set(REST_serializers, merge(ObjectUtils.toType(psb.peek(REST_serializers), Object[].class), a.serializers())); 070 071 if (a.parsers().length > 0) 072 psb.set(REST_parsers, merge(ObjectUtils.toType(psb.peek(REST_parsers), Object[].class), a.parsers())); 073 074 if (a.encoders().length > 0) 075 psb.set(REST_encoders, merge(ObjectUtils.toType(psb.peek(REST_encoders), Object[].class), a.encoders())); 076 077 if (a.produces().length > 0) 078 psb.set(REST_produces, strings(a.produces())); 079 080 if (a.consumes().length > 0) 081 psb.set(REST_consumes, strings(a.consumes())); 082 083 for (String header : strings(a.defaultRequestHeaders())) { 084 String[] h = RestUtils.parseHeader(header); 085 if (h == null) 086 throw new ConfigException("Invalid default request header specified on method ''{0}'': ''{1}''. Must be in the format: ''Header-Name: header-value''", sig, header); 087 if (isNotEmpty(h[1])) 088 psb.addTo(REST_defaultRequestHeaders, h[0], h[1]); 089 } 090 091 for (String header : strings(a.reqHeaders())) { 092 String[] h = RestUtils.parseHeader(header); 093 if (h == null) 094 throw new ConfigException("Invalid default request header specified on method ''{0}'': ''{1}''. Must be in the format: ''Header-Name: header-value''", sig, header); 095 if (isNotEmpty(h[1])) 096 psb.addTo(REST_reqHeaders, h[0], h[1]); 097 } 098 099 if (a.defaultAccept().length() > 0) { 100 s = string(a.defaultAccept()); 101 if (isNotEmpty(s)) 102 psb.addTo(REST_reqHeaders, "Accept", s); 103 } 104 105 if (a.defaultContentType().length() > 0) { 106 s = string(a.defaultContentType()); 107 if (isNotEmpty(s)) 108 psb.addTo(REST_reqHeaders, "Content-Type", s); 109 } 110 111 psb.addTo(REST_converters, a.converters()); 112 113 psb.addTo(REST_guards, reverse(a.guards())); 114 115 psb.addTo(RESTMETHOD_matchers, a.matchers()); 116 117 if (! a.clientVersion().isEmpty()) 118 psb.set(RESTMETHOD_clientVersion, a.clientVersion()); 119 120 psb.set(BEAN_beanFilters, merge(ObjectUtils.toType(psb.peek(BEAN_beanFilters), Object[].class), a.beanFilters())); 121 122 psb.set(BEAN_pojoSwaps, merge(ObjectUtils.toType(psb.peek(BEAN_pojoSwaps), Object[].class), a.pojoSwaps())); 123 124 if (a.bpi().length > 0) { 125 Map<String,String> bpiMap = new LinkedHashMap<>(); 126 for (String s1 : a.bpi()) { 127 for (String s2 : split(s1, ';')) { 128 int i = s2.indexOf(':'); 129 if (i == -1) 130 throw new ConfigException( 131 "Invalid format for @RestMethod(bpi) on method ''{0}''. Must be in the format \"ClassName: comma-delimited-tokens\". \nValue: {1}", sig, s1); 132 bpiMap.put(s2.substring(0, i).trim(), s2.substring(i+1).trim()); 133 } 134 } 135 psb.addTo(BEAN_bpi, bpiMap); 136 } 137 138 if (a.bpx().length > 0) { 139 Map<String,String> bpxMap = new LinkedHashMap<>(); 140 for (String s1 : a.bpx()) { 141 for (String s2 : split(s1, ';')) { 142 int i = s2.indexOf(':'); 143 if (i == -1) 144 throw new ConfigException( 145 "Invalid format for @RestMethod(bpx) on method ''{0}''. Must be in the format \"ClassName: comma-delimited-tokens\". \nValue: {1}", sig, s1); 146 bpxMap.put(s2.substring(0, i).trim(), s2.substring(i+1).trim()); 147 } 148 } 149 psb.addTo(BEAN_bpx, bpxMap); 150 } 151 152 if (! a.defaultCharset().isEmpty()) 153 psb.set(REST_defaultCharset, string(a.defaultCharset())); 154 155 if (! a.maxInput().isEmpty()) 156 psb.set(REST_maxInput, string(a.maxInput())); 157 158 if (! a.maxInput().isEmpty()) 159 psb.set(REST_maxInput, string(a.maxInput())); 160 161 if (! a.path().isEmpty()) 162 psb.set(RESTMETHOD_path, string(a.path())); 163 164 if (! a.rolesDeclared().isEmpty()) 165 psb.addTo(REST_rolesDeclared, strings(a.rolesDeclared())); 166 167 if (! a.roleGuard().isEmpty()) 168 psb.addTo(REST_roleGuard, string(a.roleGuard())); 169 170 for (String h : a.defaultRequestHeaders()) { 171 String[] h2 = RestUtils.parseKeyValuePair(string(h)); 172 if (h2 == null) 173 throw new ConfigException( 174 "Invalid default request header specified on method ''{0}'': ''{1}''. Must be in the format: ''name[:=]value''", sig, s); 175 psb.addTo(RESTMETHOD_defaultRequestHeaders, h2[0], h2[1]); 176 } 177 178 for (String h : a.reqHeaders()) { 179 String[] h2 = RestUtils.parseKeyValuePair(string(h)); 180 if (h2 == null) 181 throw new ConfigException( 182 "Invalid default request header specified on method ''{0}'': ''{1}''. Must be in the format: ''name[:=]value''", sig, s); 183 psb.addTo(RESTMETHOD_reqHeaders, h2[0], h2[1]); 184 } 185 186 for (String ra : a.attrs()) { 187 String[] ra2 = RestUtils.parseKeyValuePair(string(ra)); 188 if (ra2 == null) 189 throw new ConfigException( 190 "Invalid default request attribute specified on method ''{0}'': ''{1}''. Must be in the format: ''name[:=]value''", sig, s); 191 psb.addTo(RESTMETHOD_attrs, ra2[0], ra2[1]); 192 } 193 194 for (String ra : a.reqAttrs()) { 195 String[] ra2 = RestUtils.parseKeyValuePair(string(ra)); 196 if (ra2 == null) 197 throw new ConfigException( 198 "Invalid default request attribute specified on method ''{0}'': ''{1}''. Must be in the format: ''name[:=]value''", sig, s); 199 psb.addTo(RESTMETHOD_reqAttrs, ra2[0], ra2[1]); 200 } 201 202 if (! a.defaultAccept().isEmpty()) 203 psb.addTo(RESTMETHOD_reqHeaders, "Accept", string(a.defaultAccept())); 204 205 if (! a.defaultContentType().isEmpty()) 206 psb.addTo(RESTMETHOD_reqHeaders, string(a.defaultContentType())); 207 208 for (String h : a.defaultQuery()) { 209 String[] h2 = RestUtils.parseKeyValuePair(string(h)); 210 if (h == null) 211 throw new ConfigException( 212 "Invalid default query parameter specified on method ''{0}'': ''{1}''. Must be in the format: ''name[:=]value''", sig, s); 213 psb.addTo(RESTMETHOD_defaultQuery, h2[0], h2[1]); 214 } 215 216 for (String h : a.defaultFormData()) { 217 String[] h2 = RestUtils.parseKeyValuePair(string(h)); 218 if (h == null) 219 throw new ConfigException( 220 "Invalid default form data parameter specified on method ''{0}'': ''{1}''. Must be in the format: ''name[:=]value''", sig, s); 221 psb.addTo(RESTMETHOD_defaultFormData, h2[0], h2[1]); 222 } 223 224 if (! a.method().isEmpty()) 225 psb.set(RESTMETHOD_httpMethod, a.method()); 226 227 if (! a.name().isEmpty()) 228 psb.set(RESTMETHOD_httpMethod, a.name()); 229 230 if (a.priority() != 0) 231 psb.set(RESTMETHOD_priority, a.priority()); 232 233 if (! a.debug().isEmpty()) 234 psb.set(RESTMETHOD_debug, a.debug()); 235 236 if (! AnnotationUtils.empty(a.logging())) { 237 Logging al = a.logging(); 238 ObjectMap m = new ObjectMap(psb.peek(ObjectMap.class, RESTMETHOD_callLoggerConfig)); 239 240 if (! al.useStackTraceHashing().isEmpty()) 241 m.append("useStackTraceHashing", bool(al.useStackTraceHashing())); 242 243 if (! al.stackTraceHashingTimeout().isEmpty()) 244 m.append("stackTraceHashingTimeout", integer(al.stackTraceHashingTimeout(), "@Logging(stackTraceHashingTimeout)")); 245 246 if (! al.disabled().isEmpty()) 247 m.append("disabled", enablement(al.disabled())); 248 249 if (! al.level().isEmpty()) 250 m.append("level", level(al.level(), "@Logging(level)")); 251 252 if (al.rules().length > 0) { 253 ObjectList ol = new ObjectList(); 254 for (LoggingRule a2 : al.rules()) { 255 ObjectMap m2 = new ObjectMap(); 256 257 if (! a2.codes().isEmpty()) 258 m2.append("codes", string(a2.codes())); 259 260 if (! a2.exceptions().isEmpty()) 261 m2.append("exceptions", string(a2.exceptions())); 262 263 if (! a2.debugOnly().isEmpty()) 264 m2.append("debugOnly", bool(a2.debugOnly())); 265 266 if (! a2.level().isEmpty()) 267 m2.append("level", level(a2.level(), "@LoggingRule(level)")); 268 269 if (! a2.req().isEmpty()) 270 m2.append("req", string(a2.req())); 271 272 if (! a2.res().isEmpty()) 273 m2.append("res", string(a2.res())); 274 275 if (! a2.verbose().isEmpty()) 276 m2.append("verbose", bool(a2.verbose())); 277 278 if (! a2.disabled().isEmpty()) 279 m2.append("disabled", bool(a2.disabled())); 280 281 ol.add(m2); 282 } 283 m.put("rules", ol.appendAll(m.getObjectList("rules"))); 284 } 285 286 psb.set(RESTMETHOD_callLoggerConfig, m); 287 } 288 289 HtmlDoc hd = a.htmldoc(); 290 new HtmlDocBuilder(psb).process(hd); 291 for (Class<? extends Widget> wc : hd.widgets()) { 292 Widget w = castOrCreate(Widget.class, wc); 293 psb.addTo(REST_widgets, w); 294 psb.addTo(HTMLDOC_script, "$W{"+w.getName()+".script}"); 295 psb.addTo(HTMLDOC_script, "$W{"+w.getName()+".style}"); 296 } 297 } 298 299 private Enablement enablement(String in) { 300 return Enablement.fromString(string(in)); 301 } 302 303 private Level level(String in, String loc) { 304 try { 305 return Level.parse(string(in)); 306 } catch (Exception e) { 307 throw new ConfigException("Invalid syntax for level on annotation @RestMethod({1}): {2}", loc, in); 308 } 309 } 310}