Skip to content

Commit

Permalink
NXP-17285: make CommandlineService accept any parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
efge authored and nuxeojenkins committed Oct 2, 2015
1 parent a3be42e commit e35f431
Show file tree
Hide file tree
Showing 19 changed files with 295 additions and 350 deletions.
Expand Up @@ -32,14 +32,12 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.common.utils.FileUtils;
import org.nuxeo.ecm.core.api.Blob;
import org.nuxeo.ecm.core.api.Blobs;
import org.nuxeo.ecm.core.api.PropertyException;
import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
import org.nuxeo.ecm.core.api.model.Property;
import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService;
import org.nuxeo.ecm.platform.picture.api.ImageInfo;
import org.nuxeo.ecm.platform.picture.api.ImagingConvertConstants;
import org.nuxeo.ecm.platform.picture.api.ImagingService;
Expand Down Expand Up @@ -72,24 +70,14 @@ public boolean fillPictureViews(Blob blob, String filename, String title,
}

File file = blob.getFile();
CommandLineExecutorService commandLineExecutorService = Framework.getLocalService(CommandLineExecutorService.class);
boolean validFilename = file != null && commandLineExecutorService.isValidParameter(file.getName());
if (file == null || !validFilename) {
String extension;
if (file != null) {
extension = "." + FileUtils.getFileExtension(file.getName());
} else {
extension = ".jpg";
}
file = File.createTempFile("nuxeoImage", extension);
if (file == null) {
file = File.createTempFile("nuxeoImage", ".jpg");
Framework.trackFile(file, this);
blob.transferTo(file);
// use a persistent blob with our file
if (!validFilename) {
String digest = blob.getDigest();
blob = Blobs.createBlob(file, blob.getMimeType(), blob.getEncoding(), blob.getFilename());
blob.setDigest(digest);
}
String digest = blob.getDigest();
blob = Blobs.createBlob(file, blob.getMimeType(), blob.getEncoding(), blob.getFilename());
blob.setDigest(digest);
}

fileContent = blob;
Expand Down
@@ -1,44 +1,39 @@
/*
* (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
* (C) Copyright 2006-2015 Nuxeo SAS (http://nuxeo.com/) and contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public License
* (LGPL) version 2.1 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
* http://www.gnu.org/licenses/lgpl-2.1.html
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* Contributors:
* Nuxeo - initial API and implementation
*
* $Id$
*
* Thierry Delprat
* Vincent Dutat
* Vladimir Pasquier
* Julien Carsique
* Florent Guillaume
*/

package org.nuxeo.ecm.platform.commandline.executor.api;

import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Wraps command parameters (String or File). Quoted by default. Use {@link #addNamedParameter(String, String, boolean)}
* to avoid quotes.
*
* @author tiry
* @author Vincent Dutat
* Wraps command parameters (String or File path, or a list of values that will be expanded as separate parameters).
*/
public class CmdParameters implements Serializable {

private static final long serialVersionUID = 1L;

protected final Map<String, String> params = new HashMap<>();

private final HashMap<String, CmdParameter> cmdParameters = new HashMap<>();
private final Map<String, ParameterValue> params = new HashMap<>();

/**
* It is recommended to use the CmdParameters instance returned by
Expand All @@ -50,63 +45,75 @@ public CmdParameters() {
}

public void addNamedParameter(String name, String value) {
params.put(name, value);
// Quote by default
CmdParameter cmdParameter = new CmdParameter(value, true);
cmdParameters.put(name, cmdParameter);
params.put(name, new ParameterValue(value));
}

public void addNamedParameter(String name, File file) {
addNamedParameter(name, file.getAbsolutePath());
}

public Map<String, String> getParameters() {
return params;
}

/**
* @since 7.1
* @since 7.10
*/
public void addNamedParameter(String name, String value, boolean quote) {
params.put(name, value);
CmdParameter cmdParameter = new CmdParameter(value, quote);
cmdParameters.put(name, cmdParameter);
public void addNamedParameter(String name, List<String> values) {
params.put(name, new ParameterValue(values));
}

/**
* @since 7.1
* @since 7.10
*/
public void addNamedParameter(String name, File file, boolean quote) {
addNamedParameter(name, file.getAbsolutePath(), quote);
public String getParameter(String name) {
ParameterValue param = params.get(name);
return param == null ? null : param.getValue();
}

/**
* @since 7.1
*/
public HashMap<String, CmdParameter> getCmdParameters() {
return cmdParameters;
public Map<String, ParameterValue> getParameters() {
return params;
}

/**
* @since 7.1
* A parameter value holding either a single String or a list of Strings.
*
* @since 7.10
*/
public class CmdParameter {
public static class ParameterValue {

private final String value;

private final boolean quote;
private final List<String> values;

public CmdParameter(String value, boolean quote) {
public ParameterValue(String value) {
this.value = value;
this.quote = quote;
values = null;
}

/**
* @since 7.10
*/
public ParameterValue(List<String> values) {
this.values = values;
value = null;
}

public String getValue() {
return value;
}

public boolean isQuote() {
return quote;
public List<String> getValues() {
return values;
}

/**
* Checks whether this is multi-valued.
*
* @since 7.10
*/
public boolean isMulti() {
return values != null;
}
}

}
Expand Up @@ -19,7 +19,6 @@
package org.nuxeo.ecm.platform.commandline.executor.api;

import java.util.List;
import java.util.regex.Pattern;

/**
* Interface for the service that manages commandline execution.
Expand All @@ -28,10 +27,6 @@
*/
public interface CommandLineExecutorService {

Pattern VALID_PARAMETER_PATTERN = Pattern.compile("[\\p{L}_0-9-.%:=/\\\\ ]+");

Pattern VALID_PARAMETER_PATTERN_WIN = Pattern.compile("[\\p{L}_0-9-.%~:=/\\\\ ()]+");

CommandAvailability getCommandAvailability(String commandName);

ExecResult execCommand(String commandName, CmdParameters params) throws CommandNotAvailable;
Expand All @@ -40,22 +35,6 @@ public interface CommandLineExecutorService {

List<String> getAvailableCommands();

/**
* Returns true if the given {@code parameter} is valid to be used in a command.
*
* @since 5.7
*/
boolean isValidParameter(String parameter);

/**
* Checks if the given {@code parameter} is valid to be used in a command.
* <p>
* If not, throws an {@code IllegalArgumentException}.
*
* @since 5.7
*/
void checkParameter(String parameter);

/**
* @return a new {@link CmdParameters} pre-filled with commonly used parameters such as the tmp dir.
* @since 7.4
Expand Down
Expand Up @@ -20,6 +20,8 @@

import java.io.Serializable;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.lang3.SystemUtils;

import org.nuxeo.common.xmap.annotation.XNode;
Expand Down Expand Up @@ -123,4 +125,9 @@ public String getExecutor() {
return CommandLineExecutorComponent.DEFAULT_EXECUTOR;
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}

}
Expand Up @@ -22,9 +22,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -208,31 +206,6 @@ public List<String> getAvailableCommands() {
return cmds;
}

@Override
public boolean isValidParameter(String parameter) {
Pattern VALID_PATTERN;
if (SystemUtils.IS_OS_WINDOWS) {
VALID_PATTERN = VALID_PARAMETER_PATTERN_WIN;
} else {
VALID_PATTERN = VALID_PARAMETER_PATTERN;
}
return VALID_PATTERN.matcher(parameter).matches();
}

@Override
public void checkParameter(String parameter) {
if (!isValidParameter(parameter)) {
Pattern VALID_PATTERN;
if (SystemUtils.IS_OS_WINDOWS) {
VALID_PATTERN = VALID_PARAMETER_PATTERN_WIN;
} else {
VALID_PATTERN = VALID_PARAMETER_PATTERN;
}
throw new IllegalArgumentException(String.format("'%s' contains illegal characters. It should match: %s",
parameter, VALID_PATTERN));
}
}

// ******************************************
// for testing

Expand Down
Expand Up @@ -32,12 +32,14 @@
*/
public class SystemPathExistTester implements CommandTester {

@Override
public CommandTestResult test(CommandLineDescriptor cmdDescriptor) {
String cmd = cmdDescriptor.getCommand();
try {
Runtime.getRuntime().exec(cmd);
Runtime.getRuntime().exec(new String[] { cmd });
} catch (IOException e) {
return new CommandTestResult("command " + cmd + " not found in system path");
return new CommandTestResult(
"command " + cmd + " not found in system path (descriptor " + cmdDescriptor + ")");
}

return new CommandTestResult();
Expand Down

0 comments on commit e35f431

Please sign in to comment.