/*
 * Copyright 2014-2015 the original author or authors.
 *
 * 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 org.lastaflute.web.servlet.filter.hotdeploy;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;

import org.dbflute.util.DfTypeUtil;
import org.dbflute.util.Srl;

/**
 * @author modified by jflute (originated in Seasar)
 */
public class HotdeployHttpServletRequest extends HttpServletRequestWrapper {

    protected final HttpServletRequest wrapped;
    protected HttpSession session;

    public HotdeployHttpServletRequest(HttpServletRequest originalRequest) {
        super(originalRequest);
        this.wrapped = originalRequest;
    }

    @Override
    public HttpSession getSession() {
        return getSession(true);
    }

    @Override
    public HttpSession getSession(boolean create) {
        if (session != null) {
            return session;
        }
        final HttpSession originalSession = wrapped.getSession(create);
        if (originalSession == null) {
            return originalSession;
        }
        session = new HotdeployHttpSession(this, originalSession);
        return session;
    }

    public void invalidateSession() {
        session = null;
    }

    @Override
    public String toString() {
        final String plain = wrapped.toString();
        final String firstLine = plain.contains("\n") ? Srl.substringFirstFront(plain, "\n") + "..." : plain; // might contain line so...
        return DfTypeUtil.toClassTitle(this) + "{" + firstLine + "}@" + Integer.toHexString(hashCode());
    }
}

@@ -667,16 +667,53 @@
 eploy;%0A%0A
+import javax.servlet.ServletRequest;%0A
 import j
@@ -742,32 +742,32 @@
 ServletRequest;%0A
-
 import javax.ser
@@ -1062,22 +1062,16 @@
 otected 
-final 
 HttpServ
@@ -1089,16 +1089,61 @@
 wrapped;
+ // mutable, may be changed from setRequest()
 %0A    pro
@@ -1870,32 +1870,224 @@
  = null;%0A    %7D%0A%0A
+    @Override%0A    public void setRequest(ServletRequest request) %7B%0A        super.setRequest(request);%0A        wrapped = (HttpServletRequest) request; // synchronize it with new request%0A    %7D%0A%0A
     @Override%0A  
@@ -2302,16 +2302,16 @@
 e so...%0A
-
         
@@ -2412,11 +2412,87 @@
 );%0A    %7D
+%0A%0A    public HttpServletRequest getWrapped() %7B%0A        return wrapped;%0A    %7D
 %0A%7D%0A

synchronize wrapped with new request, override setRequest()