Skip to content
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

defect #2351117: use IdePluginsOctaneHttpClient instead of GoogleHttpClient and NetHttpTransport #81

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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 @@ -28,18 +28,18 @@
******************************************************************************/
package com.hpe.adm.octane.ideplugins.services;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.hpe.adm.nga.sdk.Octane;
import com.hpe.adm.nga.sdk.network.OctaneHttpClient;
import com.hpe.adm.nga.sdk.network.OctaneHttpRequest;
import com.hpe.adm.nga.sdk.network.OctaneHttpResponse;
import com.hpe.adm.nga.sdk.query.Query;
import com.hpe.adm.nga.sdk.query.QueryMethod;
import com.hpe.adm.octane.ideplugins.services.connection.ConnectionSettings;
import com.hpe.adm.octane.ideplugins.services.connection.IdePluginsOctaneHttpClient;
import com.hpe.adm.octane.ideplugins.services.connection.granttoken.GrantTokenAuthentication;
import com.hpe.adm.octane.ideplugins.services.exception.ServiceException;
import com.hpe.adm.octane.ideplugins.services.filtering.Entity;
import com.hpe.adm.octane.ideplugins.services.nonentity.OctaneVersionService;
import com.hpe.adm.octane.ideplugins.services.util.ClientType;

/**
Expand All @@ -61,13 +61,13 @@ public Octane getOctane(ConnectionSettings connectionSettings) {

public void testHttpConnection(ConnectionSettings connectionSettings) throws ServiceException {
try {
HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
HttpRequest httpRequest = HTTP_TRANSPORT.createRequestFactory()
.buildGetRequest(new GenericUrl(connectionSettings.getBaseUrl() + "/admin/server/version"));
int statusCode = httpRequest.execute().getStatusCode();
if (httpRequest.execute().getStatusCode() >= 300) {
throw new ServiceException(
"HTTP connection to url: " + connectionSettings.getBaseUrl() + " failed: http response code: + " + statusCode);
OctaneHttpClient octaneHttpClient = new IdePluginsOctaneHttpClient(connectionSettings.getBaseUrl(), ClientType.OCTANE_IDE_PLUGIN);
OctaneHttpRequest request = new OctaneHttpRequest.GetOctaneHttpRequest(OctaneVersionService.getServerVersionUrl(connectionSettings));

OctaneHttpResponse response = octaneHttpClient.execute(request);

if (!response.isSuccessStatusCode()) {
throw new ServiceException("HTTP connection to url: " + connectionSettings.getBaseUrl() + " failed");
}
} catch (Exception e) {
throw new ServiceException("HTTP connection to url: " + connectionSettings.getBaseUrl() + " failed: " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
******************************************************************************/
package com.hpe.adm.octane.ideplugins.services.nonentity;

import com.google.api.client.http.*;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.util.IOUtils;
import com.google.common.io.ByteStreams;
import com.google.inject.Inject;
import com.hpe.adm.nga.sdk.network.OctaneHttpClient;
import com.hpe.adm.nga.sdk.network.OctaneHttpRequest;
import com.hpe.adm.nga.sdk.network.OctaneHttpResponse;
import com.hpe.adm.octane.ideplugins.services.connection.ConnectionSettingsProvider;
import com.hpe.adm.octane.ideplugins.services.connection.HttpClientProvider;
import com.hpe.adm.octane.ideplugins.services.connection.IdePluginsOctaneHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
Expand All @@ -43,7 +43,6 @@
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.HttpCookie;

