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

Simplified API

This commit is contained in:
2011-09-08 11:20:56 -03:00
parent abf1edf39c
commit da1e2c46b7
10 changed files with 74 additions and 139 deletions

View File

@@ -31,17 +31,15 @@ public interface Authenticator {
* <b>Note</b>: If you want to logout the user, see * <b>Note</b>: If you want to logout the user, see
* {@link Authenticator#logout()} * {@link Authenticator#logout()}
* *
* @param listener * @return true if login was successful
* the listener do keep a track on the authentication progress
*/ */
boolean login(AuthenticatorListener listener) throws IOException; boolean login() throws IOException;
/** /**
* Logout into the {@link Service}. The session is restored to an not * Logout into the {@link Service}. The session is restored to an not
* logged-in state. * logged-in state.
* *
* @param listener * @return true if logout was successful
* the listener do keep a track on the logout progress
*/ */
boolean logout(AuthenticatorListener listener) throws IOException; boolean logout() throws IOException;
} }

View File

@@ -1,26 +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;
/**
* This listener keeps an track on the Authentication process.
*
* @author Rogiel
* @since 1.0
*/
public interface AuthenticatorListener {
}

View File

@@ -45,7 +45,7 @@ public interface DownloadService extends Service {
* might or might not perform network activity. * might or might not perform network activity.
* *
* @param url * @param url
* the url to be tested. * the {@link URL} to be tested.
* @return true if supported, false otherwise. * @return true if supported, false otherwise.
*/ */
boolean matchURL(URL url); boolean matchURL(URL url);

View File

@@ -1,29 +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;
/**
* This listener keeps an track on the progress on an {@link Uploader} service.
*
* @author Rogiel
* @since 1.0
*/
public interface UploadListener {
long getFilesize();
String getFilename();
}

View File

@@ -34,7 +34,7 @@ public interface UploadService extends Service {
* the description of the upload. If supported. * the description of the upload. If supported.
* @return the new {@link Uploader} instance * @return the new {@link Uploader} instance
*/ */
Uploader getUploader(String description); Uploader getUploader(String filename, long filesize, String description);
/** /**
* Get the maximum upload file size supported by this service. * Get the maximum upload file size supported by this service.
@@ -43,6 +43,15 @@ public interface UploadService extends Service {
*/ */
long getMaximumFilesize(); long getMaximumFilesize();
/**
* Get the list of all supported extensions. Might return <tt>null</tt> if
* there is no restriction.
*
* @return the list of supported file extensions. Can return <tt>null</tt>
* if there is not restriction
*/
String[] getSupportedExtensions();
/** /**
* Return the matrix of capabilities for this {@link Uploader}. * Return the matrix of capabilities for this {@link Uploader}.
* *

View File

@@ -34,5 +34,5 @@ public interface Uploader {
* thrown if something went wrong * thrown if something went wrong
* @throws IOException * @throws IOException
*/ */
UploadChannel upload(UploadListener listener) throws IOException; UploadChannel upload() throws IOException;
} }

View File

