package org.apache.http.client.methods;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicMarkableReference;
import org.apache.http.HttpRequest;
import org.apache.http.client.utils.CloneUtils;
import org.apache.http.concurrent.Cancellable;
import org.apache.http.conn.ClientConnectionRequest;
import org.apache.http.conn.ConnectionReleaseTrigger;
import org.apache.http.message.AbstractHttpMessage;
@SuppressWarnings("deprecation")
public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage implements
HttpExecutionAware, AbortableHttpRequest, Cloneable, HttpRequest {
private final AtomicMarkableReference<Cancellable> cancellableRef;
protected AbstractExecutionAwareRequest() {
super();
this.cancellableRef = new AtomicMarkableReference<Cancellable>(null, false);
}
@Override
@Deprecated
public void setConnectionRequest(final ClientConnectionRequest connRequest) {
setCancellable(new Cancellable() {
@Override
public boolean cancel() {
connRequest.abortRequest();
return true;
}
});
}
@Override
@Deprecated
public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger) {
setCancellable(new Cancellable() {
@Override
public boolean cancel() {
try {
releaseTrigger.abortConnection();
return true;
} catch (final IOException ex) {
return false;
}
}
});
}
@Override
public void abort() {
while (!cancellableRef.isMarked()) {
final Cancellable actualCancellable = cancellableRef.getReference();
if (cancellableRef.compareAndSet(actualCancellable, actualCancellable, false, true)) {
if (actualCancellable != null) {
actualCancellable.cancel();
}
}
}
}
@Override
public boolean isAborted() {
return this.cancellableRef.isMarked();
}
@Override
public void setCancellable(final Cancellable cancellable) {
final Cancellable actualCancellable = cancellableRef.getReference();
if (!cancellableRef.compareAndSet(actualCancellable, cancellable, false, false)) {
cancellable.cancel();
}
}
@Override
public Object clone() throws CloneNotSupportedException {
final AbstractExecutionAwareRequest clone = (AbstractExecutionAwareRequest) super.clone();
clone.headergroup = CloneUtils.cloneObject(this.headergroup);
clone.params = CloneUtils.cloneObject(this.params);
return clone;
}
@Deprecated
public void completed() {
this.cancellableRef.set(null, false);
}
public void reset() {
for (;;) {
final boolean marked = cancellableRef.isMarked();
final Cancellable actualCancellable = cancellableRef.getReference();
if (actualCancellable != null) {
actualCancellable.cancel();
}
if (cancellableRef.compareAndSet(actualCancellable, null, marked, false)) {
break;
}
}
}
}