argos/annotation-service/annotation/src/main/java/gr/cite/annotation/common/scope/fake/FakeRequestScope.java

41 lines
1.1 KiB
Java

package gr.cite.annotation.common.scope.fake;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import java.io.Closeable;
public class FakeRequestScope implements Closeable {
private RequestAttributes initialRequestAttributes = null;
private FakeRequestAttributes currentRequestAttributes = null;
boolean isInUse = false;
public FakeRequestScope() {
this.reset();
}
public void reset() {
this.close();
this.isInUse = true;
this.initialRequestAttributes = RequestContextHolder.getRequestAttributes();
this.currentRequestAttributes = new FakeRequestAttributes();
RequestContextHolder.setRequestAttributes(this.currentRequestAttributes);
}
@Override
public void close() {
if (!this.isInUse) return;
this.isInUse = false;
if (initialRequestAttributes != null) RequestContextHolder.setRequestAttributes(initialRequestAttributes);
else RequestContextHolder.resetRequestAttributes();
if (currentRequestAttributes != null) currentRequestAttributes.requestCompleted();
this.initialRequestAttributes = null;
this.currentRequestAttributes = null;
}
}