Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import nl.stil4m.transmission.rpc.RpcCommand;

public class FreeSpaceCommand extends RpcCommand<FreeSpacePath, FreeSpaceResult>{
public class FreeSpaceCommand extends RpcCommand<FreeSpacePath, FreeSpaceResult> {

public FreeSpaceCommand(Long tag) {
super(tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class TorrentActionCommand extends RpcCommand<Ids, Object> {

private final Map<TorrentAction, String> torrentActionMap = new HashMap<>();
private final String method;

{
torrentActionMap.put(TorrentAction.START, "torrent-start");
Expand All @@ -19,8 +20,6 @@ public class TorrentActionCommand extends RpcCommand<Ids, Object> {
torrentActionMap.put(TorrentAction.REANNOUNCE, "torrent-reannounce");
}

private final String method;

public TorrentActionCommand(Long tag, TorrentAction action) {
super(tag);
method = torrentActionMap.get(action);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import nl.stil4m.transmission.api.domain.ids.Ids;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package nl.stil4m.transmission.api.domain;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nl.stil4m.transmission.api.domain;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import nl.stil4m.transmission.api.domain.ids.Ids;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public enum TorrentStatus {
DOWNLOAD_WAIT(3L), // Queued to download
DOWNLOADING(4L), // Downloading
SEED_WAIT(5L), // Queued to seed
SEED (6L), // Seeding
SEED(6L), // Seeding
ISOLATED(7L); // Torrent can't find peers

private Long longValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ public InvalidResponseStatus(int statusCode) {
public int getStatusCode() {
return statusCode;
}

@Override
public String getMessage() {
return String.valueOf(statusCode);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nl.stil4m.transmission.http;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/nl/stil4m/transmission/rpc/RpcClient.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package nl.stil4m.transmission.rpc;

import com.fasterxml.jackson.databind.ObjectMapper;

import nl.stil4m.transmission.http.InvalidResponseStatus;
import nl.stil4m.transmission.http.RequestExecutor;
import nl.stil4m.transmission.http.RequestExecutorException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
Expand Down Expand Up @@ -53,8 +51,9 @@ public <T, V> void execute(RpcCommand<T, V> command, Map<String, String> h) thro
}

private <T, V> void executeCommandInner(RpcCommand<T, V> command, Map<String, String> h) throws RequestExecutorException, InvalidResponseStatus, IOException, RpcException {
requestExecutor.removeAllHeaders();
for (Map.Entry<String, String> entry : h.entrySet()) {
requestExecutor.removeAllHeaders();

requestExecutor.configureHeader(entry.getKey(), entry.getValue());
}

Expand All @@ -77,7 +76,16 @@ private <T, V> void executeCommandInner(RpcCommand<T, V> command, Map<String, St
private void setup() throws RpcException {
try {
HttpPost httpPost = createPost();
if (configuration.hasAuthInfo()) {
String auth = "Basic " + configuration.getEncodedAuthInfo();
httpPost.addHeader("Authorization", auth);
headers.put("Authorization", auth);
}
HttpResponse result = defaultHttpClient.execute(httpPost);
if (result.getStatusLine().getStatusCode() == 401) {
throw new IOException("Connection needs authentication");

}
putSessionHeader(result);
EntityUtils.consume(result.getEntity());
} catch (IOException e) {
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/nl/stil4m/transmission/rpc/RpcCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
public abstract class RpcCommand<T, V> {

private final Class targetArgclass;
private T requestArguments;

public abstract String getMethod();

private final Long tag;

private T requestArguments;
private RpcRequest<T> request;
private RpcResponse<V> response;

Expand All @@ -27,6 +23,8 @@ public RpcCommand(Long tag) {
request = new RpcRequest<T>();
}

public abstract String getMethod();

public RpcRequest<T> buildRequestPayload() {
request.setTag(tag);
request.setMethod(getMethod());
Expand All @@ -42,14 +40,14 @@ public Long getTag() {
return tag;
}

public void setResponse(RpcResponse<V> response) {
this.response = response;
}

public RpcResponse<V> getResponse() {
return response;
}

public void setResponse(RpcResponse<V> response) {
this.response = response;
}

public Class getArgumentsObject() {
return targetArgclass;
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/nl/stil4m/transmission/rpc/RpcConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@
import nl.stil4m.transmission.http.HostConfiguration;

import java.net.URI;
import java.util.Base64;

public class RpcConfiguration implements HostConfiguration {

private URI host;
private String authInfo = null;

public RpcConfiguration(URI host) {
this.host = host;
}

public RpcConfiguration(URI host, String username, String password) {
this.host = host;
setAuthInfo(username, password);
}

public RpcConfiguration() {
}

public URI getHost() {
return host;
Expand All @@ -15,4 +29,19 @@ public URI getHost() {
public void setHost(URI host) {
this.host = host;
}


public void setAuthInfo(String username, String password) {
this.authInfo = Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
}


public String getEncodedAuthInfo() {
return authInfo;
}

public boolean hasAuthInfo() {
return authInfo != null;
}
//header Authorization: Basic %s:%s
}
24 changes: 12 additions & 12 deletions src/main/java/nl/stil4m/transmission/rpc/RpcRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ public class RpcRequest<T extends Object> {

private Long tag;

public void setMethod(String method) {
this.method = method;
}

public void setArguments(T arguments) {
this.arguments = arguments;
}

public void setTag(Long tag) {
this.tag = tag;
}

public String getMethod() {
return method;
}

public void setMethod(String method) {
this.method = method;
}

public T getArguments() {
return arguments;
}

public void setArguments(T arguments) {
this.arguments = arguments;
}

public Long getTag() {
return tag;
}

public void setTag(Long tag) {
this.tag = tag;
}
}
1 change: 1 addition & 0 deletions src/main/java/nl/stil4m/transmission/rpc/TagProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class TagProvider {
public TagProvider() {
tag = 0L;
}

public Long nextTag() {
return ++tag;
}
Expand Down