1
0
mirror of https://github.com/Rogiel/httpchannel synced 2025-12-06 07:32:50 +00:00

Implements several services and improves API

This commit is contained in:
2012-01-15 18:31:50 -02:00
parent f210afd16a
commit 23f80b50e6
117 changed files with 3741 additions and 1335 deletions

View File

@@ -25,12 +25,12 @@ import java.util.regex.Pattern;
import org.apache.commons.io.FilenameUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.htmlparser.Tag;
import com.rogiel.httpchannel.service.AbstractDownloader;
import com.rogiel.httpchannel.service.AbstractAuthenticator;
import com.rogiel.httpchannel.service.AbstractHttpDownloader;
import com.rogiel.httpchannel.service.AbstractHttpService;
import com.rogiel.httpchannel.service.AbstractUploader;
import com.rogiel.httpchannel.service.AuthenticationService;
import com.rogiel.httpchannel.service.Authenticator;
import com.rogiel.httpchannel.service.AuthenticatorCapability;
@@ -50,28 +50,27 @@ import com.rogiel.httpchannel.service.UploaderCapability;
import com.rogiel.httpchannel.service.channel.InputStreamDownloadChannel;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel.LinkedUploadChannelCloseCallback;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannelContentBody;
import com.rogiel.httpchannel.service.config.ServiceConfiguration;
import com.rogiel.httpchannel.service.config.ServiceConfigurationHelper;
import com.rogiel.httpchannel.service.config.NullAuthenticatorConfiguration;
import com.rogiel.httpchannel.service.config.NullDownloaderConfiguration;
import com.rogiel.httpchannel.service.config.NullUploaderConfiguration;
import com.rogiel.httpchannel.service.exception.AuthenticationInvalidCredentialException;
import com.rogiel.httpchannel.service.impl.HotFileService.HotFileServiceConfiguration;
import com.rogiel.httpchannel.util.ThreadUtils;
import com.rogiel.httpchannel.util.htmlparser.HTMLPage;
/**
* This service handles login, upload and download to HotFile.com.
*
* @author Rogiel
* @author <a href="http://www.rogiel.com">Rogiel</a>
* @since 1.0
*/
public class HotFileService extends
AbstractHttpService<HotFileServiceConfiguration> implements Service,
UploadService, DownloadService, AuthenticationService {
public class HotFileService extends AbstractHttpService implements Service,
UploadService<NullUploaderConfiguration>,
DownloadService<NullDownloaderConfiguration>,
AuthenticationService<NullAuthenticatorConfiguration> {
/**
* This service ID
*/
public static final ServiceID SERVICE_ID = ServiceID.create("hotfile");
private static final Pattern UPLOAD_URL_PATTERN = Pattern
.compile("http://u[0-9]*\\.hotfile\\.com/upload\\.cgi\\?[0-9]*");
@@ -85,11 +84,6 @@ public class HotFileService extends
private static final Pattern DOWNLOAD_URL_PATTERN = Pattern
.compile("http://hotfile\\.com/dl/([0-9]*)/([A-Za-z0-9]*)/(.*)");
public HotFileService() {
super(ServiceConfigurationHelper
.defaultConfiguration(HotFileServiceConfiguration.class));
}
@Override
public ServiceID getID() {
return SERVICE_ID;
@@ -106,9 +100,20 @@ public class HotFileService extends
}
@Override
public Uploader getUploader(String filename, long filesize,
String description) {
return new HotFileUploader(filename, filesize);
public Uploader<NullUploaderConfiguration> getUploader(String filename,
long filesize, NullUploaderConfiguration configuration) {
return new HotFileUploader(filename, filesize, configuration);
}
@Override
public Uploader<NullUploaderConfiguration> getUploader(String filename,
long filesize) {
return getUploader(filename, filesize, newUploaderConfiguration());
}
@Override
public NullUploaderConfiguration newUploaderConfiguration() {
return NullUploaderConfiguration.SHARED_INSTANCE;
}
@Override
@@ -130,8 +135,19 @@ public class HotFileService extends
}
@Override
public Downloader getDownloader(URL url) {
return new HotFileDownloader(url);
public Downloader<NullDownloaderConfiguration> getDownloader(URL url,
NullDownloaderConfiguration configuration) {
return new HotFileDownloader(url, configuration);
}
@Override
public Downloader<NullDownloaderConfiguration> getDownloader(URL url) {
return getDownloader(url, newDownloaderConfiguration());
}
@Override
public NullDownloaderConfiguration newDownloaderConfiguration() {
return NullDownloaderConfiguration.SHARED_INSTANCE;
}
@Override
@@ -146,8 +162,20 @@ public class HotFileService extends
}
@Override
public Authenticator getAuthenticator(Credential credential) {
return new HotFileAuthenticator(credential);
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator(
Credential credential, NullAuthenticatorConfiguration configuration) {
return new HotFileAuthenticator(credential, configuration);
}
@Override
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator(
Credential credential) {
return getAuthenticator(credential, newAuthenticatorConfiguration());
}
@Override
public NullAuthenticatorConfiguration newAuthenticatorConfiguration() {
return NullAuthenticatorConfiguration.SHARED_INSTANCE;
}
@Override
@@ -155,36 +183,27 @@ public class HotFileService extends
return new CapabilityMatrix<AuthenticatorCapability>();
}
protected class HotFileUploader implements Uploader,
protected class HotFileUploader extends
AbstractUploader<NullUploaderConfiguration> implements
Uploader<NullUploaderConfiguration>,
LinkedUploadChannelCloseCallback {
private final String filename;
private final long filesize;
private Future<HTMLPage> uploadFuture;
public HotFileUploader(String filename, long filesize) {
super();
this.filename = filename;
this.filesize = filesize;
public HotFileUploader(String filename, long filesize,
NullUploaderConfiguration configuration) {
super(filename, filesize, configuration);
}
@Override
public UploadChannel upload() throws IOException {
final HTMLPage page = getAsPage("http://www.hotfile.com/");
final String action = page.getFormAction(UPLOAD_URL_PATTERN);
public UploadChannel openChannel() throws IOException {
final HTMLPage page = get("http://www.hotfile.com/").asPage();
final String action = page.findFormAction(UPLOAD_URL_PATTERN);
final LinkedUploadChannel channel = new LinkedUploadChannel(this,
filesize, filename);
final MultipartEntity entity = new MultipartEntity();
final LinkedUploadChannel channel = createLinkedChannel(this);
entity.addPart("uploads[]", new LinkedUploadChannelContentBody(
channel));
uploadFuture = postAsPageAsync(action, entity);
while (!channel.isLinked() && !uploadFuture.isDone()) {
ThreadUtils.sleep(100);
}
return channel;
uploadFuture = multipartPost(action)
.parameter("uploads[]", channel).asPageAsync();
return waitChannelLink(channel, uploadFuture);
}
@Override
@@ -199,17 +218,17 @@ public class HotFileService extends
}
}
protected class HotFileDownloader extends AbstractDownloader {
private final URL url;
public HotFileDownloader(URL url) {
this.url = url;
protected class HotFileDownloader extends
AbstractHttpDownloader<NullDownloaderConfiguration> {
public HotFileDownloader(URL url,
NullDownloaderConfiguration configuration) {
super(url, configuration);
}
@Override
public DownloadChannel download(DownloadListener listener, long position)
throws IOException {
final HTMLPage page = getAsPage(url.toString());
public DownloadChannel openChannel(DownloadListener listener,
long position) throws IOException {
final HTMLPage page = get(url).asPage();
// // try to find timer
// final String stringTimer = PatternUtils.find(DOWNLOAD_TIMER,
@@ -224,11 +243,12 @@ public class HotFileService extends
// }
final String downloadUrl = page
.getLink(DOWNLOAD_DIRECT_LINK_PATTERN);
.findLink(DOWNLOAD_DIRECT_LINK_PATTERN);
// final String tmHash = PatternUtils.find(DOWNLOAD_TMHASH_PATTERN,
// content);F
if (downloadUrl != null && downloadUrl.length() > 0) {
final HttpResponse downloadResponse = get(downloadUrl);
final HttpResponse downloadResponse = get(downloadUrl)
.request();
final String filename = FilenameUtils.getName(downloadUrl);
long contentLength = getContentLength(downloadResponse);
@@ -284,23 +304,21 @@ public class HotFileService extends
}
}
protected class HotFileAuthenticator implements Authenticator {
private final Credential credential;
public HotFileAuthenticator(Credential credential) {
this.credential = credential;
protected class HotFileAuthenticator extends
AbstractAuthenticator<NullAuthenticatorConfiguration> implements
Authenticator<NullAuthenticatorConfiguration> {
public HotFileAuthenticator(Credential credential,
NullAuthenticatorConfiguration configuration) {
super(credential, configuration);
}
@Override
public void login() throws ClientProtocolException, IOException {
final MultipartEntity entity = new MultipartEntity();
HTMLPage page = post("http://www.hotfile.com/login.php")
.parameter("returnto", "/index.php")
.parameter("user", credential.getUsername())
.parameter("pass", credential.getPassword()).asPage();
entity.addPart("returnto", new StringBody("/index.php"));
entity.addPart("user", new StringBody(credential.getUsername()));
entity.addPart("pass", new StringBody(credential.getPassword()));
HTMLPage page = postAsPage("http://www.hotfile.com/login.php",
entity);
final Tag accountTag = page.getTagByID("account");
if (accountTag == null)
throw new AuthenticationInvalidCredentialException();
@@ -308,18 +326,12 @@ public class HotFileService extends
@Override
public void logout() throws IOException {
final MultipartEntity entity = new MultipartEntity();
entity.addPart("logout", new StringBody("1"));
postAsString("http://www.megaupload.com/?c=account", entity);
post("http://www.megaupload.com/?c=account").parameter("logout",
true).request();
// TODO check logout status
}
}
public static interface HotFileServiceConfiguration extends
ServiceConfiguration {
}
@Override
public String toString() {
return this.getClass().getSimpleName() + " " + getMajorVersion() + "."

View File

@@ -20,7 +20,7 @@ import junit.framework.Assert;
import org.junit.Test;
import com.rogiel.httpchannel.service.Services;
import com.rogiel.httpchannel.service.helper.Services;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>

View File

@@ -22,8 +22,6 @@ import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.SeekableByteChannel;
@@ -38,23 +36,19 @@ import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import com.rogiel.httpchannel.service.AuthenticationService;
import com.rogiel.httpchannel.service.Credential;
import com.rogiel.httpchannel.service.DownloadChannel;
import com.rogiel.httpchannel.service.DownloadService;
import com.rogiel.httpchannel.service.Service;
import com.rogiel.httpchannel.service.ServiceHelper;
import com.rogiel.httpchannel.service.ServiceID;
import com.rogiel.httpchannel.service.Services;
import com.rogiel.httpchannel.service.UploadChannel;
import com.rogiel.httpchannel.service.UploadService;
import com.rogiel.httpchannel.service.UploaderCapability;
import com.rogiel.httpchannel.service.exception.AuthenticationInvalidCredentialException;
import com.rogiel.httpchannel.service.helper.Services;
import com.rogiel.httpchannel.service.helper.UploadServices;
import com.rogiel.httpchannel.util.ChannelUtils;
public class HotFileServiceTest {
private Service service;
private ServiceHelper helper;
private HotFileService service;
/**
* See <b>src/test/resources/config/hotfile.properties</b>
@@ -76,7 +70,6 @@ public class HotFileServiceTest {
public void setUp() throws Exception {
// MegaUploadServiceConfiguration.class;
service = new HotFileService();
helper = new ServiceHelper(service);
final Properties properties = new Properties();
properties.load(new FileInputStream(
@@ -87,33 +80,31 @@ public class HotFileServiceTest {
@Test
public void testServiceId() {
System.out.println("Service: " + service.toString());
assertEquals(ServiceID.create("hotfile"), service.getID());
}
@Test
public void testValidAuthenticator() throws IOException {
((AuthenticationService) service).getAuthenticator(
new Credential(VALID_USERNAME, VALID_PASSWORD)).login();
service.getAuthenticator(new Credential(VALID_USERNAME, VALID_PASSWORD))
.login();
}
@Test(expected = AuthenticationInvalidCredentialException.class)
public void testInvalidAuthenticator() throws IOException {
((AuthenticationService) service).getAuthenticator(
service.getAuthenticator(
new Credential(INVALID_USERNAME, INVALID_PASSWORD)).login();
}
@Test
public void testNonLoguedInUploader() throws IOException,
URISyntaxException {
public void testNonLoguedInUploader() throws IOException {
assertTrue(
"This service does not have the capability UploadCapability.FREE_UPLOAD",
((UploadService) service).getUploadCapabilities().has(
service.getUploadCapabilities().has(
UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD));
final Path path = Paths.get("src/test/resources/upload-test-file.txt");
final UploadChannel channel = helper.upload(path,
"httpchannel test upload");
final UploadChannel channel = UploadServices.upload(service, path)
.openChannel();
final SeekableByteChannel inChannel = Files.newByteChannel(path);
try {
@@ -131,15 +122,15 @@ public class HotFileServiceTest {
public void testLoguedInUploader() throws IOException {
assertTrue(
"This service does not have the capability UploadCapability.PREMIUM_UPLOAD",
((UploadService) service).getUploadCapabilities().has(
service.getUploadCapabilities().has(
UploaderCapability.PREMIUM_ACCOUNT_UPLOAD));
((AuthenticationService) service).getAuthenticator(
new Credential(VALID_USERNAME, VALID_PASSWORD)).login();
service.getAuthenticator(new Credential(VALID_USERNAME, VALID_PASSWORD))
.login();
final Path path = Paths.get("src/test/resources/upload-test-file.txt");
final UploadChannel channel = helper.upload(path,
"httpchannel test upload");
final UploadChannel channel = UploadServices.upload(service, path)
.openChannel();
final SeekableByteChannel inChannel = Files.newByteChannel(path);
try {
@@ -154,30 +145,29 @@ public class HotFileServiceTest {
}
@Test
public void testDownloader() throws IOException, MalformedURLException {
public void testDownloader() throws IOException {
final URL downloadUrl = new URL(
"http://hotfile.com/dl/129251605/9b4faf2/simulado_2010_1_res_all.zip.htm");
final DownloadService service = Services.matchURL(downloadUrl);
final DownloadService<?> service = Services.matchURL(downloadUrl);
final DownloadChannel channel = service.getDownloader(downloadUrl)
.download(null, 0);
.openChannel(null, 0);
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
IOUtils.copy(Channels.newInputStream(channel), bout);
System.out.println(bout.size());
}
@Test
public void testLoggedInDownloader() throws IOException,
MalformedURLException {
((AuthenticationService) service).getAuthenticator(
new Credential(VALID_USERNAME, VALID_PASSWORD)).login();
public void testLoggedInDownloader() throws IOException {
service.getAuthenticator(new Credential(VALID_USERNAME, VALID_PASSWORD))
.login();
final DownloadChannel channel = ((DownloadService) service)
final DownloadChannel channel = service
.getDownloader(
new URL(
"http://hotfile.com/dl/129251605/9b4faf2/simulado_2010_1_res_all.zip.html"))
.download(null, 0);
.openChannel(null, 0);
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
IOUtils.copy(Channels.newInputStream(channel), bout);

View File

@@ -1,41 +0,0 @@
/*
* This file is part of seedbox <github.com/seedbox>.
*
* seedbox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* seedbox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with seedbox. If not, see <http://www.gnu.org/licenses/>.
*/
package com.rogiel.httpchannel.service.impl;
import org.junit.Assert;
import org.junit.Test;
import com.rogiel.httpchannel.service.Service;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class ServiceCloningTest {
@Test
public void testDiscovery() {
Service original = HotFileService.SERVICE_ID.getService();
Service service = HotFileService.SERVICE_ID.getService().clone();
// set configuration to anything else
service.setServiceConfiguration(null);
Assert.assertNotSame(service, original);
Assert.assertNotSame(service.getServiceConfiguration(),
original.getServiceConfiguration());
}
}