@@ -37,7 +37,6 @@ import com.rogiel.httpchannel.service.AbstractHttpService;
import com.rogiel.httpchannel.service.AuthenticationService; import com.rogiel.httpchannel.service.AuthenticationService;
import com.rogiel.httpchannel.service.Authenticator; import com.rogiel.httpchannel.service.Authenticator;
import com.rogiel.httpchannel.service.AuthenticatorCapability; import com.rogiel.httpchannel.service.AuthenticatorCapability;
import com.rogiel.httpchannel.service.AuthenticatorListener;
import com.rogiel.httpchannel.service.CapabilityMatrix; import com.rogiel.httpchannel.service.CapabilityMatrix;
import com.rogiel.httpchannel.service.Credential; import com.rogiel.httpchannel.service.Credential;
import com.rogiel.httpchannel.service.DownloadChannel; import com.rogiel.httpchannel.service.DownloadChannel;
@@ -47,7 +46,6 @@ import com.rogiel.httpchannel.service.Downloader;
import com.rogiel.httpchannel.service.DownloaderCapability; import com.rogiel.httpchannel.service.DownloaderCapability;
import com.rogiel.httpchannel.service.Service; import com.rogiel.httpchannel.service.Service;
import com.rogiel.httpchannel.service.UploadChannel; import com.rogiel.httpchannel.service.UploadChannel;
import com.rogiel.httpchannel.service.UploadListener;
import com.rogiel.httpchannel.service.UploadListenerContentBody; import com.rogiel.httpchannel.service.UploadListenerContentBody;
import com.rogiel.httpchannel.service.UploadService; import com.rogiel.httpchannel.service.UploadService;
import com.rogiel.httpchannel.service.Uploader; import com.rogiel.httpchannel.service.Uploader;
@@ -103,8 +101,9 @@ public class HotFileService extends
} }
@Override @Override
public Uploader getUploader(String description) { public Uploader getUploader(String filename, long filesize,
return new HotFileUploader(); String description) {
return new HotFileUploader(filename, filesize);
} }
@Override @Override
@@ -112,6 +111,11 @@ public class HotFileService extends
return 1 * 1024 * 1024 * 1024; return 1 * 1024 * 1024 * 1024;
} }
@Override
public String[] getSupportedExtensions() {
return null;
}
@Override @Override
public CapabilityMatrix<UploaderCapability> getUploadCapabilities() { public CapabilityMatrix<UploaderCapability> getUploadCapabilities() {
return new CapabilityMatrix<UploaderCapability>( return new CapabilityMatrix<UploaderCapability>(
@@ -150,10 +154,19 @@ public class HotFileService extends
protected class HotFileUploader implements Uploader, protected class HotFileUploader implements Uploader,
LinkedUploadChannelCloseCallback { LinkedUploadChannelCloseCallback {
private final String filename;
private final long filesize;
private Future<String> uploadFuture; private Future<String> uploadFuture;
public HotFileUploader(String filename, long filesize) {
super();
this.filename = filename;
this.filesize = filesize;
}
@Override @Override
public UploadChannel upload(UploadListener listener) throws IOException { public UploadChannel upload() throws IOException {
final String body = HttpClientUtils.get(client, final String body = HttpClientUtils.get(client,
"http://www.hotfile.com/"); "http://www.hotfile.com/");
final String url = PatternUtils.find(UPLOAD_URL_PATTERN, body); final String url = PatternUtils.find(UPLOAD_URL_PATTERN, body);
@@ -163,7 +176,7 @@ public class HotFileService extends
upload.setEntity(entity); upload.setEntity(entity);
final LinkedUploadChannel channel = new LinkedUploadChannel(this, final LinkedUploadChannel channel = new LinkedUploadChannel(this,
listener.getFilesize(), listener.getFilename()); filesize, filename);
entity.addPart("uploads[]", new UploadListenerContentBody(channel)); entity.addPart("uploads[]", new UploadListenerContentBody(channel));
@@ -246,8 +259,7 @@ public class HotFileService extends
} }
@Override @Override
public boolean login(AuthenticatorListener listener) public boolean login() throws ClientProtocolException, IOException {
throws ClientProtocolException, IOException {
final HttpPost login = new HttpPost( final HttpPost login = new HttpPost(
"http://www.hotfile.com/login.php"); "http://www.hotfile.com/login.php");
final MultipartEntity entity = new MultipartEntity(); final MultipartEntity entity = new MultipartEntity();
@@ -265,8 +277,7 @@ public class HotFileService extends
} }
@Override @Override
public boolean logout(AuthenticatorListener listener) public boolean logout() throws IOException {
throws IOException {
final HttpPost logout = new HttpPost( final HttpPost logout = new HttpPost(
"http://www.megaupload.com/?c=account"); "http://www.megaupload.com/?c=account");
final MultipartEntity entity = new MultipartEntity(); final MultipartEntity entity = new MultipartEntity();

View File

@@ -37,7 +37,6 @@ import com.rogiel.httpchannel.service.AbstractHttpService;
import com.rogiel.httpchannel.service.AuthenticationService; import com.rogiel.httpchannel.service.AuthenticationService;
import com.rogiel.httpchannel.service.Authenticator; import com.rogiel.httpchannel.service.Authenticator;
import com.rogiel.httpchannel.service.AuthenticatorCapability; import com.rogiel.httpchannel.service.AuthenticatorCapability;
import com.rogiel.httpchannel.service.AuthenticatorListener;
import com.rogiel.httpchannel.service.CapabilityMatrix; import com.rogiel.httpchannel.service.CapabilityMatrix;
import com.rogiel.httpchannel.service.Credential; import com.rogiel.httpchannel.service.Credential;
import com.rogiel.httpchannel.service.DownloadChannel; import com.rogiel.httpchannel.service.DownloadChannel;
@@ -47,7 +46,6 @@ import com.rogiel.httpchannel.service.Downloader;
import com.rogiel.httpchannel.service.DownloaderCapability; import com.rogiel.httpchannel.service.DownloaderCapability;
import com.rogiel.httpchannel.service.Service; import com.rogiel.httpchannel.service.Service;
import com.rogiel.httpchannel.service.UploadChannel; import com.rogiel.httpchannel.service.UploadChannel;
import com.rogiel.httpchannel.service.UploadListener;
import com.rogiel.httpchannel.service.UploadListenerContentBody; import com.rogiel.httpchannel.service.UploadListenerContentBody;
import com.rogiel.httpchannel.service.UploadService; import com.rogiel.httpchannel.service.UploadService;
import com.rogiel.httpchannel.service.Uploader; import com.rogiel.httpchannel.service.Uploader;
@@ -104,8 +102,9 @@ public class MegaUploadService extends
} }
@Override @Override
public Uploader getUploader(String description) { public Uploader getUploader(String filename, long filesize,
return new MegaUploadUploader(description); String description) {
return new MegaUploadUploader(filename, filesize, description);
} }
@Override @Override
@@ -113,6 +112,11 @@ public class MegaUploadService extends
return 1 * 1024 * 1024 * 1024; return 1 * 1024 * 1024 * 1024;
} }
@Override
public String[] getSupportedExtensions() {
return null;
}
@Override @Override
public CapabilityMatrix<UploaderCapability> getUploadCapabilities() { public CapabilityMatrix<UploaderCapability> getUploadCapabilities() {
return new CapabilityMatrix<UploaderCapability>( return new CapabilityMatrix<UploaderCapability>(
@@ -151,17 +155,22 @@ public class MegaUploadService extends
protected class MegaUploadUploader implements Uploader, protected class MegaUploadUploader implements Uploader,
LinkedUploadChannelCloseCallback { LinkedUploadChannelCloseCallback {
private final String filename;
private final long filesize;
private final String description; private final String description;
private Future<String> uploadFuture; private Future<String> uploadFuture;
public MegaUploadUploader(String description) { public MegaUploadUploader(String filename, long filesize,
String description) {
this.filename = filename;
this.filesize = filesize;
this.description = (description != null ? description this.description = (description != null ? description
: configuration.getDefaultUploadDescription()); : configuration.getDefaultUploadDescription());
} }
@Override @Override
public UploadChannel upload(UploadListener listener) throws IOException { public UploadChannel upload() throws IOException {
final String body = HttpClientUtils.get(client, final String body = HttpClientUtils.get(client,
"http://www.megaupload.com/multiupload/"); "http://www.megaupload.com/multiupload/");
final String url = PatternUtils.find(UPLOAD_URL_PATTERN, body); final String url = PatternUtils.find(UPLOAD_URL_PATTERN, body);
@@ -171,7 +180,7 @@ public class MegaUploadService extends
upload.setEntity(entity); upload.setEntity(entity);
final LinkedUploadChannel channel = new LinkedUploadChannel(this, final LinkedUploadChannel channel = new LinkedUploadChannel(this,
listener.getFilesize(), listener.getFilename()); filesize, filename);
entity.addPart("multifile_0", entity.addPart("multifile_0",
new UploadListenerContentBody(channel)); new UploadListenerContentBody(channel));
@@ -265,7 +274,7 @@ public class MegaUploadService extends
} }
@Override @Override
public boolean login(AuthenticatorListener listener) throws IOException { public boolean login() throws IOException {
final HttpPost login = new HttpPost( final HttpPost login = new HttpPost(
"http://www.megaupload.com/?c=login"); "http://www.megaupload.com/?c=login");
final MultipartEntity entity = new MultipartEntity(); final MultipartEntity entity = new MultipartEntity();
@@ -283,7 +292,7 @@ public class MegaUploadService extends
} }
@Override @Override
public boolean logout(AuthenticatorListener listener) throws IOException { public boolean logout() throws IOException {
final HttpPost logout = new HttpPost( final HttpPost logout = new HttpPost(
"http://www.megaupload.com/?c=account"); "http://www.megaupload.com/?c=account");
final MultipartEntity entity = new MultipartEntity(); final MultipartEntity entity = new MultipartEntity();

View File

@@ -45,13 +45,11 @@ import com.rogiel.httpchannel.service.DownloadListener;
import com.rogiel.httpchannel.service.DownloadService; import com.rogiel.httpchannel.service.DownloadService;
import com.rogiel.httpchannel.service.Service; import com.rogiel.httpchannel.service.Service;
import com.rogiel.httpchannel.service.UploadChannel; import com.rogiel.httpchannel.service.UploadChannel;
import com.rogiel.httpchannel.service.UploadListener;
import com.rogiel.httpchannel.service.UploadService; import com.rogiel.httpchannel.service.UploadService;
import com.rogiel.httpchannel.service.UploaderCapability; import com.rogiel.httpchannel.service.UploaderCapability;
import com.rogiel.httpchannel.service.captcha.Captcha; import com.rogiel.httpchannel.service.captcha.Captcha;
import com.rogiel.httpchannel.service.config.ServiceConfigurationHelper; import com.rogiel.httpchannel.service.config.ServiceConfigurationHelper;
import com.rogiel.httpchannel.service.impl.HotFileService.HotFileServiceConfiguration; import com.rogiel.httpchannel.service.impl.HotFileService.HotFileServiceConfiguration;
import com.rogiel.httpchannel.service.impl.MegaUploadService.MegaUploadServiceConfiguration;
public class HotFileServiceTest { public class HotFileServiceTest {
private Service service; private Service service;
@@ -95,13 +93,13 @@ public class HotFileServiceTest {
@Test @Test
public void testValidAuthenticator() throws IOException { public void testValidAuthenticator() throws IOException {
Assert.assertTrue(((AuthenticationService) service).getAuthenticator( Assert.assertTrue(((AuthenticationService) service).getAuthenticator(
new Credential(VALID_USERNAME, VALID_PASSWORD)).login(null)); new Credential(VALID_USERNAME, VALID_PASSWORD)).login());
} }
@Test @Test
public void testInvalidAuthenticator() throws IOException { public void testInvalidAuthenticator() throws IOException {
Assert.assertFalse(((AuthenticationService) service).getAuthenticator( Assert.assertFalse(((AuthenticationService) service).getAuthenticator(
new Credential(INVALID_USERNAME, INVALID_PASSWORD)).login(null)); new Credential(INVALID_USERNAME, INVALID_PASSWORD)).login());
} }
@Test @Test
@@ -111,17 +109,9 @@ public class HotFileServiceTest {
((UploadService) service).getUploadCapabilities().has( ((UploadService) service).getUploadCapabilities().has(
UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD)); UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD));
final UploadChannel channel = ((UploadService) service).getUploader( final UploadChannel channel = ((UploadService) service).getUploader(
"Upload by httpchannel").upload(new UploadListener() { "simulado_2010_1_res_all.zip",
@Override new File("simulado_2010_1_res_all.zip").length(), null)
public long getFilesize() { .upload();
return new File("simulado_2010_1_res_all.zip").length();
}
@Override
public String getFilename() {
return "simulado_2010_1_res_all.zip";
}
});
final FileChannel fileChannel = new FileInputStream( final FileChannel fileChannel = new FileInputStream(
"simulado_2010_1_res_all.zip").getChannel(); "simulado_2010_1_res_all.zip").getChannel();
@@ -141,20 +131,12 @@ public class HotFileServiceTest {
UploaderCapability.PREMIUM_ACCOUNT_UPLOAD)); UploaderCapability.PREMIUM_ACCOUNT_UPLOAD));
Assert.assertTrue(((AuthenticationService) service).getAuthenticator( Assert.assertTrue(((AuthenticationService) service).getAuthenticator(
new Credential(VALID_USERNAME, VALID_PASSWORD)).login(null)); new Credential(VALID_USERNAME, VALID_PASSWORD)).login());
final UploadChannel channel = ((UploadService) service).getUploader( final UploadChannel channel = ((UploadService) service).getUploader(
"Upload by httpchannel").upload(new UploadListener() { "simulado_2010_1_res_all.zip",
@Override new File("simulado_2010_1_res_all.zip").length(), null)
public long getFilesize() { .upload();
return new File("simulado_2010_1_res_all.zip").length();
}
@Override
public String getFilename() {
return "simulado_2010_1_res_all.zip";
}
});
final FileChannel fileChannel = new FileInputStream( final FileChannel fileChannel = new FileInputStream(
"simulado_2010_1_res_all.zip").getChannel(); "simulado_2010_1_res_all.zip").getChannel();
@@ -195,10 +177,12 @@ public class HotFileServiceTest {
public void testLoggedInDownloader() throws IOException, public void testLoggedInDownloader() throws IOException,
MalformedURLException { MalformedURLException {
Assert.assertTrue(((AuthenticationService) service).getAuthenticator( Assert.assertTrue(((AuthenticationService) service).getAuthenticator(
new Credential(VALID_USERNAME, VALID_PASSWORD)).login(null)); new Credential(VALID_USERNAME, VALID_PASSWORD)).login());
final DownloadChannel channel = ((DownloadService) service) final DownloadChannel channel = ((DownloadService) service)
.getDownloader(new URL("http://hotfile.com/dl/129251605/9b4faf2/simulado_2010_1_res_all.zip.html")) .getDownloader(
new URL(
"http://hotfile.com/dl/129251605/9b4faf2/simulado_2010_1_res_all.zip.html"))
.download(new DownloadListener() { .download(new DownloadListener() {
@Override @Override
public boolean timer(long time, TimerWaitReason reason) { public boolean timer(long time, TimerWaitReason reason) {

View File

@@ -42,7 +42,6 @@ import com.rogiel.httpchannel.service.DownloadListener;
import com.rogiel.httpchannel.service.DownloadService; import com.rogiel.httpchannel.service.DownloadService;
import com.rogiel.httpchannel.service.Service; import com.rogiel.httpchannel.service.Service;
import com.rogiel.httpchannel.service.UploadChannel; import com.rogiel.httpchannel.service.UploadChannel;
import com.rogiel.httpchannel.service.UploadListener;
import com.rogiel.httpchannel.service.UploadService; import com.rogiel.httpchannel.service.UploadService;
import com.rogiel.httpchannel.service.UploaderCapability; import com.rogiel.httpchannel.service.UploaderCapability;
import com.rogiel.httpchannel.service.captcha.Captcha; import com.rogiel.httpchannel.service.captcha.Captcha;
@@ -91,13 +90,13 @@ public class MegaUploadServiceTest {
@Test @Test
public void testValidAuthenticator() throws IOException { public void testValidAuthenticator() throws IOException {
Assert.assertTrue(((AuthenticationService) service).getAuthenticator( Assert.assertTrue(((AuthenticationService) service).getAuthenticator(
new Credential(VALID_USERNAME, VALID_PASSWORD)).login(null)); new Credential(VALID_USERNAME, VALID_PASSWORD)).login());
} }
@Test @Test
public void testInvalidAuthenticator() throws IOException { public void testInvalidAuthenticator() throws IOException {
Assert.assertFalse(((AuthenticationService) service).getAuthenticator( Assert.assertFalse(((AuthenticationService) service).getAuthenticator(
new Credential(INVALID_USERNAME, INVALID_PASSWORD)).login(null)); new Credential(INVALID_USERNAME, INVALID_PASSWORD)).login());
} }
@Test @Test
@@ -107,17 +106,7 @@ public class MegaUploadServiceTest {
((UploadService) service).getUploadCapabilities().has( ((UploadService) service).getUploadCapabilities().has(
UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD)); UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD));
final UploadChannel channel = ((UploadService) service).getUploader( final UploadChannel channel = ((UploadService) service).getUploader(
"Upload by httpchannel").upload(new UploadListener() { "test.bin", 10, "Upload by httpchannel").upload();
@Override
public long getFilesize() {
return 10;
}
@Override
public String getFilename() {
return "test.bin";
}
});
final ByteBuffer buffer = ByteBuffer.allocate(10); final ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put((byte) 0x00); buffer.put((byte) 0x00);
buffer.put((byte) 0x00); buffer.put((byte) 0x00);
@@ -145,20 +134,10 @@ public class MegaUploadServiceTest {
UploaderCapability.PREMIUM_ACCOUNT_UPLOAD)); UploaderCapability.PREMIUM_ACCOUNT_UPLOAD));
Assert.assertTrue(((AuthenticationService) service).getAuthenticator( Assert.assertTrue(((AuthenticationService) service).getAuthenticator(
new Credential(VALID_USERNAME, VALID_PASSWORD)).login(null)); new Credential(VALID_USERNAME, VALID_PASSWORD)).login());
final UploadChannel channel = ((UploadService) service).getUploader( final UploadChannel channel = ((UploadService) service).getUploader(
"Upload by httpchannel").upload(new UploadListener() { "test.bin", 10, "Upload by httpchannel").upload();
@Override
public long getFilesize() {
return 10;
}
@Override
public String getFilename() {
return "test.bin";
}
});
final ByteBuffer buffer = ByteBuffer.allocate(10); final ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put((byte) 0x00); buffer.put((byte) 0x00);
buffer.put((byte) 0x00); buffer.put((byte) 0x00);