001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.processor; 018 019import java.util.HashMap; 020import java.util.Locale; 021import java.util.Map; 022 023import org.apache.camel.AsyncProcessor; 024import org.apache.camel.CamelContext; 025import org.apache.camel.CamelContextAware; 026import org.apache.camel.Exchange; 027import org.apache.camel.Message; 028import org.apache.camel.processor.binding.BindingException; 029import org.apache.camel.spi.DataFormat; 030import org.apache.camel.spi.DataType; 031import org.apache.camel.spi.DataTypeAware; 032import org.apache.camel.spi.RestConfiguration; 033import org.apache.camel.util.ExchangeHelper; 034import org.apache.camel.util.MessageHelper; 035import org.apache.camel.util.ObjectHelper; 036 037/** 038 * A {@link org.apache.camel.processor.CamelInternalProcessorAdvice} that binds the REST DSL incoming 039 * and outgoing messages from sources of json or xml to Java Objects. 040 * <p/> 041 * The binding uses {@link org.apache.camel.spi.DataFormat} for the actual work to transform 042 * from xml/json to Java Objects and reverse again. 043 * <p/> 044 * The rest producer side is implemented in {@link org.apache.camel.component.rest.RestProducerBindingProcessor} 045 * 046 * @see CamelInternalProcessor 047 */ 048public class RestBindingAdvice implements CamelInternalProcessorAdvice<Map<String, Object>> { 049 private static final String STATE_KEY_DO_MARSHAL = "doMarshal"; 050 private static final String STATE_KEY_ACCEPT = "accept"; 051 private static final String STATE_JSON = "json"; 052 private static final String STATE_XML = "xml"; 053 054 private final AsyncProcessor jsonUnmarshal; 055 private final AsyncProcessor xmlUnmarshal; 056 private final AsyncProcessor jsonMarshal; 057 private final AsyncProcessor xmlMarshal; 058 private final String consumes; 059 private final String produces; 060 private final String bindingMode; 061 private final boolean skipBindingOnErrorCode; 062 private final boolean enableCORS; 063 private final Map<String, String> corsHeaders; 064 private final Map<String, String> queryDefaultValues; 065 066 public RestBindingAdvice(CamelContext camelContext, DataFormat jsonDataFormat, DataFormat xmlDataFormat, 067 DataFormat outJsonDataFormat, DataFormat outXmlDataFormat, 068 String consumes, String produces, String bindingMode, 069 boolean skipBindingOnErrorCode, boolean enableCORS, 070 Map<String, String> corsHeaders, 071 Map<String, String> queryDefaultValues) throws Exception { 072 073 if (jsonDataFormat != null) { 074 this.jsonUnmarshal = new UnmarshalProcessor(jsonDataFormat); 075 } else { 076 this.jsonUnmarshal = null; 077 } 078 if (outJsonDataFormat != null) { 079 this.jsonMarshal = new MarshalProcessor(outJsonDataFormat); 080 } else if (jsonDataFormat != null) { 081 this.jsonMarshal = new MarshalProcessor(jsonDataFormat); 082 } else { 083 this.jsonMarshal = null; 084 } 085 086 if (xmlDataFormat != null) { 087 this.xmlUnmarshal = new UnmarshalProcessor(xmlDataFormat); 088 } else { 089 this.xmlUnmarshal = null; 090 } 091 if (outXmlDataFormat != null) { 092 this.xmlMarshal = new MarshalProcessor(outXmlDataFormat); 093 } else if (xmlDataFormat != null) { 094 this.xmlMarshal = new MarshalProcessor(xmlDataFormat); 095 } else { 096 this.xmlMarshal = null; 097 } 098 099 if (jsonMarshal != null) { 100 camelContext.addService(jsonMarshal, true); 101 } 102 if (jsonUnmarshal != null) { 103 camelContext.addService(jsonUnmarshal, true); 104 } 105 if (xmlMarshal instanceof CamelContextAware) { 106 camelContext.addService(xmlMarshal, true); 107 } 108 if (xmlUnmarshal instanceof CamelContextAware) { 109 camelContext.addService(xmlUnmarshal, true); 110 } 111 112 this.consumes = consumes; 113 this.produces = produces; 114 this.bindingMode = bindingMode; 115 this.skipBindingOnErrorCode = skipBindingOnErrorCode; 116 this.enableCORS = enableCORS; 117 this.corsHeaders = corsHeaders; 118 this.queryDefaultValues = queryDefaultValues; 119 120 } 121 122 @Override 123 public Map<String, Object> before(Exchange exchange) throws Exception { 124 Map<String, Object> state = new HashMap<>(); 125 if (isOptionsMethod(exchange, state)) { 126 return state; 127 } 128 unmarshal(exchange, state); 129 return state; 130 } 131 132 @Override 133 public void after(Exchange exchange, Map<String, Object> state) throws Exception { 134 if (enableCORS) { 135 setCORSHeaders(exchange, state); 136 } 137 if (state.get(STATE_KEY_DO_MARSHAL) != null) { 138 marshal(exchange, state); 139 } 140 } 141 142 private boolean isOptionsMethod(Exchange exchange, Map<String, Object> state) { 143 String method = exchange.getIn().getHeader(Exchange.HTTP_METHOD, String.class); 144 if ("OPTIONS".equalsIgnoreCase(method)) { 145 // for OPTIONS methods then we should not route at all as its part of CORS 146 exchange.setProperty(Exchange.ROUTE_STOP, true); 147 return true; 148 } 149 return false; 150 } 151 152 private void unmarshal(Exchange exchange, Map<String, Object> state) throws Exception { 153 boolean isXml = false; 154 boolean isJson = false; 155 156 String contentType = ExchangeHelper.getContentType(exchange); 157 if (contentType != null) { 158 isXml = contentType.toLowerCase(Locale.ENGLISH).contains("xml"); 159 isJson = contentType.toLowerCase(Locale.ENGLISH).contains("json"); 160 } 161 // if content type could not tell us if it was json or xml, then fallback to if the binding was configured with 162 // that information in the consumes 163 if (!isXml && !isJson) { 164 isXml = consumes != null && consumes.toLowerCase(Locale.ENGLISH).contains("xml"); 165 isJson = consumes != null && consumes.toLowerCase(Locale.ENGLISH).contains("json"); 166 } 167 168 if (exchange.getIn() instanceof DataTypeAware && (isJson || isXml)) { 169 ((DataTypeAware)exchange.getIn()).setDataType(new DataType(isJson ? "json" : "xml")); 170 } 171 172 // only allow xml/json if the binding mode allows that 173 isXml &= bindingMode.equals("auto") || bindingMode.contains("xml"); 174 isJson &= bindingMode.equals("auto") || bindingMode.contains("json"); 175 176 // if we do not yet know if its xml or json, then use the binding mode to know the mode 177 if (!isJson && !isXml) { 178 isXml = bindingMode.equals("auto") || bindingMode.contains("xml"); 179 isJson = bindingMode.equals("auto") || bindingMode.contains("json"); 180 } 181 182 state.put(STATE_KEY_ACCEPT, exchange.getIn().getHeader("Accept", String.class)); 183 184 String body = null; 185 if (exchange.getIn().getBody() != null) { 186 187 // okay we have a binding mode, so need to check for empty body as that can cause the marshaller to fail 188 // as they assume a non-empty body 189 if (isXml || isJson) { 190 // we have binding enabled, so we need to know if there body is empty or not 191 // so force reading the body as a String which we can work with 192 body = MessageHelper.extractBodyAsString(exchange.getIn()); 193 if (body != null) { 194 if (exchange.getIn() instanceof DataTypeAware) { 195 ((DataTypeAware)exchange.getIn()).setBody(body, new DataType(isJson ? "json" : "xml")); 196 } else { 197 exchange.getIn().setBody(body); 198 } 199 200 if (isXml && isJson) { 201 // we have still not determined between xml or json, so check the body if its xml based or not 202 isXml = body.startsWith("<"); 203 isJson = !isXml; 204 } 205 } 206 } 207 } 208 209 // add missing default values which are mapped as headers 210 if (queryDefaultValues != null) { 211 for (Map.Entry<String, String> entry : queryDefaultValues.entrySet()) { 212 if (exchange.getIn().getHeader(entry.getKey()) == null) { 213 exchange.getIn().setHeader(entry.getKey(), entry.getValue()); 214 } 215 } 216 } 217 218 // favor json over xml 219 if (isJson && jsonUnmarshal != null) { 220 // add reverse operation 221 state.put(STATE_KEY_DO_MARSHAL, STATE_JSON); 222 if (ObjectHelper.isNotEmpty(body)) { 223 jsonUnmarshal.process(exchange); 224 ExchangeHelper.prepareOutToIn(exchange); 225 } 226 return; 227 } else if (isXml && xmlUnmarshal != null) { 228 // add reverse operation 229 state.put(STATE_KEY_DO_MARSHAL, STATE_XML); 230 if (ObjectHelper.isNotEmpty(body)) { 231 xmlUnmarshal.process(exchange); 232 ExchangeHelper.prepareOutToIn(exchange); 233 } 234 return; 235 } 236 237 // we could not bind 238 if ("off".equals(bindingMode) || bindingMode.equals("auto")) { 239 // okay for auto we do not mind if we could not bind 240 state.put(STATE_KEY_DO_MARSHAL, STATE_JSON); 241 } else { 242 if (bindingMode.contains("xml")) { 243 exchange.setException(new BindingException("Cannot bind to xml as message body is not xml compatible", exchange)); 244 } else { 245 exchange.setException(new BindingException("Cannot bind to json as message body is not json compatible", exchange)); 246 } 247 } 248 249 } 250 251 private void marshal(Exchange exchange, Map<String, Object> state) { 252 // only marshal if there was no exception 253 if (exchange.getException() != null) { 254 return; 255 } 256 257 if (skipBindingOnErrorCode) { 258 Integer code = exchange.hasOut() ? exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class) : exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class); 259 // if there is a custom http error code then skip binding 260 if (code != null && code >= 300) { 261 return; 262 } 263 } 264 265 boolean isXml = false; 266 boolean isJson = false; 267 268 // accept takes precedence 269 String accept = (String)state.get(STATE_KEY_ACCEPT); 270 if (accept != null) { 271 isXml = accept.toLowerCase(Locale.ENGLISH).contains("xml"); 272 isJson = accept.toLowerCase(Locale.ENGLISH).contains("json"); 273 } 274 // fallback to content type if still undecided 275 if (!isXml && !isJson) { 276 String contentType = ExchangeHelper.getContentType(exchange); 277 if (contentType != null) { 278 isXml = contentType.toLowerCase(Locale.ENGLISH).contains("xml"); 279 isJson = contentType.toLowerCase(Locale.ENGLISH).contains("json"); 280 } 281 } 282 // if content type could not tell us if it was json or xml, then fallback to if the binding was configured with 283 // that information in the consumes 284 if (!isXml && !isJson) { 285 isXml = produces != null && produces.toLowerCase(Locale.ENGLISH).contains("xml"); 286 isJson = produces != null && produces.toLowerCase(Locale.ENGLISH).contains("json"); 287 } 288 289 // only allow xml/json if the binding mode allows that (when off we still want to know if its xml or json) 290 if (bindingMode != null) { 291 isXml &= bindingMode.equals("off") || bindingMode.equals("auto") || bindingMode.contains("xml"); 292 isJson &= bindingMode.equals("off") || bindingMode.equals("auto") || bindingMode.contains("json"); 293 294 // if we do not yet know if its xml or json, then use the binding mode to know the mode 295 if (!isJson && !isXml) { 296 isXml = bindingMode.equals("auto") || bindingMode.contains("xml"); 297 isJson = bindingMode.equals("auto") || bindingMode.contains("json"); 298 } 299 } 300 301 // in case we have not yet been able to determine if xml or json, then use the same as in the unmarshaller 302 if (isXml && isJson) { 303 isXml = state.get(STATE_KEY_DO_MARSHAL).equals(STATE_XML); 304 isJson = !isXml; 305 } 306 307 // need to prepare exchange first 308 ExchangeHelper.prepareOutToIn(exchange); 309 310 // ensure there is a content type header (even if binding is off) 311 ensureHeaderContentType(produces, isXml, isJson, exchange); 312 313 if (bindingMode == null || "off".equals(bindingMode)) { 314 // binding is off, so no message body binding 315 return; 316 } 317 318 // is there any marshaller at all 319 if (jsonMarshal == null && xmlMarshal == null) { 320 return; 321 } 322 323 // is the body empty 324 if ((exchange.hasOut() && exchange.getOut().getBody() == null) || (!exchange.hasOut() && exchange.getIn().getBody() == null)) { 325 return; 326 } 327 328 String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class); 329 // need to lower-case so the contains check below can match if using upper case 330 contentType = contentType.toLowerCase(Locale.US); 331 try { 332 // favor json over xml 333 if (isJson && jsonMarshal != null) { 334 // only marshal if its json content type 335 if (contentType.contains("json")) { 336 jsonMarshal.process(exchange); 337 setOutputDataType(exchange, new DataType("json")); 338 } 339 } else if (isXml && xmlMarshal != null) { 340 // only marshal if its xml content type 341 if (contentType.contains("xml")) { 342 xmlMarshal.process(exchange); 343 setOutputDataType(exchange, new DataType("xml")); 344 } 345 } else { 346 // we could not bind 347 if (bindingMode.equals("auto")) { 348 // okay for auto we do not mind if we could not bind 349 } else { 350 if (bindingMode.contains("xml")) { 351 exchange.setException(new BindingException("Cannot bind to xml as message body is not xml compatible", exchange)); 352 } else { 353 exchange.setException(new BindingException("Cannot bind to json as message body is not json compatible", exchange)); 354 } 355 } 356 } 357 } catch (Throwable e) { 358 exchange.setException(e); 359 } 360 } 361 362 private void setOutputDataType(Exchange exchange, DataType type) { 363 Message target = exchange.hasOut() ? exchange.getOut() : exchange.getIn(); 364 if (target instanceof DataTypeAware) { 365 ((DataTypeAware)target).setDataType(type); 366 } 367 } 368 369 private void ensureHeaderContentType(String contentType, boolean isXml, boolean isJson, Exchange exchange) { 370 // favor given content type 371 if (contentType != null) { 372 String type = ExchangeHelper.getContentType(exchange); 373 if (type == null) { 374 exchange.getIn().setHeader(Exchange.CONTENT_TYPE, contentType); 375 } 376 } 377 378 // favor json over xml 379 if (isJson) { 380 // make sure there is a content-type with json 381 String type = ExchangeHelper.getContentType(exchange); 382 if (type == null) { 383 exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json"); 384 } 385 } else if (isXml) { 386 // make sure there is a content-type with xml 387 String type = ExchangeHelper.getContentType(exchange); 388 if (type == null) { 389 exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/xml"); 390 } 391 } 392 } 393 394 private void setCORSHeaders(Exchange exchange, Map<String, Object> state) { 395 // add the CORS headers after routing, but before the consumer writes the response 396 Message msg = exchange.hasOut() ? exchange.getOut() : exchange.getIn(); 397 398 // use default value if none has been configured 399 String allowOrigin = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Origin") : null; 400 if (allowOrigin == null) { 401 allowOrigin = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN; 402 } 403 String allowMethods = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Methods") : null; 404 if (allowMethods == null) { 405 allowMethods = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS; 406 } 407 String allowHeaders = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Headers") : null; 408 if (allowHeaders == null) { 409 allowHeaders = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS; 410 } 411 String maxAge = corsHeaders != null ? corsHeaders.get("Access-Control-Max-Age") : null; 412 if (maxAge == null) { 413 maxAge = RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE; 414 } 415 String allowCredentials = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Credentials") : null; 416 417 // Restrict the origin if credentials are allowed. 418 // https://www.w3.org/TR/cors/ - section 6.1, point 3 419 String origin = exchange.getIn().getHeader("Origin", String.class); 420 if ("true".equalsIgnoreCase(allowCredentials) && "*".equals(allowOrigin) && origin != null) { 421 allowOrigin = origin; 422 } 423 424 msg.setHeader("Access-Control-Allow-Origin", allowOrigin); 425 msg.setHeader("Access-Control-Allow-Methods", allowMethods); 426 msg.setHeader("Access-Control-Allow-Headers", allowHeaders); 427 msg.setHeader("Access-Control-Max-Age", maxAge); 428 if (allowCredentials != null) { 429 msg.setHeader("Access-Control-Allow-Credentials", allowCredentials); 430 } 431 } 432 433}