public class ImageService {

Expand Down Expand Up @@ -72,11 +71,11 @@ private File saveImageToTempFile(String pictureLink) throws Exception {
imgFile = new File(octanePhotosDir, pictureName);
}

HttpResponse httpResponse = downloadImage(pictureLink, 2);
OctaneHttpResponse httpResponse = downloadImage(pictureLink, 2);

try (InputStream is = httpResponse.getContent();
try (InputStream is = httpResponse.getInputStream();
OutputStream os = new FileOutputStream(imgFile)) {
IOUtils.copy(is, os);
ByteStreams.copy(is, os);
} catch (IOException e) {
logger.error(e.getMessage());
}
Expand All @@ -88,30 +87,23 @@ private File saveImageToTempFile(String pictureLink) throws Exception {
* @param pictureLink - src link to the image from server
* @return
*/
private HttpResponse downloadImage(String pictureLink, int tryCount) throws Exception {

HttpResponse httpResponse;
HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
try {
HttpRequest httpRequest = requestFactory.buildGetRequest(new GenericUrl(pictureLink));
HttpCookie lwssoCookie = ((IdePluginsOctaneHttpClient) httpClientProvider.getOctaneHttpClient()).getSessionHttpCookie();
httpRequest.getHeaders().setCookie(lwssoCookie.toString());
httpResponse = httpRequest.execute();
} catch (IOException e) {
if(e instanceof HttpResponseException && ((HttpResponseException)e).getStatusCode() == 401 && tryCount > 0) {
//means that the cookie expired
logger.error("Cookie expired, retrying: " + e.getMessage());
((IdePluginsOctaneHttpClient) httpClientProvider.getOctaneHttpClient())
.setLastUsedAuthentication(connectionSettingsProvider.getConnectionSettings().getAuthentication());
httpClientProvider.getOctaneHttpClient().authenticate();
private OctaneHttpResponse downloadImage(String pictureLink, int tryCount) throws Exception {

OctaneHttpClient octaneHttpClient = httpClientProvider.getOctaneHttpClient();
OctaneHttpRequest request = new OctaneHttpRequest.GetOctaneHttpRequest(pictureLink);

OctaneHttpResponse response = octaneHttpClient.execute(request);

if (!response.isSuccessStatusCode()) {
if (tryCount > 0) {
return downloadImage(pictureLink, --tryCount);
} else {
logger.error(e.getMessage());
logger.error("Failed to load image from ValueEdge");
throw new Exception("Failed to load image from ValueEdge");
}
}
return httpResponse;

return response;
}

public String downloadPictures(String descriptionField) throws Exception{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
import com.hpe.adm.nga.sdk.network.OctaneHttpClient;
import com.hpe.adm.nga.sdk.network.OctaneHttpRequest;
import com.hpe.adm.nga.sdk.network.OctaneHttpResponse;
import com.hpe.adm.nga.sdk.network.google.GoogleHttpClient;
import com.hpe.adm.octane.ideplugins.services.connection.ConnectionSettings;
import com.hpe.adm.octane.ideplugins.services.connection.ConnectionSettingsProvider;
import com.hpe.adm.octane.ideplugins.services.connection.IdePluginsOctaneHttpClient;
import com.hpe.adm.octane.ideplugins.services.exception.ServiceRuntimeException;
import com.hpe.adm.octane.ideplugins.services.util.ClientType;
import com.hpe.adm.octane.ideplugins.services.util.OctaneVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -58,8 +59,9 @@ public class OctaneVersionService {

private static String getVersionString(ConnectionSettings connectionSettings) {
try {
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
OctaneHttpRequest request = new OctaneHttpRequest.GetOctaneHttpRequest(getServerVersionUrl(connectionSettings));
OctaneHttpClient octaneHttpClient = new GoogleHttpClient(connectionSettings.getBaseUrl(), connectionSettings.getAuthentication());
OctaneHttpClient octaneHttpClient = new IdePluginsOctaneHttpClient(connectionSettings.getBaseUrl(), ClientType.OCTANE_IDE_PLUGIN);
OctaneHttpResponse response = octaneHttpClient.execute(request);
String jsonString = response.getContent();
return new JsonParser().parse(jsonString).getAsJsonObject().get("display_version").getAsString();
Expand Down