Skip to content

Commit

Permalink
Dropbox component support specifying route params using headers
Browse files Browse the repository at this point in the history
  • Loading branch information
tlehoux authored and davsclaus committed May 28, 2017
1 parent d896f56 commit ede8927
Show file tree
Hide file tree
Showing 18 changed files with 483 additions and 48 deletions.
Expand Up @@ -65,8 +65,9 @@ protected Endpoint createEndpoint(String uri, String remaining, Map<String, Obje
configuration.setUploadMode(DropboxUploadMode.valueOf((String)parameters.get("uploadMode"))); configuration.setUploadMode(DropboxUploadMode.valueOf((String)parameters.get("uploadMode")));
} }



//pass validation test //pass validation test
DropboxConfigurationValidator.validate(configuration); DropboxConfigurationValidator.validateCommonProperties(configuration);


// and then override from parameters // and then override from parameters
setProperties(configuration, parameters); setProperties(configuration, parameters);
Expand Down
Expand Up @@ -21,7 +21,11 @@
import org.apache.camel.component.dropbox.DropboxEndpoint; import org.apache.camel.component.dropbox.DropboxEndpoint;
import org.apache.camel.component.dropbox.core.DropboxAPIFacade; import org.apache.camel.component.dropbox.core.DropboxAPIFacade;
import org.apache.camel.component.dropbox.dto.DropboxDelResult; import org.apache.camel.component.dropbox.dto.DropboxDelResult;
import org.apache.camel.component.dropbox.util.DropboxHelper;
import org.apache.camel.component.dropbox.util.DropboxRequestHeader;
import org.apache.camel.component.dropbox.util.DropboxResultHeader; import org.apache.camel.component.dropbox.util.DropboxResultHeader;
import org.apache.camel.component.dropbox.validator.DropboxConfigurationValidator;
import org.apache.camel.util.ObjectHelper;


