Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
555 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>httpchannel-service</artifactId> | ||
<groupId>com.rogiel.httpchannel</groupId> | ||
<version>1.0.1-SNAPSHOT</version> | ||
<relativePath>..</relativePath> | ||
</parent> | ||
<artifactId>httpchannel-service-wupload</artifactId> | ||
<groupId>com.rogiel.httpchannel.services</groupId> | ||
<name>HttpChannel/Service/WUpload</name> | ||
<description>Provides download and upload access to wupload.com</description> | ||
</project> |
74 changes: 74 additions & 0 deletions
74
...nnel-service-wupload/src/main/java/com/rogiel/httpchannel/service/wupload/WUploadAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 com.rogiel.httpchannel.service.wupload; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
|
||
import javax.xml.bind.JAXB; | ||
|
||
import com.rogiel.httpchannel.http.HttpContext; | ||
import com.rogiel.httpchannel.service.wupload.xml.FSAPI; | ||
import com.rogiel.httpchannel.service.wupload.xml.FSGetUploadURL; | ||
import com.rogiel.httpchannel.service.wupload.xml.FSUpload; | ||
|
||
/** | ||
* @author <a href="http://www.rogiel.com">Rogiel</a> | ||
*/ | ||
public class WUploadAPI { | ||
private static final String BASE_URI = "http://api.wupload.com/"; | ||
|
||
private final HttpContext ctx; | ||
|
||
private String email; | ||
private String password; | ||
|
||
public WUploadAPI(HttpContext ctx) { | ||
this.ctx = ctx; | ||
} | ||
|
||
public URI getUploadURI() throws IOException { | ||
return URI.create((((FSGetUploadURL) execute(FSUpload.class, | ||
"upload?method=getUploadUrl").getResponse()).getResponse() | ||
.getUploadURI())); | ||
} | ||
|
||
public long getMaxFilesize() throws IOException { | ||
return ((FSGetUploadURL) execute(FSUpload.class, | ||
"upload?method=getUploadUrl").getResponse()).getResponse() | ||
.getMaxFilesize(); | ||
} | ||
|
||
public void login(String email, String password) { | ||
this.email = email; | ||
this.password = password; | ||
} | ||
|
||
public void logout() { | ||
this.email = null; | ||
this.password = null; | ||
} | ||
|
||
private <T extends FSAPI> T execute(Class<T> type, String requestURI) | ||
throws IOException { | ||
final URI uri = URI.create(BASE_URI + requestURI + "&u=" + email | ||
+ "&p=" + password + "&format=xml"); | ||
return JAXB.unmarshal(ctx.get(uri).asStream(), type); | ||
} | ||
} |
211 changes: 211 additions & 0 deletions
211
...-service-wupload/src/main/java/com/rogiel/httpchannel/service/wupload/WUploadService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 com.rogiel.httpchannel.service.wupload; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
import java.util.regex.Pattern; | ||
|
||
import com.rogiel.httpchannel.service.AbstractAuthenticator; | ||
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; | ||
import com.rogiel.httpchannel.service.CapabilityMatrix; | ||
import com.rogiel.httpchannel.service.Credential; | ||
import com.rogiel.httpchannel.service.Service; | ||
import com.rogiel.httpchannel.service.ServiceID; | ||
import com.rogiel.httpchannel.service.ServiceMode; | ||
import com.rogiel.httpchannel.service.UploadChannel; | ||
import com.rogiel.httpchannel.service.UploadService; | ||
import com.rogiel.httpchannel.service.Uploader; | ||
import com.rogiel.httpchannel.service.UploaderCapability; | ||
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel; | ||
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel.LinkedUploadChannelCloseCallback; | ||
import com.rogiel.httpchannel.service.config.NullAuthenticatorConfiguration; | ||
import com.rogiel.httpchannel.service.config.NullUploaderConfiguration; | ||
import com.rogiel.httpchannel.util.PatternUtils; | ||
|
||
/** | ||
* This service handles login, upload and download to WUpload.com. | ||
* | ||
* @author <a href="http://www.rogiel.com">Rogiel</a> | ||
* @since 1.0 | ||
*/ | ||
public class WUploadService extends AbstractHttpService implements Service, | ||
UploadService<NullUploaderConfiguration>, | ||
AuthenticationService<NullAuthenticatorConfiguration> { | ||
/** | ||
* This service ID | ||
*/ | ||
public static final ServiceID SERVICE_ID = ServiceID.create("wupload"); | ||
|
||
/** | ||
* The download URI pattern | ||
*/ | ||
private static final Pattern DOWNLOAD_URI_PATTERN = Pattern | ||
.compile("http://www\\.wupload\\.com/file/[0-9]*(/.*)?"); | ||
/** | ||
* The FileSonic API | ||
*/ | ||
private final WUploadAPI api = new WUploadAPI(http); | ||
|
||
@Override | ||
public ServiceID getServiceID() { | ||
return SERVICE_ID; | ||
} | ||
|
||
@Override | ||
public int getMajorVersion() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public int getMinorVersion() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public CapabilityMatrix<ServiceMode> getPossibleServiceModes() { | ||
return new CapabilityMatrix<ServiceMode>(ServiceMode.UNAUTHENTICATED, | ||
ServiceMode.NON_PREMIUM, ServiceMode.PREMIUM); | ||
} | ||
|
||
@Override | ||
public Uploader<NullUploaderConfiguration> getUploader(String filename, | ||
long filesize, NullUploaderConfiguration configuration) { | ||
return new UploaderImpl(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 | ||
public long getMaximumFilesize() { | ||
try { | ||
return api.getMaxFilesize(); | ||
} catch (IOException e) { | ||
return -1; | ||
} | ||
} | ||
|
||
@Override | ||
public String[] getSupportedExtensions() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public CapabilityMatrix<UploaderCapability> getUploadCapabilities() { | ||
return new CapabilityMatrix<UploaderCapability>( | ||
UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD, | ||
UploaderCapability.PREMIUM_ACCOUNT_UPLOAD); | ||
} | ||
|
||
@Override | ||
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator( | ||
Credential credential, NullAuthenticatorConfiguration configuration) { | ||
return new AuthenticatorImpl(credential, configuration); | ||
} | ||
|
||
@Override | ||
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator( | ||
Credential credential) { | ||
return getAuthenticator(credential, newAuthenticatorConfiguration()); | ||
} | ||
|
||
@Override | ||
public NullAuthenticatorConfiguration newAuthenticatorConfiguration() { | ||
return NullAuthenticatorConfiguration.SHARED_INSTANCE; | ||
} | ||
|
||
@Override | ||
public CapabilityMatrix<AuthenticatorCapability> getAuthenticationCapability() { | ||
return new CapabilityMatrix<AuthenticatorCapability>(); | ||
} | ||
|
||
protected class UploaderImpl extends | ||
AbstractUploader<NullUploaderConfiguration> implements | ||
Uploader<NullUploaderConfiguration>, | ||
LinkedUploadChannelCloseCallback { | ||
private Future<String> uploadFuture; | ||
|
||
public UploaderImpl(String filename, long filesize, | ||
NullUploaderConfiguration configuration) { | ||
super(WUploadService.this, filename, filesize, configuration); | ||
} | ||
|
||
@Override | ||
public UploadChannel openChannel() throws IOException { | ||
logger.debug("Starting upload to wupload.com"); | ||
final LinkedUploadChannel channel = createLinkedChannel(this); | ||
uploadFuture = multipartPost(api.getUploadURI().toString()) | ||
.parameter("files[]", channel).asStringAsync(); | ||
return waitChannelLink(channel, uploadFuture); | ||
} | ||
|
||
@Override | ||
public String finish() throws IOException { | ||
try { | ||
return PatternUtils.find(DOWNLOAD_URI_PATTERN, | ||
uploadFuture.get()); | ||
} catch (InterruptedException e) { | ||
return null; | ||
} catch (ExecutionException e) { | ||
throw (IOException) e.getCause(); | ||
} | ||
} | ||
} | ||
|
||
protected class AuthenticatorImpl extends | ||
AbstractAuthenticator<NullAuthenticatorConfiguration> implements | ||
Authenticator<NullAuthenticatorConfiguration> { | ||
public AuthenticatorImpl(Credential credential, | ||
NullAuthenticatorConfiguration configuration) { | ||
super(credential, configuration); | ||
} | ||
|
||
@Override | ||
public void login() throws IOException { | ||
logger.debug("Logging to wupload.com"); | ||
api.login(credential.getUsername(), credential.getPassword()); | ||
serviceMode = ServiceMode.NON_PREMIUM; | ||
} | ||
|
||
@Override | ||
public void logout() throws IOException { | ||
api.logout(); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.getClass().getSimpleName() + " " + getMajorVersion() + "." | ||
+ getMinorVersion(); | ||
} | ||
} |
Oops, something went wrong.