Skip to content

Remove Watson references #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 13, 2019
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ venv
.factorypath
/test-output/
config
.iml
4 changes: 0 additions & 4 deletions build/bintray-config.template
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
"includePattern": "target/sdk-core-${TRAVIS_TAG}-javadoc.jar",
"uploadPattern": "com/ibm/cloud/sdk-core/${TRAVIS_TAG}/sdk-core-${TRAVIS_TAG}-javadoc.jar"
},
{
"includePattern": "target/sdk-core-${TRAVIS_TAG}-tests.jar",
"uploadPattern": "com/ibm/cloud/sdk-core/${TRAVIS_TAG}/sdk-core-${TRAVIS_TAG}-tests.jar"
},
{
"includePattern": "pom.xml",
"uploadPattern": "com/ibm/cloud/sdk-core/${TRAVIS_TAG}/sdk-core-${TRAVIS_TAG}.pom"
Expand Down
12 changes: 0 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -257,18 +257,6 @@
<!-- <configuration> <suiteXmlFiles> <suiteXmlFile>src/test/java/testng.xml</suiteXmlFile>
</suiteXmlFiles> </configuration> -->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
*/
package com.ibm.cloud.sdk.core.http;

import com.ibm.cloud.sdk.core.service.WatsonService;
import com.ibm.cloud.sdk.core.service.BaseService;
import com.ibm.cloud.sdk.core.service.security.DelegatingSSLSocketFactory;
import com.ibm.cloud.sdk.core.util.HttpLogging;

import okhttp3.ConnectionSpec;
import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;
Expand Down Expand Up @@ -52,7 +51,7 @@
public class HttpClientSingleton {
private static HttpClientSingleton instance = null;

private static final Logger LOG = Logger.getLogger(WatsonService.class.getName());
private static final Logger LOG = Logger.getLogger(BaseService.class.getName());

/**
* TrustManager for disabling SSL verification, which essentially lets everything through.
Expand Down Expand Up @@ -130,7 +129,7 @@ private void addCookieJar(final OkHttpClient.Builder builder) {
final CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);

builder.cookieJar(new WatsonCookieJar(cookieManager));
builder.cookieJar(new ServiceCookieJar(cookieManager));
}

/**
Expand Down Expand Up @@ -213,7 +212,7 @@ protected SSLSocket configureSocket(SSLSocket socket) throws IOException {
}

/**
* Creates an {@link OkHttpClient} instance with a new {@link WatsonCookieJar}.
* Creates an {@link OkHttpClient} instance with a new {@link ServiceCookieJar}.
*
* @return the client
*/
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/com/ibm/cloud/sdk/core/http/HttpHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,4 @@ public interface HttpHeaders {
* See <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.47">HTTP/1.1 documentation</a>.
*/
String WWW_AUTHENTICATE = "WWW-Authenticate";

/** The Authorization token header. */
String X_WATSON_AUTHORIZATION_TOKEN = "X-Watson-Authorization-Token";

/** Allow Watson to collect the payload. */
String X_WATSON_LEARNING_OPT_OUT = "X-Watson-Learning-Opt-Out";

/** Mark Bluemix interactions from tests. */
String X_WATSON_TEST = "X-Watson-Test";

}
15 changes: 7 additions & 8 deletions src/main/java/com/ibm/cloud/sdk/core/http/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@
*/
package com.ibm.cloud.sdk.core.http;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonObject;
import com.ibm.cloud.sdk.core.service.WatsonService;
import com.ibm.cloud.sdk.core.service.BaseService;
import com.ibm.cloud.sdk.core.util.GsonSingleton;
import com.ibm.cloud.sdk.core.util.StringHelper;
import com.ibm.cloud.sdk.core.util.Validator;

import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
* Convenience class for constructing HTTP/HTTPS requests.
*/
Expand Down Expand Up @@ -334,10 +333,10 @@ public RequestBuilder bodyContent(InputStream stream, String contentType) {
public RequestBuilder bodyContent(String contentType, Object jsonContent, Object jsonPatchContent,
InputStream nonJsonContent) {
if (contentType != null) {
if (WatsonService.isJsonMimeType(contentType)) {
if (BaseService.isJsonMimeType(contentType)) {
this.bodyContent(
GsonSingleton.getGson().toJsonTree(jsonContent).getAsJsonObject().toString(), contentType);
} else if (WatsonService.isJsonPatchMimeType(contentType)) {
} else if (BaseService.isJsonPatchMimeType(contentType)) {
this.bodyContent(
GsonSingleton.getGson().toJsonTree(jsonPatchContent).getAsJsonObject().toString(), contentType);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,27 @@
*/
package com.ibm.cloud.sdk.core.http;

import java.net.CookieHandler;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
import okhttp3.JavaNetCookieJar;

import java.net.CookieHandler;
import java.util.List;

/**
* This is an adapter that uses {@link JavaNetCookieJar} and ignore Speech to Text sessions for session less requests.
* This is an adapter that uses {@link JavaNetCookieJar}.
*
*/
public final class WatsonCookieJar implements CookieJar {
private static final String SESSIONID = "SESSIONID";
private static final String SESSIONS = "sessions";
private static final String SPEECH_TO_TEXT = "speech-to-text";
public final class ServiceCookieJar implements CookieJar {
private JavaNetCookieJar adapter;

/**
* Instantiates a new Watson cookie jar.
* Instantiates a new ServiceCookieJar.
*
* @param cookieHandler the cookie handler
*/
public WatsonCookieJar(CookieHandler cookieHandler) {
public ServiceCookieJar(CookieHandler cookieHandler) {
this.adapter = new JavaNetCookieJar(cookieHandler);
}

Expand All @@ -58,19 +54,7 @@ public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
*/
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> cookies = this.adapter.loadForRequest(url);

// TODO: Removes the SESSIONID for speech to text session lest requests
if (url.encodedPathSegments().contains(SPEECH_TO_TEXT) && !url.encodedPathSegments().contains(SESSIONS)) {
List<Cookie> sessionLessCookies = new ArrayList<Cookie>();
for (Cookie cookie : cookies) {
if (!cookie.name().equalsIgnoreCase(SESSIONID)) {
sessionLessCookies.add(cookie);
}
}
cookies = sessionLessCookies;
}
return cookies;
return this.adapter.loadForRequest(url);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
import com.ibm.cloud.sdk.core.http.HttpClientSingleton;
import com.ibm.cloud.sdk.core.http.HttpConfigOptions;
import com.ibm.cloud.sdk.core.http.HttpHeaders;
import com.ibm.cloud.sdk.core.http.HttpMediaType;
import com.ibm.cloud.sdk.core.http.HttpStatus;
import com.ibm.cloud.sdk.core.http.RequestBuilder;
import com.ibm.cloud.sdk.core.http.ResponseConverter;
import com.ibm.cloud.sdk.core.http.ServiceCall;
import com.ibm.cloud.sdk.core.http.ServiceCallback;
Expand All @@ -37,13 +35,11 @@
import com.ibm.cloud.sdk.core.service.security.IamTokenManager;
import com.ibm.cloud.sdk.core.util.CredentialUtils;
import com.ibm.cloud.sdk.core.util.RequestUtils;
import com.ibm.cloud.sdk.core.util.ResponseConverterUtils;
import jersey.repackaged.jsr166e.CompletableFuture;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Credentials;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Request.Builder;
Expand All @@ -55,24 +51,15 @@
import java.util.regex.Pattern;

/**
* Watson service abstract common functionality of various Watson Services. It handle authentication and default url.
*
* @see <a href="http://www.ibm.com/watson/developercloud/"> IBM Watson Developer Cloud</a>
* Abstracts common functionality of various IBM Cloud services.
*/
public abstract class WatsonService {
public abstract class BaseService {

private static final String URL = "url";
private static final String PATH_AUTHORIZATION_V1_TOKEN = "/v1/token";
private static final String AUTHORIZATION = "authorization";
private static final String BASIC = "Basic ";
private static final String BEARER = "Bearer ";
private static final String APIKEY_AS_USERNAME = "apikey";
private static final String ICP_PREFIX = "icp-";
private static final Logger LOG = Logger.getLogger(WatsonService.class.getName());
private static final String AUTH_HEADER_DEPRECATION_MESSAGE = "Authenticating with the X-Watson-Authorization-Token"
+ "header is deprecated. The token continues to work with Cloud Foundry services, but is not supported for "
+ "services that use Identity and Access Management (IAM) authentication. For details see the IAM "
+ "authentication section in the README.";
private static final Logger LOG = Logger.getLogger(BaseService.class.getName());
private String apiKey;
private String username;
private String password;
Expand All @@ -84,17 +71,10 @@ public abstract class WatsonService {
private OkHttpClient client;

/** The default headers. */
protected Headers defaultHeaders = null;
private Headers defaultHeaders = null;

/** The skip authentication. */
protected boolean skipAuthentication = false;

/** The Constant MESSAGE_CODE. */
protected static final String MESSAGE_CODE = "code";


/** The Constant VERSION. */
protected static final String VERSION = "version";
private boolean skipAuthentication = false;


// Regular expression for JSON-related mimetypes.
Expand All @@ -104,11 +84,11 @@ public abstract class WatsonService {
Pattern.compile("(?i)application\\/json\\-patch\\+json(;.*)?");

/**
* Instantiates a new Watson service.
* Instantiates a new IBM Cloud service.
*
* @param name the service name
*/
public WatsonService(final String name) {
public BaseService(final String name) {
this.name = name;

// file credentials take precedence
Expand Down Expand Up @@ -280,23 +260,6 @@ public String getEndPoint() {
return endPoint;
}

/**
* Gets an authorization token that can be use to authorize API calls.
*
*
* @return the token
*/
public ServiceCall<String> getToken() {
HttpUrl url = HttpUrl.parse(getEndPoint()).newBuilder()
.setPathSegment(0, AUTHORIZATION)
.addPathSegment(PATH_AUTHORIZATION_V1_TOKEN)
.build();
Request request = RequestBuilder.get(url)
.header(HttpHeaders.ACCEPT, HttpMediaType.TEXT_PLAIN).query(URL, getEndPoint()).build();

return createServiceCall(request, ResponseConverterUtils.getString());
}

/**
* Checks the status of the tokenManager.
*
Expand Down Expand Up @@ -343,10 +306,6 @@ protected void setAuthentication(final Builder builder) {
builder.addHeader(HttpHeaders.AUTHORIZATION, BEARER + accessToken);
} else if (getApiKey() == null) {
if (skipAuthentication) {
Headers currentHeaders = builder.build().headers();
if (currentHeaders.get(HttpHeaders.X_WATSON_AUTHORIZATION_TOKEN) != null) {
LOG.warning(AUTH_HEADER_DEPRECATION_MESSAGE);
}
return;
}
throw new IllegalArgumentException("apiKey or username and password were not specified");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

/**
* This package contains a collection of runtime exceptions thrown by the Watson services.
* This package contains a collection of runtime exceptions thrown by IBM Cloud services.
*/
package com.ibm.cloud.sdk.core.service.exception;

Loading