Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2007-2015, Kaazing Corporation. All rights reserved.
*
* 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.apache.mina.filter.reqres;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.apache.mina.core.filterchain.IoFilterChain;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.write.WriteRequest;
import org.apache.mina.filter.util.WriteRequestFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* TODO Add documentation
*
* @author Apache MINA Project
* @org.apache.xbean.XBean
*/
public class RequestResponseFilter extends WriteRequestFilter {
private final AttributeKey RESPONSE_INSPECTOR = new AttributeKey(getClass(), "responseInspector");
private final AttributeKey REQUEST_STORE = new AttributeKey(getClass(), "requestStore");
private final AttributeKey UNRESPONDED_REQUEST_STORE = new AttributeKey(getClass(), "unrespondedRequestStore");
private final ResponseInspectorFactory responseInspectorFactory;
private final ScheduledExecutorService timeoutScheduler;
private final static Logger LOGGER = LoggerFactory.getLogger(RequestResponseFilter.class);
public RequestResponseFilter(final ResponseInspector responseInspector,
ScheduledExecutorService timeoutScheduler) {
if (responseInspector == null) {
throw new NullPointerException("responseInspector");
}
if (timeoutScheduler == null) {
throw new NullPointerException("timeoutScheduler");
}
this.responseInspectorFactory = new ResponseInspectorFactory() {
public ResponseInspector getResponseInspector() {
return responseInspector;
}
};
this.timeoutScheduler = timeoutScheduler;
}
public RequestResponseFilter(
ResponseInspectorFactory responseInspectorFactory,
ScheduledExecutorService timeoutScheduler) {
if (responseInspectorFactory == null) {
throw new NullPointerException("responseInspectorFactory");
}
if (timeoutScheduler == null) {
throw new NullPointerException("timeoutScheduler");
}
this.responseInspectorFactory = responseInspectorFactory;
this.timeoutScheduler = timeoutScheduler;
}
@Override
public void onPreAdd(IoFilterChain parent, String name,
NextFilter nextFilter) throws Exception {
if (parent.contains(this)) {
throw new IllegalArgumentException(
"You can't add the same filter instance more than once. Create another instance and add it.");
}
IoSession session = parent.getSession();
session.setAttribute(RESPONSE_INSPECTOR, responseInspectorFactory
.getResponseInspector());
session.setAttribute(REQUEST_STORE, createRequestStore(session));
session.setAttribute(UNRESPONDED_REQUEST_STORE, createUnrespondedRequestStore(session));
}
@Override
public void onPostRemove(IoFilterChain parent, String name,
NextFilter nextFilter) throws Exception {
IoSession session = parent.getSession();
destroyUnrespondedRequestStore(getUnrespondedRequestStore(session));
destroyRequestStore(getRequestStore(session));
session.removeAttribute(UNRESPONDED_REQUEST_STORE);
session.removeAttribute(REQUEST_STORE);
session.removeAttribute(RESPONSE_INSPECTOR);
}
@Override
public void messageReceived(NextFilter nextFilter, IoSession session,
Object message) throws Exception {
ResponseInspector responseInspector = (ResponseInspector) session
.getAttribute(RESPONSE_INSPECTOR);
Object requestId = responseInspector.getRequestId(message);
if (requestId == null) {
// Not a response message. Ignore.
nextFilter.messageReceived(session, message);
return;
}
// Retrieve (or remove) the corresponding request.
ResponseType type = responseInspector.getResponseType(message);
if (type == null) {
nextFilter.exceptionCaught(session, new IllegalStateException(
responseInspector.getClass().getName()
+ "#getResponseType() may not return null."));
}
Map