1
0
mirror of https://github.com/Rogiel/httpchannel synced 2025-12-05 23:22:51 +00:00

Implements 2shared service and improves channel link synchronization

This commit is contained in:
2012-01-22 21:58:01 -02:00
parent 3b06f0b9e6
commit cd835e2064
21 changed files with 440 additions and 25 deletions

View File

@@ -19,14 +19,14 @@
package com.rogiel.httpchannel.service;
import java.net.URI;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import com.rogiel.httpchannel.http.GetRequest;
import com.rogiel.httpchannel.http.HttpContext;
import com.rogiel.httpchannel.http.PostMultipartRequest;
import com.rogiel.httpchannel.http.PostRequest;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel;
import com.rogiel.httpchannel.util.ThreadUtils;
import com.rogiel.httpchannel.service.exception.UploadServiceException;
/**
* Abstract base service for HTTP enabled services.
@@ -38,13 +38,9 @@ public abstract class AbstractHttpService extends AbstractService implements
Service {
protected final HttpContext http = new HttpContext();
protected LinkedUploadChannel waitChannelLink(LinkedUploadChannel channel,
Future<?> future) {
logger.debug("Waiting channel {} to link", channel);
while (!channel.isLinked() && !future.isDone()) {
ThreadUtils.sleep(100);
}
return channel;
protected LinkedUploadChannel waitChannelLink(LinkedUploadChannel channel)
throws UploadServiceException {
return channel.waitLink(20, TimeUnit.SECONDS);
}
public GetRequest get(String uri) {

View File

@@ -24,6 +24,9 @@ import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.WritableByteChannel;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -32,6 +35,7 @@ import com.rogiel.httpchannel.service.UploadChannel;
import com.rogiel.httpchannel.service.UploadService;
import com.rogiel.httpchannel.service.Uploader;
import com.rogiel.httpchannel.service.exception.UploadLinkNotFoundException;
import com.rogiel.httpchannel.service.exception.UploadServiceException;
/**
* This channel is linked onto another {@link Channel} that actually writes data
@@ -45,6 +49,11 @@ public class LinkedUploadChannel implements UploadChannel {
*/
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* The synchronization phaser
*/
private final Phaser phaser = new Phaser();
/**
* The upload service
*/
@@ -90,6 +99,9 @@ public class LinkedUploadChannel implements UploadChannel {
this.closeCallback = closeCallback;
this.filename = filename;
this.length = filesize;
// register on the phaser
phaser.register();
}
@Override
@@ -167,6 +179,7 @@ public class LinkedUploadChannel implements UploadChannel {
if (!channel.isOpen())
throw new IOException("The destination channel is closed");
this.channel = channel;
phaser.arrive();
}
/**
@@ -176,4 +189,28 @@ public class LinkedUploadChannel implements UploadChannel {
public boolean isLinked() {
return channel != null;
}
/**
* Waits for the channels to get linked
*
* @param timeout
* the amount of time to wait
* @param unit
* the time unit for the wait
*
* @throws UploadServiceException
* if the channel has not been linked after the timeout has
* expired
*/
public LinkedUploadChannel waitLink(int timeout, TimeUnit unit)
throws UploadServiceException {
logger.debug("Waiting channel {} to link", this);
try {
phaser.awaitAdvanceInterruptibly(phaser.getPhase(), timeout, unit);
} catch (InterruptedException | TimeoutException e) {
throw new UploadServiceException("Channel has not linked in "
+ timeout + " " + unit);
}
return this;
}
}

View File

@@ -35,6 +35,7 @@ import org.htmlparser.tags.ImageTag;
import org.htmlparser.tags.InputTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.ScriptTag;
import org.htmlparser.tags.TextareaTag;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
@@ -184,17 +185,21 @@ public class HTMLPage {
public String getInputValueById(final String id) {
return inputValue(filter(InputTag.class, new InputIDFilter(id)));
}
public int getInputValueByIdInt(final String id) {
return Integer.parseInt(inputValue(filter(InputTag.class, new InputIDFilter(id))));
}
public int getInputValueByIdInt(final String id) {
return Integer.parseInt(inputValue(filter(InputTag.class,
new InputIDFilter(id))));
}
public String getInputValue(final Pattern pattern) {
return inputValue(filter(InputTag.class, new InputValuePatternFilter(
pattern)));
}
public String getTextareaValueById(String id) {
return ((TextareaTag) getTagByID(id)).getStringText();
}
public Tag getTagByID(final String id) {
for (final Tag tag : filter(Tag.class, new IDFilter(id))) {
return tag;