From 1aa7dda6adaf2f96ccfbb7f0d5b00a8113eab735 Mon Sep 17 00:00:00 2001 From: fighterxxl Date: Sun, 27 Jan 2019 11:27:03 +0100 Subject: [PATCH] Support for Basic Authentication for the transmission-daemon --- .../nl/stil4m/transmission/rpc/RpcClient.java | 168 ++++++++++-------- .../transmission/rpc/RpcConfiguration.java | 20 ++- 2 files changed, 109 insertions(+), 79 deletions(-) diff --git a/src/main/java/nl/stil4m/transmission/rpc/RpcClient.java b/src/main/java/nl/stil4m/transmission/rpc/RpcClient.java index 4f0d832..77b5ff2 100644 --- a/src/main/java/nl/stil4m/transmission/rpc/RpcClient.java +++ b/src/main/java/nl/stil4m/transmission/rpc/RpcClient.java @@ -5,6 +5,8 @@ import nl.stil4m.transmission.http.RequestExecutorException; import org.apache.http.HttpResponse; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; @@ -16,82 +18,92 @@ public class RpcClient { - private final RpcConfiguration configuration; - private final ObjectMapper objectMapper; - private final Map headers; - private final DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); - - private final RequestExecutor requestExecutor; - - public RpcClient(RpcConfiguration configuration, ObjectMapper objectMapper) { - this.requestExecutor = new RequestExecutor(objectMapper, configuration, defaultHttpClient); - this.configuration = configuration; - this.objectMapper = objectMapper; - headers = new HashMap<>(); - } - - public void execute(RpcCommand command, Map h) throws RpcException { - try { - executeCommandInner(command, h); - } catch (RequestExecutorException | IOException e) { - throw new RpcException(e); - } catch (InvalidResponseStatus e) { - setup(); - try { - executeCommandInner(command, h); - } catch (Exception | RequestExecutorException | InvalidResponseStatus inner) { - throw new RpcException(inner); - } - } - } - - private void executeCommandInner(RpcCommand command, Map h) throws RequestExecutorException, InvalidResponseStatus, IOException, RpcException { - for (Map.Entry entry : h.entrySet()) { - requestExecutor.removeAllHeaders(); - requestExecutor.configureHeader(entry.getKey(), entry.getValue()); - } - - RpcRequest request = command.buildRequestPayload(); - RpcResponse response = requestExecutor.execute(request, RpcResponse.class, 200); - - Map args = (Map) response.getArguments(); - String stringValue = objectMapper.writeValueAsString(args); - response.setArguments((V) objectMapper.readValue(stringValue, command.getArgumentsObject())); - if (!command.getTag().equals(response.getTag())) { - throw new RpcException(String.format("Invalid response tag. Expected %d, but got %d", command.getTag(), request.getTag())); - } - command.setResponse(response); - - if (!"success".equals(response.getResult())) { - throw new RpcException("Rpc Command Failed: " + response.getResult(), command); - } - } - - private void setup() throws RpcException { - try { - DefaultHttpClient defaultHttpClient = getClient(); - HttpPost httpPost = createPost(); - HttpResponse result = defaultHttpClient.execute(httpPost); - putSessionHeader(result); - EntityUtils.consume(result.getEntity()); - } catch (IOException e) { - throw new RpcException(e); - } - } - - protected HttpPost createPost() { - return new HttpPost(configuration.getHost()); - } - - protected DefaultHttpClient getClient() { - return defaultHttpClient; - } - - public void executeWithHeaders(RpcCommand command) throws RpcException { - execute(command, headers); - } - - private void putSessionHeader(HttpResponse result) { - headers.put("X-Transmission-Session-Id", result.getFirstHeader("X-Transmission-Session-Id").getValue()); - } + private final RpcConfiguration configuration; + private final ObjectMapper objectMapper; + private final Map headers; + private final DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); + + private final RequestExecutor requestExecutor; + + public RpcClient(RpcConfiguration configuration, ObjectMapper objectMapper) { + this.requestExecutor = new RequestExecutor(objectMapper, configuration, defaultHttpClient); + this.configuration = configuration; + this.objectMapper = objectMapper; + headers = new HashMap<>(); + + if (configuration.getUsername() != null && configuration.getPassword() != null) { + + getClient().getCredentialsProvider() // + .setCredentials(new AuthScope(configuration.getHost().getHost(), configuration.getHost().getPort()), + new UsernamePasswordCredentials(configuration.getUsername(), configuration.getPassword())); + + } + } + + public void execute(RpcCommand command, Map h) throws RpcException { + try { + executeCommandInner(command, h); + } catch (RequestExecutorException | IOException e) { + throw new RpcException(e); + } catch (InvalidResponseStatus e) { + setup(); + try { + executeCommandInner(command, h); + } catch (Exception | RequestExecutorException | InvalidResponseStatus inner) { + throw new RpcException(inner); + } + } + } + + private void executeCommandInner(RpcCommand command, Map h) + throws RequestExecutorException, InvalidResponseStatus, IOException, RpcException { + for (Map.Entry entry : h.entrySet()) { + requestExecutor.removeAllHeaders(); + requestExecutor.configureHeader(entry.getKey(), entry.getValue()); + } + + RpcRequest request = command.buildRequestPayload(); + RpcResponse response = requestExecutor.execute(request, RpcResponse.class, 200); + + Map args = (Map) response.getArguments(); + String stringValue = objectMapper.writeValueAsString(args); + response.setArguments((V) objectMapper.readValue(stringValue, command.getArgumentsObject())); + if (!command.getTag().equals(response.getTag())) { + throw new RpcException( + String.format("Invalid response tag. Expected %d, but got %d", command.getTag(), request.getTag())); + } + command.setResponse(response); + + if (!"success".equals(response.getResult())) { + throw new RpcException("Rpc Command Failed: " + response.getResult(), command); + } + } + + private void setup() throws RpcException { + try { + DefaultHttpClient defaultHttpClient = getClient(); + HttpPost httpPost = createPost(); + HttpResponse result = defaultHttpClient.execute(httpPost); + putSessionHeader(result); + EntityUtils.consume(result.getEntity()); + } catch (IOException e) { + throw new RpcException(e); + } + } + + protected HttpPost createPost() { + return new HttpPost(configuration.getHost()); + } + + protected DefaultHttpClient getClient() { + return defaultHttpClient; + } + + public void executeWithHeaders(RpcCommand command) throws RpcException { + execute(command, headers); + } + + private void putSessionHeader(HttpResponse result) { + headers.put("X-Transmission-Session-Id", result.getFirstHeader("X-Transmission-Session-Id").getValue()); + } } diff --git a/src/main/java/nl/stil4m/transmission/rpc/RpcConfiguration.java b/src/main/java/nl/stil4m/transmission/rpc/RpcConfiguration.java index 1cabca9..f4a9e83 100644 --- a/src/main/java/nl/stil4m/transmission/rpc/RpcConfiguration.java +++ b/src/main/java/nl/stil4m/transmission/rpc/RpcConfiguration.java @@ -7,7 +7,9 @@ public class RpcConfiguration implements HostConfiguration { private URI host; - + private String username = null; + private String password = null; + public URI getHost() { return host; } @@ -15,4 +17,20 @@ public URI getHost() { public void setHost(URI host) { this.host = host; } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } }