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

JIT-48822: Dropbox | Add Verbose Logging to the Dropbox Connector #13

Open
wants to merge 3 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
21 changes: 21 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down Expand Up @@ -258,6 +264,21 @@
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@
import com.dropbox.core.v2.DbxClientV2;
import com.dropbox.core.v2.files.ListFolderResult;
import org.jitterbit.connector.sdk.Connection;
import org.jitterbit.connector.verbose.logging.dropbox.VerboseLogger;
import org.json.JSONObject;

import java.util.Objects;
import java.util.logging.Logger;

/**
* Connection to a Dropbox endpoint. Uses the
* <a href="https://dropbox.github.io/dropbox-sdk-java/api-docs/v2.1.x/"
* target="_blank">Official Dropbox Java SDK 2.1.2 API</a>.
*/
public class DropboxConnection implements Connection {
public class DropboxConnection implements Connection, DropboxConstants {

/**
* Constructs a Dropbox connection using a Dropbox app key and access token.
Expand All @@ -53,10 +55,19 @@ public void open() throws ConnectionException {
return;
}
try {
if (VerboseLogger.getLogger().isDebugEnabled()) {
JSONObject request = new JSONObject();
request.put(APP_KEY, appKey);
request.put(ACCESS_TOKEN, "********");
request.put(LOCALE, locale);
VerboseLogger.debug(DropboxConnection.class.getName(), "open", "Request: " + request.toString());
}
DbxRequestConfig dbxConfig = new DbxRequestConfig(appKey, locale);
client = new DbxClientV2(dbxConfig, accessToken);
ListFolderResult results = client.files().listFolder("");
logger.info("Dropbox Connection successful -> app-key: " + appKey + ", access-token: " + accessToken);
VerboseLogger.debug(DropboxConnection.class.getName(), "open", "Response: " + results.toString());
logger.info("Dropbox Connection successful ");

} catch (Exception x) {
x.printStackTrace();
throw new ConnectionException(Messages.DROPBOX_CODE07,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,15 @@ public interface DropboxConstants {
String FETCH_FILE_NAMESPACE = "http://org.jitterbit.connector/dropbox/fetchfile";
String PROCESS_FILE_NAMESPACE = "http://org.jitterbit.connector/dropbox/processfile";
String PUT_FILE_NAMESPACE = "http://org.jitterbit.connector/dropbox/putfile";

String QUERY_PARAMS = "queryParam";
String URL = "url";
String PATH_PARAMS = "pathParams";
String REQUEST_PAYLOAD = "requestPayload: ";
String RESPONSE_PAYLOAD = "responsePayload";
String STATUS_CODE = "statusCode";
String HEADERS = "headers";
String HTTP_METHOD = "method";
String TOKEN = "sl.A8hERaJ_rI-wKbDuOQSNTwwwIg8uY51HWsRRH1ax3O4vQxE5ICE28T7KvtkVRqRFkylAj"
+ "nK7sA0u19TGZfAszsyuLylTdoh4fRy_j30whafhjUyvIHm8OcM5lrgPlQVl1YwVbw1c";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
import com.dropbox.core.DbxDownloader;
import com.dropbox.core.v2.DbxClientV2;
import com.dropbox.core.v2.files.FileMetadata;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.jitterbit.connector.dropbox.DropboxConnection;
import org.jitterbit.connector.dropbox.DropboxConstants;
import org.jitterbit.connector.dropbox.DropboxUtils;
import org.jitterbit.connector.dropbox.Messages;
import org.jitterbit.connector.dropbox.schema.FetchFileRequest;
import org.jitterbit.connector.dropbox.schema.FetchFileResponse;
import org.jitterbit.connector.sdk.Discoverable;
import org.jitterbit.connector.sdk.JitterbitActivity;
Expand All @@ -28,6 +31,8 @@
import org.jitterbit.connector.sdk.metadata.ActivityFunctionParameters;
import org.jitterbit.connector.sdk.metadata.ActivityRequestResponseMetaData;
import org.jitterbit.connector.sdk.util.Utils;
import org.jitterbit.connector.verbose.logging.dropbox.VerboseLogger;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
Expand Down Expand Up @@ -98,7 +103,11 @@ public void execute(JitterbitActivity.ExecutionContext context) throws ActivityE
logger.info("Fetching: " + path);
connection = (DropboxConnection) context.getConnection();
DbxClientV2 client = connection.getClient();

FetchFileRequest req = DropboxUtils.
unmarshall(FetchFileRequest.class, context.getRequestPayload().getInputStream());
//Verbose Logger
VerboseLogger.debug(FetchFileActivity.class.getName(), "execute",
new ObjectMapper().writer().withRootName("request").writeValueAsString(req));
DbxDownloader<FileMetadata> result = client.files().download(path);
FetchFileResponse opRsp = new FetchFileResponse();

Expand All @@ -120,6 +129,9 @@ public void execute(JitterbitActivity.ExecutionContext context) throws ActivityE
ByteArrayOutputStream baos = new ByteArrayOutputStream();
result.download(baos);
opRsp.setContent(baos.toByteArray());
//Verbose Logger
VerboseLogger.debug(FetchFileActivity.class.getName(), "execute",
new ObjectMapper().writer().withRootName("response").writeValueAsString(opRsp));

// Marshall the response to the response payload output stream
DropboxUtils.marshall(FetchFileResponse.class, opRsp, context.getResponsePayload().getOutputStream());
Expand Down Expand Up @@ -161,10 +173,27 @@ public void execute(JitterbitActivity.ExecutionContext context) throws ActivityE
try {
DropboxUtils.setRequestResponseSchemas(activitySchemaResponse,
"xsds",
null,
FETCH_FILE_REQ_XSD,
FETCH_FILE_RSP_XSD);
activitySchemaResponse.setResponseRootElement(QName.valueOf("{" + FETCH_FILE_NAMESPACE + "}" +
FETCH_FILE_RSP_ROOT));
//Verbose Logging for Request and Response Schema
if (VerboseLogger.getLogger().isDebugEnabled()) {
JSONObject requestSchemaJson = new JSONObject();
requestSchemaJson.put("schemaName", activitySchemaResponse.getRequestSchema().getName());
requestSchemaJson.put("content", activitySchemaResponse.getRequestSchema().getContent());
requestSchemaJson.put("content-type", activitySchemaResponse.getRequestSchema().getSchemaContentType());
VerboseLogger.debug(FetchFileActivity.class.getName(),
"getActivityRequestResponseMetadata", "requestSchema: "
+ requestSchemaJson.toString());
JSONObject responseSchemaJson = new JSONObject();
responseSchemaJson.put("schemaName", activitySchemaResponse.getResponseSchema().getName());
responseSchemaJson.put("content", activitySchemaResponse.getResponseSchema().getContent());
responseSchemaJson.put("content-type", activitySchemaResponse.getResponseSchema().getSchemaContentType());
VerboseLogger.debug(FetchFileActivity.class.getName(),
"getActivityRequestResponseMetadata", "responseSchema: "
+ responseSchemaJson.toString());
}
return activitySchemaResponse;
} catch (Exception x) {
logger.log(Level.SEVERE, x.getLocalizedMessage(), x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.jitterbit.connector.sdk.metadata.DiscoverableObject;
import org.jitterbit.connector.sdk.metadata.DiscoverableObjectRequest;
import org.jitterbit.connector.sdk.metadata.SchemaMetaData;
import org.jitterbit.connector.verbose.logging.dropbox.VerboseLogger;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.util.Iterator;
Expand Down Expand Up @@ -143,6 +145,21 @@ public ActivityRequestResponseMetaData getActivityRequestResponseMetadata(
);
activitySchemaResponse.setResponseRootElement(new QName(""));
activitySchemaResponse.setResponseSchema(md);
//Verbose Logging for Request and Response Schema
if (VerboseLogger.getLogger().isDebugEnabled()) {
JSONObject requestSchemaJson = new JSONObject();
requestSchemaJson.put("schemaName", activitySchemaResponse.getRequestSchema().getName());
requestSchemaJson.put("content", activitySchemaResponse.getRequestSchema().getContent());
requestSchemaJson.put("content-type", activitySchemaResponse.getRequestSchema().getSchemaContentType());
VerboseLogger.debug(GetFileActivity.class.getName(), "getActivityRequestResponseMetadata", "requestSchema: "
+ requestSchemaJson.toString());
JSONObject responseSchemaJson = new JSONObject();
responseSchemaJson.put("schemaName", activitySchemaResponse.getResponseSchema().getName());
responseSchemaJson.put("content", activitySchemaResponse.getResponseSchema().getContent());
responseSchemaJson.put("content-type", activitySchemaResponse.getResponseSchema().getSchemaContentType());
VerboseLogger.debug(GetFileActivity.class.getName(), "getActivityRequestResponseMetadata", "responseSchema: "
+ responseSchemaJson.toString());
}
return activitySchemaResponse;
} catch (Exception x) {
logger.log(java.util.logging.Level.SEVERE, x.getLocalizedMessage(), x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
import org.jitterbit.connector.sdk.metadata.DiscoverableObjectRequest;
import org.jitterbit.connector.sdk.metadata.SchemaMetaData;
import org.jitterbit.connector.sdk.metadata.SchemaMetaData.SchemaContentType;

import org.jitterbit.connector.verbose.logging.dropbox.VerboseLogger;
import org.json.JSONObject;

import java.util.List;
import java.util.logging.Logger;
Expand Down Expand Up @@ -164,6 +165,23 @@ public void execute(JitterbitActivity.ExecutionContext context) throws ActivityE
fileName,
selectedObject.schemaContentType);
activitySchemaResponse.setResponseRootElement(QName.valueOf(rootName));
//Verbose Logging for Request and Response Schema
if (VerboseLogger.getLogger().isDebugEnabled()) {
JSONObject requestSchemaJson = new JSONObject();
requestSchemaJson.put("schemaName", activitySchemaResponse.getRequestSchema().getName());
requestSchemaJson.put("content", activitySchemaResponse.getRequestSchema().getContent());
requestSchemaJson.put("content-type", activitySchemaResponse.getRequestSchema().getSchemaContentType());
VerboseLogger.debug(ProcessFileActivity.class.getName(),
"getActivityRequestResponseMetadata", "requestSchema: "
+ requestSchemaJson.toString());
JSONObject responseSchemaJson = new JSONObject();
responseSchemaJson.put("schemaName", activitySchemaResponse.getResponseSchema().getName());
responseSchemaJson.put("content", activitySchemaResponse.getResponseSchema().getContent());
responseSchemaJson.put("content-type", activitySchemaResponse.getResponseSchema().getSchemaContentType());
VerboseLogger.debug(ProcessFileActivity.class.getName(),
"getActivityRequestResponseMetadata", "responseSchema: "
+ responseSchemaJson.toString());
}
return activitySchemaResponse;
} catch (Exception x) {
logger.log(java.util.logging.Level.SEVERE, x.getLocalizedMessage(), x);
Expand Down Expand Up @@ -199,6 +217,7 @@ protected ActivityRequestResponseMetaData multipleSchemas2() {
.setName("myschema4.xsd")
.setSchemaContentType(SchemaContentType.XSD)
.setContent(loadResource(DropboxUtils.class.getClassLoader(), "support-xsds/myschema4.xsd")));

} catch (Exception x) {
Logger.getLogger(ProcessFileActivity.class.getName()).severe(x.getLocalizedMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.dropbox.core.v2.files.FileMetadata;
import com.dropbox.core.v2.files.ListFolderResult;
import com.dropbox.core.v2.files.WriteMode;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.jitterbit.connector.dropbox.DropboxConnection;
import org.jitterbit.connector.dropbox.DropboxConstants;
import org.jitterbit.connector.dropbox.DropboxUtils;
Expand All @@ -29,6 +31,8 @@
import org.jitterbit.connector.sdk.exceptions.ActivityExecutionException;
import org.jitterbit.connector.sdk.metadata.ActivityFunctionParameters;
import org.jitterbit.connector.sdk.metadata.ActivityRequestResponseMetaData;
import org.jitterbit.connector.verbose.logging.dropbox.VerboseLogger;
import org.json.JSONObject;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -101,6 +105,9 @@ public void execute(ExecutionContext context) throws ActivityExecutionException
DbxClientV2 client = connection.getClient();

PutFileRequest req = DropboxUtils.unmarshall(PutFileRequest.class, context.getRequestPayload().getInputStream());
//Verbose Logger
VerboseLogger.debug(PutFileActivity.class.getName(), "execute",
new ObjectMapper().writer().withRootName("request").writeValueAsString(req));
dropboxPath = getPath(filename, folder, req);
logger.info("Uploading file: " + dropboxPath);
// Upload file
Expand All @@ -112,14 +119,16 @@ public void execute(ExecutionContext context) throws ActivityExecutionException
.withClientModified(new Date())
.start()
.uploadAndFinish(new ByteArrayInputStream(req.getContent()));

response.setName(metadata.getName());
response.setPathLower(metadata.getPathLower());
response.setContentHash(metadata.getContentHash());
response.setId(metadata.getId());
response.setRev(metadata.getRev());
response.setSize(BigInteger.valueOf(metadata.getSize()));

//Verbose Logger
VerboseLogger.debug(PutFileActivity.class.getName(), "execute",
new ObjectMapper().writer().withRootName("response").writeValueAsString(response));
// Marshall the response to the response payload output stream
DropboxUtils.marshall(PutFileResponse.class, response, context.getResponsePayload().getOutputStream());
} catch (Throwable x) {
Expand Down Expand Up @@ -164,6 +173,21 @@ public void execute(ExecutionContext context) throws ActivityExecutionException
activitySchemaResponse
.setRequestRootElement(new QName(PUT_FILE_NAMESPACE, PUT_FILE_REQ_ROOT))
.setResponseRootElement(new QName(PUT_FILE_NAMESPACE, PUT_FILE_RSP_ROOT));
//Verbose Logging for Request and Response Schema
if (VerboseLogger.getLogger().isDebugEnabled()) {
JSONObject requestSchemaJson = new JSONObject();
requestSchemaJson.put("schemaName", activitySchemaResponse.getRequestSchema().getName());
requestSchemaJson.put("content", activitySchemaResponse.getRequestSchema().getContent());
requestSchemaJson.put("content-type", activitySchemaResponse.getRequestSchema().getSchemaContentType());
VerboseLogger.debug(PutFileActivity.class.getName(), "getActivityRequestResponseMetadata", "requestSchema: "
+ requestSchemaJson.toString());
JSONObject responseSchemaJson = new JSONObject();
responseSchemaJson.put("schemaName", activitySchemaResponse.getResponseSchema().getName());
responseSchemaJson.put("content", activitySchemaResponse.getResponseSchema().getContent());
responseSchemaJson.put("content-type", activitySchemaResponse.getResponseSchema().getSchemaContentType());
VerboseLogger.debug(PutFileActivity.class.getName(), "getActivityRequestResponseMetadata", "responseSchema: "
+ responseSchemaJson.toString());
}
return activitySchemaResponse;
} catch (Exception x) {
logger.log(Level.SEVERE, x.getLocalizedMessage(), x);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.jitterbit.connector.verbose.logging.dropbox;

import com.google.common.net.HttpHeaders;

import org.apache.http.client.methods.HttpRequestBase;
import org.apache.log4j.Logger;
import org.jitterbit.connector.dropbox.DropboxConstants;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
* @author jitendra.singh
*/
public class VerboseLogger implements DropboxConstants {

private static final Logger logger = Logger.getLogger(VerboseLogger.class);

public static Logger getLogger() {
return logger;
}

public static void info(String className, String methodName, String message) {
logger.info(className + "." + methodName + ", msg: " + message);
}

public static void debug(String className, String methodName, String message) {
if (logger.isDebugEnabled()) {
logger.debug(className + "." + methodName + ", msg: " + message);
}
}

public static void debug(String className, String methodName, HttpRequestBase httpRequest)
throws MalformedURLException {
if (logger.isDebugEnabled()) {
JSONObject request = new JSONObject();
try {
request.put(URL, httpRequest.getURI().toURL());
request.put(QUERY_PARAMS, httpRequest.getURI().getQuery());
} catch (JSONException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
request.put(HTTP_METHOD, httpRequest.getMethod());
request.put(HEADERS, Arrays.stream(httpRequest.getAllHeaders()).map(head -> {
if (head.getName().equalsIgnoreCase(HttpHeaders.AUTHORIZATION)) {
return new String(head.getName() + ": ********");
}
return new String(head.getName() + ": " + head.getValue());
}).collect(Collectors.toList()));
logger.debug(className + "." + methodName + ", msg: Request Send: " + request);
}
}

public static void error(String className, String methodName, String message) {
logger.error(className + "." + methodName + ", msg: " + message);
}

public static void error(String className, String methodName, String errorMessage, Throwable error) {
logger.error(className + "." + methodName + ", msg: " + errorMessage, error);
}
}
1 change: 0 additions & 1 deletion src/main/resources/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ Class-Path: lib/dropbox-core-sdk-3.0.6.jar
lib/commons-io-2.6.jar
lib/guava-19.0.jar
lib/log4j-1.2.17.jar

Loading