public class DropboxDelProducer extends DropboxProducer { public class DropboxDelProducer extends DropboxProducer {


Expand All @@ -31,11 +35,16 @@ public DropboxDelProducer(DropboxEndpoint endpoint, DropboxConfiguration configu


@Override @Override
public void process(Exchange exchange) throws Exception { public void process(Exchange exchange) throws Exception {

String remotePath = DropboxHelper.getRemotePath(configuration, exchange);
DropboxConfigurationValidator.validateDelOp(remotePath);

DropboxDelResult result = new DropboxAPIFacade(configuration.getClient(), exchange) DropboxDelResult result = new DropboxAPIFacade(configuration.getClient(), exchange)
.del(configuration.getRemotePath()); .del(remotePath);

exchange.getIn().setHeader(DropboxResultHeader.DELETED_PATH.name(), result.getEntry()); exchange.getIn().setHeader(DropboxResultHeader.DELETED_PATH.name(), result.getEntry());
exchange.getIn().setBody(result.getEntry()); exchange.getIn().setBody(result.getEntry());
log.debug("Deleted: {}", configuration.getRemotePath()); log.debug("Deleted: {}", remotePath);
} }


} }
Expand Up @@ -23,7 +23,11 @@
import org.apache.camel.component.dropbox.DropboxEndpoint; import org.apache.camel.component.dropbox.DropboxEndpoint;
import org.apache.camel.component.dropbox.core.DropboxAPIFacade; import org.apache.camel.component.dropbox.core.DropboxAPIFacade;
import org.apache.camel.component.dropbox.dto.DropboxFileDownloadResult; import org.apache.camel.component.dropbox.dto.DropboxFileDownloadResult;
import org.apache.camel.component.dropbox.util.DropboxHelper;
import org.apache.camel.component.dropbox.util.DropboxRequestHeader;
import org.apache.camel.component.dropbox.util.DropboxResultHeader; import org.apache.camel.component.dropbox.util.DropboxResultHeader;
import org.apache.camel.component.dropbox.validator.DropboxConfigurationValidator;
import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;


Expand All @@ -36,8 +40,11 @@ public DropboxGetProducer(DropboxEndpoint endpoint, DropboxConfiguration configu


@Override @Override
public void process(Exchange exchange) throws Exception { public void process(Exchange exchange) throws Exception {
String remotePath = DropboxHelper.getRemotePath(configuration, exchange);
DropboxConfigurationValidator.validateGetOp(remotePath);

DropboxFileDownloadResult result = new DropboxAPIFacade(configuration.getClient(), exchange) DropboxFileDownloadResult result = new DropboxAPIFacade(configuration.getClient(), exchange)
.get(configuration.getRemotePath()); .get(remotePath);


Map<String, Object> map = result.getEntries(); Map<String, Object> map = result.getEntries();
if (map.size() == 1) { if (map.size() == 1) {
Expand Down
Expand Up @@ -21,7 +21,11 @@
import org.apache.camel.component.dropbox.DropboxEndpoint; import org.apache.camel.component.dropbox.DropboxEndpoint;
import org.apache.camel.component.dropbox.core.DropboxAPIFacade; import org.apache.camel.component.dropbox.core.DropboxAPIFacade;
import org.apache.camel.component.dropbox.dto.DropboxMoveResult; import org.apache.camel.component.dropbox.dto.DropboxMoveResult;
import org.apache.camel.component.dropbox.util.DropboxHelper;
import org.apache.camel.component.dropbox.util.DropboxRequestHeader;
import org.apache.camel.component.dropbox.util.DropboxResultHeader; import org.apache.camel.component.dropbox.util.DropboxResultHeader;
import org.apache.camel.component.dropbox.validator.DropboxConfigurationValidator;
import org.apache.camel.util.ObjectHelper;


public class DropboxMoveProducer extends DropboxProducer { public class DropboxMoveProducer extends DropboxProducer {


Expand All @@ -31,13 +35,18 @@ public DropboxMoveProducer(DropboxEndpoint endpoint, DropboxConfiguration config


@Override @Override
public void process(Exchange exchange) throws Exception { public void process(Exchange exchange) throws Exception {
String remotePath = DropboxHelper.getRemotePath(configuration, exchange);
String newRemotePath = DropboxHelper.getNewRemotePath(configuration, exchange);

DropboxConfigurationValidator.validateMoveOp(remotePath, newRemotePath);

DropboxMoveResult result = new DropboxAPIFacade(configuration.getClient(), exchange) DropboxMoveResult result = new DropboxAPIFacade(configuration.getClient(), exchange)
.move(configuration.getRemotePath(), configuration.getNewRemotePath()); .move(remotePath, newRemotePath);


exchange.getIn().setHeader(DropboxResultHeader.MOVED_PATH.name(), result.getOldPath()); exchange.getIn().setHeader(DropboxResultHeader.MOVED_PATH.name(), result.getOldPath());
exchange.getIn().setBody(result.getNewPath()); exchange.getIn().setBody(result.getNewPath());


log.debug("Moved from {} to {}", configuration.getRemotePath(), configuration.getNewRemotePath()); log.debug("Moved from {} to {}", remotePath, newRemotePath);
} }


} }
Expand Up @@ -23,8 +23,13 @@
import org.apache.camel.component.dropbox.DropboxEndpoint; import org.apache.camel.component.dropbox.DropboxEndpoint;
import org.apache.camel.component.dropbox.core.DropboxAPIFacade; import org.apache.camel.component.dropbox.core.DropboxAPIFacade;
import org.apache.camel.component.dropbox.dto.DropboxFileUploadResult; import org.apache.camel.component.dropbox.dto.DropboxFileUploadResult;
import org.apache.camel.component.dropbox.util.DropboxHelper;
import org.apache.camel.component.dropbox.util.DropboxRequestHeader;
import org.apache.camel.component.dropbox.util.DropboxResultCode; import org.apache.camel.component.dropbox.util.DropboxResultCode;
import org.apache.camel.component.dropbox.util.DropboxResultHeader; import org.apache.camel.component.dropbox.util.DropboxResultHeader;
import org.apache.camel.component.dropbox.util.DropboxUploadMode;
import org.apache.camel.component.dropbox.validator.DropboxConfigurationValidator;
import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;


Expand All @@ -37,9 +42,14 @@ public DropboxPutProducer(DropboxEndpoint endpoint, DropboxConfiguration configu


@Override @Override
public void process(Exchange exchange) throws Exception { public void process(Exchange exchange) throws Exception {
DropboxFileUploadResult result = new DropboxAPIFacade(configuration.getClient(), exchange) String remotePath = DropboxHelper.getRemotePath(configuration, exchange);
.put(configuration.getLocalPath(), configuration.getRemotePath(), configuration.getUploadMode()); String localPath = DropboxHelper.getLocalPath(configuration, exchange);
DropboxUploadMode uploadMode = DropboxHelper.getUploadMode(configuration, exchange);

DropboxConfigurationValidator.validatePutOp(localPath, remotePath, uploadMode);


DropboxFileUploadResult result = new DropboxAPIFacade(configuration.getClient(), exchange)
.put(localPath, remotePath, uploadMode);


Map<String, DropboxResultCode> map = result.getResults(); Map<String, DropboxResultCode> map = result.getResults();
if (map.size() == 1) { if (map.size() == 1) {
Expand Down
Expand Up @@ -22,7 +22,11 @@
import org.apache.camel.component.dropbox.DropboxEndpoint; import org.apache.camel.component.dropbox.DropboxEndpoint;
import org.apache.camel.component.dropbox.core.DropboxAPIFacade; import org.apache.camel.component.dropbox.core.DropboxAPIFacade;
import org.apache.camel.component.dropbox.dto.DropboxSearchResult; import org.apache.camel.component.dropbox.dto.DropboxSearchResult;
import org.apache.camel.component.dropbox.util.DropboxHelper;
import org.apache.camel.component.dropbox.util.DropboxRequestHeader;
import org.apache.camel.component.dropbox.util.DropboxResultHeader; import org.apache.camel.component.dropbox.util.DropboxResultHeader;
import org.apache.camel.component.dropbox.validator.DropboxConfigurationValidator;
import org.apache.camel.util.ObjectHelper;


public class DropboxSearchProducer extends DropboxProducer { public class DropboxSearchProducer extends DropboxProducer {


Expand All @@ -32,8 +36,13 @@ public DropboxSearchProducer(DropboxEndpoint endpoint, DropboxConfiguration conf


@Override @Override
public void process(Exchange exchange) throws Exception { public void process(Exchange exchange) throws Exception {
String remotePath = DropboxHelper.getRemotePath(configuration, exchange);
String query = DropboxHelper.getQuery(configuration, exchange);

DropboxConfigurationValidator.validateSearchOp(remotePath);

DropboxSearchResult result = new DropboxAPIFacade(configuration.getClient(), exchange) DropboxSearchResult result = new DropboxAPIFacade(configuration.getClient(), exchange)
.search(configuration.getRemotePath(), configuration.getQuery()); .search(remotePath, query);


StringBuilder fileExtracted = new StringBuilder(); StringBuilder fileExtracted = new StringBuilder();
for (DbxEntry entry : result.getFound()) { for (DbxEntry entry : result.getFound()) {
Expand Down
@@ -0,0 +1,66 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.dropbox.util;

import org.apache.camel.Exchange;
import org.apache.camel.component.dropbox.DropboxConfiguration;
import org.apache.camel.util.ObjectHelper;

public final class DropboxHelper {

private DropboxHelper() { }

public static String getRemotePath(DropboxConfiguration configuration, Exchange exchange) {
return ObjectHelper.isNotEmpty(
exchange.getIn().getHeader(DropboxRequestHeader.REMOTE_PATH.name()))
? exchange.getIn().getHeader(DropboxRequestHeader.REMOTE_PATH.name(), String.class).replaceAll("\\s", "+")
: configuration.getRemotePath();
}

public static String getNewRemotePath(DropboxConfiguration configuration, Exchange exchange) {
return ObjectHelper.isNotEmpty(
exchange.getIn().getHeader(DropboxRequestHeader.NEW_REMOTE_PATH.name()))
? exchange.getIn().getHeader(DropboxRequestHeader.NEW_REMOTE_PATH.name(), String.class)
: configuration.getNewRemotePath();
}

public static String getLocalPath(DropboxConfiguration configuration, Exchange exchange) {
return ObjectHelper.isNotEmpty(
exchange.getIn().getHeader(DropboxRequestHeader.LOCAL_PATH.name()))
? exchange.getIn().getHeader(DropboxRequestHeader.LOCAL_PATH.name(), String.class)
: configuration.getLocalPath();
}

public static String getQuery(DropboxConfiguration configuration, Exchange exchange) {
return ObjectHelper.isNotEmpty(
exchange.getIn().getHeader(DropboxRequestHeader.QUERY.name()))
? exchange.getIn().getHeader(DropboxRequestHeader.QUERY.name(), String.class)
: configuration.getQuery();
}

public static DropboxUploadMode getUploadMode(DropboxConfiguration configuration, Exchange exchange) {
return ObjectHelper.isNotEmpty(
exchange.getIn().getHeader(DropboxRequestHeader.UPLOAD_MODE.name()))
? DropboxUploadMode.valueOf(exchange.getIn().getHeader(DropboxRequestHeader.UPLOAD_MODE.name(), String.class))
: configuration.getUploadMode();
}





}
@@ -0,0 +1,35 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.dropbox.util;

public enum DropboxRequestHeader {
REMOTE_PATH("remotePath"),
NEW_REMOTE_PATH("newRemotePath"),
LOCAL_PATH("localPath"),
UPLOAD_MODE("uploadMode"),
QUERY("query");

private String name;

DropboxRequestHeader(String name) {
this.name = name;
}

public String toString() {
return name;
}
}
Expand Up @@ -23,6 +23,7 @@
import org.apache.camel.component.dropbox.DropboxConfiguration; import org.apache.camel.component.dropbox.DropboxConfiguration;
import org.apache.camel.component.dropbox.util.DropboxException; import org.apache.camel.component.dropbox.util.DropboxException;
import org.apache.camel.component.dropbox.util.DropboxOperation; import org.apache.camel.component.dropbox.util.DropboxOperation;
import org.apache.camel.component.dropbox.util.DropboxUploadMode;


import static org.apache.camel.component.dropbox.util.DropboxConstants.DROPBOX_FILE_SEPARATOR; import static org.apache.camel.component.dropbox.util.DropboxConstants.DROPBOX_FILE_SEPARATOR;


Expand All @@ -32,28 +33,8 @@ public final class DropboxConfigurationValidator {


private DropboxConfigurationValidator() { } private DropboxConfigurationValidator() { }


/**
* Validate the parameters passed in the incoming url.
* @param configuration object containing the parameters.
* @throws DropboxException
*/
public static void validate(DropboxConfiguration configuration) throws DropboxException {
validateCommonProperties(configuration);
DropboxOperation op = configuration.getOperation();
if (op == DropboxOperation.get) {
validateGetOp(configuration);
} else if (op == DropboxOperation.put) {
validatePutOp(configuration);
} else if (op == DropboxOperation.search) {
validateSearchOp(configuration);
} else if (op == DropboxOperation.del) {
validateDelOp(configuration);
} else if (op == DropboxOperation.move) {
validateMoveOp(configuration);
}
}


private static void validateCommonProperties(DropboxConfiguration configuration) throws DropboxException { public static void validateCommonProperties(DropboxConfiguration configuration) throws DropboxException {
if (configuration.getAccessToken() == null || configuration.getAccessToken().equals("")) { if (configuration.getAccessToken() == null || configuration.getAccessToken().equals("")) {
throw new DropboxException("option <accessToken> is not present or not valid!"); throw new DropboxException("option <accessToken> is not present or not valid!");
} }
Expand All @@ -62,34 +43,34 @@ private static void validateCommonProperties(DropboxConfiguration configuration)
} }
} }


private static void validateGetOp(DropboxConfiguration configuration) throws DropboxException { public static void validateGetOp(String remotePath) throws DropboxException {
validateRemotePath(configuration.getRemotePath()); validateRemotePath(remotePath);
} }


private static void validatePutOp(DropboxConfiguration configuration) throws DropboxException { public static void validatePutOp(String localPath, String remotePath, DropboxUploadMode uploadMode) throws DropboxException {
validateLocalPath(configuration.getLocalPath()); validateLocalPath(localPath);
//remote path is optional //remote path is optional
if (configuration.getRemotePath() != null) { if (remotePath != null) {
validateRemotePathForPut(configuration.getRemotePath()); validateRemotePathForPut(remotePath);
} else { //in case remote path is not set, local path is even the remote path so it must be validated as UNIX } else { //in case remote path is not set, local path is even the remote path so it must be validated as UNIX
validatePathInUnix(configuration.getLocalPath()); validatePathInUnix(localPath);
} }
if (configuration.getUploadMode() == null) { if (uploadMode == null) {
throw new DropboxException("option <uploadMode> is not present or not valid!"); throw new DropboxException("option <uploadMode> is not present or not valid!");
} }
} }


private static void validateSearchOp(DropboxConfiguration configuration) throws DropboxException { public static void validateSearchOp(String remotePath) throws DropboxException {
validateRemotePath(configuration.getRemotePath()); validateRemotePath(remotePath);
} }


private static void validateDelOp(DropboxConfiguration configuration) throws DropboxException { public static void validateDelOp(String remotePath) throws DropboxException {
validateRemotePath(configuration.getRemotePath()); validateRemotePath(remotePath);
} }


private static void validateMoveOp(DropboxConfiguration configuration) throws DropboxException { public static void validateMoveOp(String remotePath, String newRemotePath) throws DropboxException {
validateRemotePath(configuration.getRemotePath()); validateRemotePath(remotePath);
validateRemotePath(configuration.getNewRemotePath()); validateRemotePath(newRemotePath);
} }


private static void validateLocalPath(String localPath) throws DropboxException { private static void validateLocalPath(String localPath) throws DropboxException {
Expand All @@ -103,6 +84,7 @@ private static void validateLocalPath(String localPath) throws DropboxException
} }


private static void validateRemotePath(String remotePath) throws DropboxException { private static void validateRemotePath(String remotePath) throws DropboxException {

if (remotePath == null || !remotePath.startsWith(DROPBOX_FILE_SEPARATOR)) { if (remotePath == null || !remotePath.startsWith(DROPBOX_FILE_SEPARATOR)) {
throw new DropboxException("option <remotePath> is not valid!"); throw new DropboxException("option <remotePath> is not valid!");
} }
Expand Down

0 comments on commit ede8927

Please sign in to comment.