/app/com/atlassian/connect/play/java/remoteapps/ConnectClient.java
Java | 62 lines | 53 code | 8 blank | 1 comment | 2 complexity | c709884df6e792a6706d9c5942ed4d87 MD5 | raw file
Possible License(s): Apache-2.0
- package com.atlassian.connect.play.java.remoteapps;
- import com.atlassian.connect.play.java.util.Environment;
- import com.ning.http.client.Realm;
- import play.libs.F;
- import play.libs.WS;
- import static com.atlassian.connect.play.java.util.Utils.LOGGER;
- import static com.google.common.base.Preconditions.checkNotNull;
- import static java.lang.String.format;
- public final class ConnectClient
- {
- private final String baseUrl;
- public ConnectClient(String baseUrl)
- {
- this.baseUrl = checkNotNull(baseUrl);
- }
- public F.Promise<Boolean> install(final String uri)
- {
- // You can specify the username and password of the system administrator in conf/env.properties
- final String userName = Environment.getOptionalEnv("adminuser", "admin");
- final String password = Environment.getOptionalEnv("adminpass", "admin");
- final String postUrl = baseUrl + "/rest/atlassian-connect/latest/installer";
- final String parameters = "url=" + uri;
- LOGGER.debug(format("Posting to URL '%s', with parameters '%s' using credentials of '%s' user", postUrl, parameters, userName));
- return WS.url(postUrl)
- .setAuth(userName, password, Realm.AuthScheme.BASIC)
- .post(parameters)
- .map(new F.Function<WS.Response, Boolean>()
- {
- public Boolean apply(WS.Response response) throws Throwable
- {
- int status = response.getStatus();
- if (200 <= status && status < 300)
- {
- LOGGER.info(format("Plugin successfully installed on %s (using the Atlassian Connect REST end point).", baseUrl));
- return true;
- }
- else
- {
- LOGGER.error(format("Failed to install plugin into '%s': %d - %s", baseUrl, status, response.getStatusText()));
- return false;
- }
- }
- })
- .recover(new F.Function<Throwable, Boolean>()
- {
- @Override
- public Boolean apply(Throwable throwable) throws Throwable
- {
- LOGGER.error(format("Unable to install plugin into '%s'", baseUrl), throwable);
- return false;
- }
- });
- }
- }