/*
 * Copyright 1998-2012 Linux.org.ru
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package ru.org.linux.csrf;

import com.google.common.collect.ImmutableMap;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.support.RequestDataValueProcessor;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static ru.org.linux.csrf.CSRFProtectionService.CSRF_ATTRIBUTE;
import static ru.org.linux.csrf.CSRFProtectionService.CSRF_INPUT_NAME;

//
// Thanks to http://blog.eyallupu.com/2012/04/csrf-defense-in-spring-mvc-31.html for the idea
//
@Component("requestDataValueProcessor")
public class CSRFRequestDataValueProcessor implements RequestDataValueProcessor {
  @Override
  public String processAction(HttpServletRequest request, String action) {
    return action;
  }

  @Override
  public String processFormFieldValue(HttpServletRequest request, String name, String value, String type) {
    return value;
  }

  @Override
  public Map<String, String> getExtraHiddenFields(HttpServletRequest request) {
    return ImmutableMap.of(CSRF_INPUT_NAME, (String) request.getAttribute(CSRF_ATTRIBUTE));
  }

  @Override
  public String processUrl(HttpServletRequest request, String url) {
    return url;
  }
}

@@ -693,16 +693,101 @@
 bleMap;%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 import o
@@ -827,16 +827,16 @@
 ponent;%0A
-
 import o
@@ -1335,16 +1335,109 @@
 essor %7B%0A
+  private static final Log logger = LogFactory.getLog(CSRFRequestDataValueProcessor.class);%0A%0A
   @Overr
@@ -1782,95 +1782,281 @@
     
-return ImmutableMap.of(CSRF_INPUT_NAME, (String) request.getAttribute(CSRF_ATTRIBUTE));
+String csrfAttribute = (String) request.getAttribute(CSRF_ATTRIBUTE);%0A%0A    if (csrfAttribute!=null) %7B%0A      return ImmutableMap.of(CSRF_INPUT_NAME, csrfAttribute);%0A    %7D else %7B%0A      logger.debug(%22missing CSRF attribute %22+request.getRequestURI());%0A%0A      return null;%0A    %7D
 %0A  %7D

fix NPE