Skip to content

Commit

Permalink
Change to Java Core for Http Client requests
Browse files Browse the repository at this point in the history
  • Loading branch information
cristinalola committed Jul 23, 2020
1 parent 79cb110 commit 5760e8b
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 262 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ dependencies {
implementation "com.lucidworks-connector.sdk:connector-plugin-sdk:${connectorsSDKVersion}"
implementation "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
implementation "com.github.jasminb:jsonapi-converter:${jsonApiVersion}"
implementation "com.squareup.okhttp3:okhttp:${okHttpVersion}"
compileOnly "org.projectlombok:lombok:${lombokVersion}"
annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
testCompile group: 'junit', name: 'junit', version: '4.12'
Expand Down
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ clientJarAbsolutePath=/path/to/fusion/installation
lombokVersion=1.18.12
jacksonVersion=2.9.10
jsonApiVersion=0.10
okHttpVersion=4.7.2
mockitoVersion=2.7.22
# plugins
pluginClass=com.lucidworks.fusion.connector.ConnectorPlugin
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/com/lucidworks/fusion/connector/Runner.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
package com.lucidworks.fusion.connector;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lucidworks.fusion.connector.model.DrupalLoginRequest;
import com.lucidworks.fusion.connector.model.DrupalLoginResponse;
import com.lucidworks.fusion.connector.model.TopLevelJsonapi;
import com.lucidworks.fusion.connector.service.ConnectorService;
import com.lucidworks.fusion.connector.service.ContentService;
import com.lucidworks.fusion.connector.service.DrupalOkHttp;
import com.lucidworks.fusion.connector.service.DrupalHttpClient;
import com.lucidworks.fusion.connector.util.DataUtil;

import java.util.Map;

public class Runner {

public static void main(String[] args) {
String baseUrl = "http://s5ee7c4bb7c413wcrxueduzw.devcloud.acquia-sites.com/";
String baseUrl = "http://s5efe1a8b62a65rx9apyfzmk.devcloud.acquia-sites.com";
ObjectMapper mapper = new ObjectMapper();

DrupalOkHttp drupalOkHttp = new DrupalOkHttp(mapper);
ContentService contentService = new ContentService(mapper);
DrupalHttpClient drupalHttpClient = new DrupalHttpClient();

DrupalLoginRequest drupalLoginRequest = new DrupalLoginRequest("authenticated", "authenticated");
String loginResponse = drupalHttpClient.doLogin("http://s5efe1a8b62a65rx9apyfzmk.devcloud.acquia-sites.com/user/login", "authenticated", "authenticated");

DrupalLoginResponse drupalLoginResponse = drupalOkHttp.loginResponse(normalizeUrl(baseUrl) + normalizeUrl("/user/login"), drupalLoginRequest);
ContentService contentService = new ContentService(mapper);

ConnectorService connectorService = new ConnectorService(normalizeUrl(baseUrl) + normalizeUrl("/en/fusion/node/article"), new DrupalLoginResponse(), contentService, mapper);
ConnectorService connectorService = new ConnectorService(normalizeUrl(baseUrl) + normalizeUrl("/en/fusion"), contentService, drupalHttpClient);

Map<String, String> response = connectorService.prepareDataToUpload();

Expand All @@ -34,13 +31,11 @@ public static void main(String[] args) {

for (String key : objectMap.keySet()) {
Map<String, Object> pageContentMap = objectMap.get(key);
System.out.println(pageContentMap.values().toString());
System.out.println("Key: " + key);
}

System.out.println("Logout is successful: " + drupalOkHttp.logout(normalizeUrl(baseUrl) + "/user/logout", drupalLoginResponse));
}


private static String normalizeUrl(String initialUrl) {
String normalizedUrl = initialUrl.endsWith("/") ?
initialUrl.substring(0, initialUrl.length() - 1) : initialUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ interface Properties extends ConnectorPluginProperties {
description = "Password to login into drupal to be able to fetch content from it.",
order = 3
)
@StringSchema()
@StringSchema(encrypted = true)
String getPassword();

@Property(
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lucidworks.fusion.connector.config.ContentConfig;
import com.lucidworks.fusion.connector.exception.ServiceException;
import com.lucidworks.fusion.connector.model.DrupalLoginRequest;
import com.lucidworks.fusion.connector.model.DrupalLoginResponse;
import com.lucidworks.fusion.connector.model.TopLevelJsonapi;
import com.lucidworks.fusion.connector.plugin.api.fetcher.result.FetchResult;
import com.lucidworks.fusion.connector.plugin.api.fetcher.type.content.ContentFetcher;
import com.lucidworks.fusion.connector.service.ConnectorService;
import com.lucidworks.fusion.connector.service.ContentService;
import com.lucidworks.fusion.connector.service.DrupalOkHttp;
import com.lucidworks.fusion.connector.service.DrupalHttpClient;
import com.lucidworks.fusion.connector.util.DataUtil;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -29,19 +27,17 @@ public class JsonContentFetcher implements ContentFetcher {
private ContentService contentService;
private ConnectorService connectorService;
private ObjectMapper objectMapper;
private DrupalOkHttp drupalOkHttp;
private DrupalLoginResponse drupalLoginResponse;
private DrupalHttpClient drupalHttpClient;

@Inject
public JsonContentFetcher(
ContentConfig connectorConfig
) {
this.connectorConfig = connectorConfig;
this.objectMapper = new ObjectMapper();
this.drupalOkHttp = new DrupalOkHttp(objectMapper);
this.drupalHttpClient = new DrupalHttpClient();
this.contentService = new ContentService(objectMapper);
this.drupalLoginResponse = getDrupalLoginResponse();
this.connectorService = new ConnectorService(getDrupalContentEntryUrl(), this.drupalLoginResponse, contentService, objectMapper);
this.connectorService = new ConnectorService(getDrupalContentEntryUrl(), contentService, drupalHttpClient);
}

@Override
Expand All @@ -50,9 +46,10 @@ public FetchResult fetch(FetchContext fetchContext) {
Map<String, TopLevelJsonapi> topLevelJsonapiMap = new HashMap<>();
Map<String, String> contentMap = new HashMap<>();

doLogin();

try {
contentMap = connectorService.prepareDataToUpload();

topLevelJsonapiMap = contentService.getTopLevelJsonapiDataMap();

} catch (ServiceException e) {
Expand Down Expand Up @@ -85,25 +82,13 @@ public FetchResult fetch(FetchContext fetchContext) {
.emit();
}

logout();

return fetchContext.newResult();
}

private DrupalLoginResponse getDrupalLoginResponse() {
String username = connectorConfig.properties().getUsername();
String password = connectorConfig.properties().getPassword();


if (username != null && !username.isEmpty() &&
password != null && !password.isEmpty()) {
DrupalLoginRequest drupalLoginRequest = new DrupalLoginRequest(username, password);

drupalLoginResponse = drupalOkHttp.loginResponse(getDrupalLoginUrl(), drupalLoginRequest);

return drupalLoginResponse;
} else {
return new DrupalLoginResponse();
private void doLogin() {
String username = connectorConfig.properties().getUsername(), password = connectorConfig.properties().getPassword();
if (username != null && password != null) {
drupalHttpClient.doLogin(getDrupalLoginUrl(), username, password);
}
}

Expand All @@ -114,10 +99,6 @@ private String normalizeUrl(String initialUrl) {
return normalizedUrl;
}

private boolean logout() {
return drupalOkHttp.logout(getDrupalLogoutUrl(), drupalLoginResponse);
}

private String getDrupalUrl() {
return connectorConfig.properties().getUrl();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lucidworks.fusion.connector.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
Expand All @@ -14,6 +15,8 @@ public class TopLevelJsonapi implements Serializable {
private Jsonapi jsonapi;
private Data[] data;
private Map<String, LinkHref> Links;

@JsonIgnore
private Meta meta;
private Errors errors;
private Included included;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.lucidworks.fusion.connector.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lucidworks.fusion.connector.exception.ServiceException;
import com.lucidworks.fusion.connector.model.DrupalLoginResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
Expand All @@ -16,8 +14,8 @@ public class ConnectorService {
private final DrupalContentCrawler drupalContentCrawler;
private boolean isProcessStarted = false;

public ConnectorService(String drupalUrl, DrupalLoginResponse drupalLoginResponse, ContentService contentService, ObjectMapper mapper) {
this.drupalContentCrawler = new DrupalContentCrawler(drupalUrl, drupalLoginResponse, contentService, mapper);
public ConnectorService(String drupalUrl, ContentService contentService, DrupalHttpClient drupalHttpClient) {
this.drupalContentCrawler = new DrupalContentCrawler(drupalUrl, contentService, drupalHttpClient);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.lucidworks.fusion.connector.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lucidworks.fusion.connector.exception.ServiceException;
import com.lucidworks.fusion.connector.model.DrupalLoginResponse;
import com.lucidworks.fusion.connector.model.TopLevelJsonapi;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -21,28 +19,23 @@ public class DrupalContentCrawler {
private boolean processFinished = false;
private List<String> drupalUrls;
private Map<String, String> visitedUrls;
private DrupalLoginResponse loggedInUser;
private DrupalOkHttp drupalOkHttp;
private DrupalHttpClient drupalHttpClient;
private ContentService contentService;
private Map<String, TopLevelJsonapi> topLevelJsonapiMap;

/**
* Constructor for Crawler
*
* @param drupalUrl the url for the first GET request
* @param loggedInUser the loggedInUser with JWT token inside
* @param contentService the content service class
* @param drupalUrl the url for the first GET request
* @param drupalHttpClient the Http Client object
* @param contentService the content service class
*/
public DrupalContentCrawler(String drupalUrl, DrupalLoginResponse loggedInUser, ContentService contentService,
ObjectMapper mapper) {
this.drupalOkHttp = new DrupalOkHttp(mapper);
this.loggedInUser = loggedInUser;

public DrupalContentCrawler(String drupalUrl, ContentService contentService, DrupalHttpClient drupalHttpClient) {
this.drupalUrls = new ArrayList<>(Arrays.asList(drupalUrl));
this.visitedUrls = new HashMap<>();
this.topLevelJsonapiMap = new HashMap<>();

this.contentService = contentService;
this.drupalHttpClient = drupalHttpClient;
}

/**
Expand All @@ -58,7 +51,7 @@ public void startCrawling() {
try {
do {
drupalUrls.stream().forEach(url -> {
String responseBody = drupalOkHttp.getDrupalContent(url, loggedInUser);
String responseBody = drupalHttpClient.getContent(url);
if (responseBody != null) {
currentStepContent.put(url, responseBody);
urlsVisitedInCurrentStep.add(url);
Expand Down
Loading

0 comments on commit 5760e8b

Please sign in to comment.