You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@taverna.apache.org by re...@apache.org on 2015/03/19 14:43:54 UTC
[26/35] incubator-taverna-common-activities git commit: package names
changed to org.apache.taverna.*
http://git-wip-us.apache.org/repos/asf/incubator-taverna-common-activities/blob/433612be/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/RESTActivityFactory.java
----------------------------------------------------------------------
diff --git a/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/RESTActivityFactory.java b/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/RESTActivityFactory.java
new file mode 100644
index 0000000..8e6d8f2
--- /dev/null
+++ b/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/RESTActivityFactory.java
@@ -0,0 +1,164 @@
+/*
+* 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.taverna.activities.rest;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.taverna.activities.rest.URISignatureHandler.URISignatureParsingException;
+import net.sf.taverna.t2.security.credentialmanager.CredentialManager;
+import net.sf.taverna.t2.workflowmodel.Edits;
+import net.sf.taverna.t2.workflowmodel.processor.activity.ActivityConfigurationException;
+import net.sf.taverna.t2.workflowmodel.processor.activity.ActivityFactory;
+import net.sf.taverna.t2.workflowmodel.processor.activity.ActivityInputPort;
+import net.sf.taverna.t2.workflowmodel.processor.activity.ActivityOutputPort;
+
+import org.apache.http.client.CredentialsProvider;
+import org.apache.log4j.Logger;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+/**
+ * An {@link ActivityFactory} for creating <code>RESTActivity</code>.
+ *
+ * @author David Withers
+ */
+public class RESTActivityFactory implements ActivityFactory {
+
+ private static Logger logger = Logger.getLogger(RESTActivityFactory.class);
+
+ private CredentialsProvider credentialsProvider;
+ private Edits edits;
+
+ @Override
+ public RESTActivity createActivity() {
+ RESTActivity activity = new RESTActivity(credentialsProvider);
+ activity.setEdits(edits);
+ return activity;
+ }
+
+ @Override
+ public URI getActivityType() {
+ return URI.create(RESTActivity.URI);
+ }
+
+ @Override
+ public JsonNode getActivityConfigurationSchema() {
+ ObjectMapper objectMapper = new ObjectMapper();
+ try {
+ return objectMapper.readTree(getClass().getResource("/schema.json"));
+ } catch (IOException e) {
+ return objectMapper.createObjectNode();
+ }
+ }
+
+ public void setCredentialManager(CredentialManager credentialManager) {
+ credentialsProvider = new RESTActivityCredentialsProvider(credentialManager);
+ }
+
+ @Override
+ public Set<ActivityInputPort> getInputPorts(JsonNode configuration)
+ throws ActivityConfigurationException {
+ Set<ActivityInputPort> activityInputPorts = new HashSet<>();
+ RESTActivityConfigurationBean configBean = new RESTActivityConfigurationBean(configuration);
+ // ---- CREATE INPUTS ----
+
+ // all input ports are dynamic and depend on the configuration
+ // of the particular instance of the REST activity
+
+ // POST and PUT operations send data, so an input for the message body
+ // is required
+ if (RESTActivity.hasMessageBodyInputPort(configBean.getHttpMethod())) {
+ // the input message will be just an XML string for now
+ activityInputPorts.add(edits.createActivityInputPort(RESTActivity.IN_BODY, 0, true, null, configBean.getOutgoingDataFormat()
+ .getDataFormat()));
+ }
+
+ // now process the URL signature - extract all placeholders and create
+ // an input port for each
+ List<String> placeholders = URISignatureHandler
+ .extractPlaceholders(configBean.getUrlSignature());
+ String acceptsHeaderValue = configBean.getAcceptsHeaderValue();
+ if (acceptsHeaderValue != null && !acceptsHeaderValue.isEmpty()) {
+ try {
+ List<String> acceptsPlaceHolders = URISignatureHandler
+ .extractPlaceholders(acceptsHeaderValue);
+ acceptsPlaceHolders.removeAll(placeholders);
+ placeholders.addAll(acceptsPlaceHolders);
+ }
+ catch (URISignatureParsingException e) {
+ logger.error(e);
+ }
+ }
+ for (ArrayList<String> httpHeaderNameValuePair : configBean.getOtherHTTPHeaders()) {
+ try {
+ List<String> headerPlaceHolders = URISignatureHandler
+ .extractPlaceholders(httpHeaderNameValuePair.get(1));
+ headerPlaceHolders.removeAll(placeholders);
+ placeholders.addAll(headerPlaceHolders);
+ }
+ catch (URISignatureParsingException e) {
+ logger.error(e);
+ }
+ }
+ for (String placeholder : placeholders) {
+ // these inputs will have a dynamic name each;
+ // the data type is string as they are the values to be
+ // substituted into the URL signature at the execution time
+ activityInputPorts.add(edits.createActivityInputPort(placeholder, 0, true, null, String.class));
+ }
+ return activityInputPorts;
+ }
+
+ @Override
+ public Set<ActivityOutputPort> getOutputPorts(JsonNode configuration)
+ throws ActivityConfigurationException {
+ Set<ActivityOutputPort> activityOutputPorts = new HashSet<>();
+ RESTActivityConfigurationBean configBean = new RESTActivityConfigurationBean(configuration);
+ // ---- CREATE OUTPUTS ----
+ // all outputs are of depth 0 - i.e. just a single value on each;
+
+ // output ports for Response Body and Status are static - they don't
+ // depend on the configuration of the activity;
+ activityOutputPorts.add(edits.createActivityOutputPort(RESTActivity.OUT_RESPONSE_BODY, 0, 0));
+ activityOutputPorts.add(edits.createActivityOutputPort(RESTActivity.OUT_STATUS, 0, 0));
+ if (configBean.getShowActualUrlPort()) {
+ activityOutputPorts.add(edits.createActivityOutputPort(RESTActivity.OUT_COMPLETE_URL, 0, 0));
+ }
+ if (configBean.getShowResponseHeadersPort()) {
+ activityOutputPorts.add(edits.createActivityOutputPort(RESTActivity.OUT_RESPONSE_HEADERS, 1, 1));
+ }
+ // Redirection port may be hidden/shown
+ if (configBean.getShowRedirectionOutputPort()) {
+ activityOutputPorts.add(edits.createActivityOutputPort(RESTActivity.OUT_REDIRECTION, 0, 0));
+ }
+ return activityOutputPorts;
+ }
+
+ public void setEdits(Edits edits) {
+ this.edits = edits;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-common-activities/blob/433612be/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/RESTActivityHealthCheck.java
----------------------------------------------------------------------
diff --git a/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/RESTActivityHealthCheck.java b/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/RESTActivityHealthCheck.java
new file mode 100644
index 0000000..7986fe5
--- /dev/null
+++ b/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/RESTActivityHealthCheck.java
@@ -0,0 +1,51 @@
+/*
+* 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.taverna.activities.rest;
+
+import net.sf.taverna.t2.visit.VisitKind;
+import net.sf.taverna.t2.visit.Visitor;
+
+/**
+ * A <code>RESTActivityHealthCheck</code> is a kind of visit that determines
+ * if the corresponding REST activity in a workflow will work during a workflow run.
+ *
+ * @author Sergejs Aleksejevs
+ */
+public class RESTActivityHealthCheck extends VisitKind {
+
+ // The following values indicate the type of results that can be associated
+ // with a VisitReport generated by a health-checking visitor.
+
+ public static final int CORRECTLY_CONFIGURED = 0;
+ public static final int GENERAL_CONFIG_PROBLEM = 10;
+
+ @Override
+ public Class<? extends Visitor<?>> getVisitorClass() {
+ return RESTActivityHealthChecker.class;
+ }
+
+ private static class Singleton {
+ private static RESTActivityHealthCheck instance = new RESTActivityHealthCheck();
+ }
+
+ public static RESTActivityHealthCheck getInstance() {
+ return Singleton.instance;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-common-activities/blob/433612be/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/RESTActivityHealthChecker.java
----------------------------------------------------------------------
diff --git a/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/RESTActivityHealthChecker.java b/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/RESTActivityHealthChecker.java
new file mode 100644
index 0000000..7dd73be
--- /dev/null
+++ b/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/RESTActivityHealthChecker.java
@@ -0,0 +1,77 @@
+/*
+* 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.taverna.activities.rest;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import net.sf.taverna.t2.visit.VisitReport;
+import net.sf.taverna.t2.visit.VisitReport.Status;
+import net.sf.taverna.t2.workflowmodel.health.HealthCheck;
+import net.sf.taverna.t2.workflowmodel.health.HealthChecker;
+
+/**
+ * A {@link HealthChecker} for a {@link RESTActivity}.
+ *
+ * @author Sergejs Aleksejevs
+ */
+public class RESTActivityHealthChecker implements HealthChecker<RESTActivity> {
+ @Override
+ public boolean canVisit(Object subject) {
+ return (subject instanceof RESTActivity);
+ }
+
+ @Override
+ public VisitReport visit(RESTActivity activity, List<Object> ancestors) {
+ // collection of validation reports that this health checker will create
+ List<VisitReport> reports = new ArrayList<VisitReport>();
+
+ RESTActivityConfigurationBean configBean = activity.getConfigurationBean();
+ if (configBean.isValid()) {
+ reports.add(new VisitReport(RESTActivityHealthCheck.getInstance(), activity,
+ "REST Activity is configured correctly",
+ RESTActivityHealthCheck.CORRECTLY_CONFIGURED, Status.OK));
+ } else {
+ reports.add(new VisitReport(RESTActivityHealthCheck.getInstance(), activity,
+ "REST Activity - bad configuration",
+ RESTActivityHealthCheck.GENERAL_CONFIG_PROBLEM, Status.SEVERE));
+ }
+
+ // (possibly other types of reports could be added later)
+
+ // collection all reports together
+ Status worstStatus = VisitReport.getWorstStatus(reports);
+ VisitReport report = new VisitReport(RESTActivityHealthCheck.getInstance(), activity,
+ "REST Activity Report", HealthCheck.NO_PROBLEM, worstStatus, reports);
+
+ return report;
+ }
+
+ /**
+ * Health check for the REST activity only involves
+ * verifying details in the configuration bean -
+ * that is quick.
+ */
+ @Override
+ public boolean isTimeConsuming() {
+ return false;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-common-activities/blob/433612be/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/URISignatureHandler.java
----------------------------------------------------------------------
diff --git a/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/URISignatureHandler.java b/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/URISignatureHandler.java
new file mode 100644
index 0000000..ead2362
--- /dev/null
+++ b/taverna-rest-activity/src/main/java/org/apache/taverna/activities/rest/URISignatureHandler.java
@@ -0,0 +1,431 @@
+/*
+* 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.taverna.activities.rest;
+
+import java.io.UnsupportedEncodingException;
+import java.text.Normalizer;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+
+
+/**
+ * This class deals with URI signatures - essentially, strings that represent
+ * some resource's URI with zero or more placeholders. URI signatures are known
+ * at workflow definition time and represent the pattern of the complete URIs
+ * that will be used at workflow run time.
+ *
+ * An example of the URI signature is:
+ * http://sysmo-db.org/sops/{sop_id}/experimental_conditions
+ * /{cond_id}?condition_unit={unit}
+ *
+ * Placeholders "{sop_id}", "{cond_id}" and "{unit}" will be replaced by the
+ * real values prior to using the URI for a real request.
+ *
+ * This class is concerned with validation of URI signatures, extraction of
+ * placeholders and substituting placeholders to generate complete URIs.
+ *
+ * @author Sergejs Aleksejevs
+ */
+public class URISignatureHandler {
+
+ private static Logger logger = Logger.getLogger(URISignatureHandler.class);
+
+
+ public static final char PLACEHOLDER_START_SYMBOL = '{';
+ public static final char PLACEHOLDER_END_SYMBOL = '}';
+
+ /**
+ * Extracts placeholders of the given URI signature with their positions in
+ * the signature in the order of their occurrence.
+ *
+ * Extraction is done in a robust way with signature validity checks being
+ * carried out simultaneously. This makes sure that even with no explicit
+ * validation (see {@link URISignatureHandler#isValid(String)}) no
+ * unexpected faults occur.
+ *
+ * @param uriSignature
+ * The URI signature to process.
+ * @return A map of placeholders as they are encountered (from start to end)
+ * in the URI signature and their start positions. Keys of the map
+ * are the "titles" of the placeholders without opening and closing
+ * placeholder symbols; values are the URI signature string indices,
+ * where the title of the corresponding placeholder starts in the
+ * string.
+ */
+ public static LinkedHashMap<String, Integer> extractPlaceholdersWithPositions(
+ String uriSignature) {
+ // no signature - nothing to process
+ if (uriSignature == null || uriSignature.isEmpty())
+ throw new URISignatureParsingException(
+ "URI signature is null or empty - nothing to process.");
+
+ LinkedHashMap<String, Integer> foundPlaceholdersWithPositions = new LinkedHashMap<>();
+
+ int nestingLevel = 0;
+ int startSymbolIdx = -1;
+
+ // go through the signature character by character trying to extract
+ // placeholders
+ for (int i = 0; i < uriSignature.length(); i++) {
+ switch (uriSignature.charAt(i)) {
+ case PLACEHOLDER_START_SYMBOL:
+ if (++nestingLevel != 1)
+ throw new URISignatureParsingException(
+ "Malformed URL: at least two parameter placeholder opening\n" +
+ "symbols follow each other without being closed appropriately\n" +
+ "(possibly the signature contains nested placeholders).");
+ startSymbolIdx = i;
+ break;
+
+ case PLACEHOLDER_END_SYMBOL:
+ if (--nestingLevel < 0)
+ throw new URISignatureParsingException(
+ "Malformed URL: parameter placeholder closing symbol found before the opening one.");
+ if (nestingLevel == 0) {
+ // correctly opened and closed placeholder found; check if
+ // it is a "fresh" one
+ String placeholderCandidate = uriSignature.substring(
+ startSymbolIdx + 1, i);
+ if (!foundPlaceholdersWithPositions
+ .containsKey(placeholderCandidate)) {
+ foundPlaceholdersWithPositions.put(
+ placeholderCandidate, startSymbolIdx + 1);
+ } else {
+ throw new URISignatureParsingException(
+ "Malformed URL: duplicate parameter placeholder \""
+ + placeholderCandidate + "\" found.");
+ }
+ }
+ break;
+
+ default:
+ continue;
+ }
+ }
+
+ // the final check - make sure that after traversing the string, we are
+ // not "inside" one of the placeholders (e.g. this could happen if a
+ // placeholder
+ // opening symbol was found, but the closing one never occurred after
+ // that)
+ if (nestingLevel > 0)
+ throw new URISignatureParsingException(
+ "Malformed URL: parameter placeholder opening symbol found,\n"
+ + "but the closing one has not been encountered.");
+
+ return foundPlaceholdersWithPositions;
+ }
+
+ /**
+ * Works identical to
+ * {@link URISignatureHandler#extractPlaceholdersWithPositions(String)}
+ * except for returning only the list of placeholder titles - without
+ * positions.
+ *
+ * @param uriSignature
+ * The URI signature to process.
+ * @return List of the placeholder titles in the order of their occurrence
+ * in the provided URI signature.
+ */
+ public static List<String> extractPlaceholders(String uriSignature) {
+ return new ArrayList<>(extractPlaceholdersWithPositions(uriSignature)
+ .keySet());
+ }
+
+ /**
+ * This method performs explicit validation of the URI signature. If the
+ * validation succeeds, the method terminates quietly; in case of any
+ * identified problems a {@link URISignatureParsingException} is thrown.
+ *
+ * @param uriSignature
+ * The URI signature to validate.
+ * @throws URISignatureParsingException
+ */
+ public static void validate(String uriSignature)
+ throws URISignatureParsingException {
+ // this method essentially needs to do exactly the same thing
+ // as the method to extract the placeholders with their corresponding
+ // positions; all necessary validation is already performed there -
+ // hence the trick is simply to call that method (discarding its
+ // output),
+ // while keeping track of any exceptions that may be generated by the
+ // called method;
+ //
+ // for this simply call the placeholder extraction method - any
+ // exceptions
+ // will be forwarded up the method call stack; in case of success, the
+ // method
+ // will terminate quietly
+ extractPlaceholdersWithPositions(uriSignature);
+ }
+
+ /**
+ * Check if the URL string contains "unsafe" characters, i.e. characters
+ * that need URL-encoding.
+ * From RFC 1738: "...Only alphanumerics [0-9a-zA-Z], the special
+ * characters "$-_.+!*'()," (not including the quotes) and reserved
+ * characters used for their reserved purposes may be
+ * used unencoded within a URL."
+ * Reserved characters are: ";/?:@&=" ..." and "%" used for escaping.
+ */
+ public static void checkForUnsafeCharacters(String candidateURLSignature) throws URISignatureParsingException{
+ String allowedURLCharactersString = new String("abcdefghijklmnopqrstuvwxyz0123456789$-_.+!*'(),;/?:@&=%");
+ char[] allowedURLCharactersArray = allowedURLCharactersString.toCharArray();
+ List<Character> allowedURLCharactersList = new ArrayList<Character>();
+ for (char value : allowedURLCharactersArray)
+ allowedURLCharactersList.add(new Character(value));
+
+ int index = 0;
+ String unsafeCharactersDetected = "";
+ while (index < candidateURLSignature.length()){
+ char character = candidateURLSignature.charAt(index);
+ if (character == '{') { // a start of a parameter
+ // This is a paramater name - ignore until we find the closing '}'
+ index++;
+ while(character != '}' && index < candidateURLSignature.length()){
+ character = candidateURLSignature.charAt(index);
+ index++;
+ }
+ } else if (!allowedURLCharactersList.contains(Character
+ .valueOf(Character.toLowerCase(character)))) {
+ // We found an unsafe character in the URL - add to the list of unsafe characters
+ unsafeCharactersDetected += "'" + character + "', ";
+ index++;
+ } else {
+ index++;
+ }
+ }
+ String message = "";
+ unsafeCharactersDetected = unsafeCharactersDetected.trim();
+ if (unsafeCharactersDetected.endsWith(",")){ // remove the last ","
+ unsafeCharactersDetected = unsafeCharactersDetected.substring(0, unsafeCharactersDetected.lastIndexOf(','));
+ }
+ if (!unsafeCharactersDetected.equals("")){
+ message += "REST service's URL contains unsafe characters that need\nto be URL-encoded or the service will most probably fail:\n"+ unsafeCharactersDetected;
+ throw new URISignatureParsingException(message);
+ }
+ }
+
+
+ /**
+ * Tests whether the provided URI signature is valid or not.
+ *
+ * @param uriSignature
+ * URI signature to check for validity.
+ * @return <code>true</code> if the URI signature is valid;
+ * <code>false</code> otherwise.
+ */
+ public static boolean isValid(String uriSignature) {
+ try {
+ // no exceptions are generated by validate(), the validation has
+ // succeeded
+ validate(uriSignature);
+ return (true);
+ } catch (URISignatureParsingException e) {
+ return false;
+ }
+ }
+
+ /**
+ * Substitutes real values for all placeholders encountered in the URI
+ * signature and produces a complete URI that can be used directly.
+ *
+ * @param uriSignature
+ * The URI signature to use as a basis.
+ * @param parameters
+ * Map of {name,value} pairs for all placeholders in the
+ * signature. These values will be used to replace the
+ * placeholders in the signature.
+ * @param escapeParameters
+ * Whether to URL-escape paramaters before placing them in the
+ * final URL.
+ * @return A complete URI with all placeholders replaced by the provided
+ * values.
+ * @throws URISignatureParsingException
+ * Thrown if there is a problem with the provided URI signature
+ * (e.g. null, empty, ill-formed, etc).
+ * @throws URIGenerationFromSignatureException
+ * Thrown if there is a problem with the provided parameter map
+ * (e.g. null, empty, not containing enough values for some of
+ * the placeholders found in <code>uriSignature</code>.
+ */
+ public static String generateCompleteURI(String uriSignature,
+ Map<String, String> specifiedParameters, boolean escapeParameters)
+ throws URISignatureParsingException,
+ URIGenerationFromSignatureException {
+ StringBuilder completeURI = new StringBuilder(uriSignature);
+
+ // no need to make any checks on the uriSignature - it is
+ // already handled by extractPlaceholdersWithPositions() --
+ // if something goes wrong a runtime exception will be thrown
+ // during placeholder extraction
+ LinkedHashMap<String, Integer> placeholdersWithPositions = extractPlaceholdersWithPositions(uriSignature);
+
+ // check that the URI signature contains some placeholders
+ if (placeholdersWithPositions.keySet().size() > 0) {
+ Map<String, String> parameters;
+ // some work will actually have to be done to replace placeholders
+ // with real values;
+ // check that the parameter map contains some values
+ if (specifiedParameters == null || specifiedParameters.isEmpty()) {
+ parameters = Collections.emptyMap();
+ } else {
+ parameters = specifiedParameters;
+ }
+
+ // the 'placeholders' linked list is guaranteed to be in the order
+ // of occurrence of placeholders in the URI signature;
+ // this will allow to traverse the URI signature and replace the
+ // placeholders in the reverse order --
+ // this way it is possible to use the indices of placeholders that
+ // were already found during their extraction to
+ // improve performance
+ LinkedList<String> placeholders = new LinkedList<String>(
+ placeholdersWithPositions.keySet());
+ Collections.reverse(placeholders);
+ Iterator<String> placeholdersIterator = placeholders.iterator();
+
+ while (placeholdersIterator.hasNext()) {
+ String placeholder = placeholdersIterator.next();
+ int placeholderStartPos = placeholdersWithPositions
+ .get(placeholder) - 1;
+ int placeholderEndPos = placeholderStartPos
+ + placeholder.length() + 2;
+ if (parameters.containsKey(placeholder)) {
+ if (escapeParameters) {
+ completeURI.replace(placeholderStartPos,
+ placeholderEndPos, urlEncodeQuery(parameters
+ .get(placeholder)));
+ } else {
+ completeURI.replace(placeholderStartPos,
+ placeholderEndPos, parameters.get(placeholder));
+ }
+ } else {
+ int qnPos = completeURI.lastIndexOf("?", placeholderStartPos);
+ int ampPos = completeURI.lastIndexOf("&", placeholderStartPos);
+ int slashPos = completeURI.lastIndexOf("/", placeholderStartPos);
+ int startParamPos = Math.max(qnPos, ampPos);
+ if (startParamPos > -1 && startParamPos > slashPos) {
+ // We found an optional parameter, so delete all of it
+ if (qnPos > ampPos) {
+ // It might be the first or only parameter so delete carefully
+ if (placeholderEndPos >= (completeURI.length() - 1)) {
+ // No parameters
+ completeURI.replace(startParamPos, placeholderEndPos, "");
+ } else {
+ // Remove the & from the following parameter, not the ? that starts
+ completeURI.replace(startParamPos + 1, placeholderEndPos + 1, "");
+ }
+ } else {
+ // Just delete the optional parameter in total
+ completeURI.replace(startParamPos, placeholderEndPos, "");
+ }
+ } else {
+ throw new URIGenerationFromSignatureException(
+ "Parameter map does not contain a key/value for \""
+ + placeholder + "\" mandatory placeholder");
+ }
+ }
+ }
+ }
+ /*
+ * else { NO PLACEHOLDERS, SO NOTHING TO REPLACE WITH REAL VALUES - JUST
+ * RETURN THE ORIGINAL 'uriSignature' }
+ */
+
+ return (completeURI.toString());
+ }
+
+ /**
+ * Exceptions of this type may be thrown when errors occur during URI
+ * signature parsing - these will often indicate the reason for failure
+ * (e.g. missing URI signature, nested placeholders, ill-formed signature,
+ * etc).
+ *
+ * @author Sergejs Aleksejevs
+ */
+ @SuppressWarnings("serial")
+ public static class URISignatureParsingException extends
+ IllegalArgumentException {
+ public URISignatureParsingException() {
+ }
+
+ public URISignatureParsingException(String message) {
+ super(message);
+ }
+ }
+
+ /**
+ * Exceptions of this type may be thrown during generation of a complete URI
+ * from the provided signature and parameter hash. These may occur because
+ * of wrong parameters, etc.
+ *
+ * @author Sergejs Aleksejevs
+ */
+ @SuppressWarnings("serial")
+ public static class URIGenerationFromSignatureException extends
+ RuntimeException {
+ public URIGenerationFromSignatureException() {
+ }
+
+ public URIGenerationFromSignatureException(String message) {
+ super(message);
+ }
+ }
+
+ /**
+ * Prepares the string to serve as a part of url query to the server.
+ *
+ * @param query
+ * The string that needs URL encoding.
+ * @return URL encoded string that can be inserted into the request URL.
+ */
+ public static String urlEncodeQuery(String query) {
+ String ns = Normalizer.normalize(query, Normalizer.Form.NFC);
+ byte[] bb = null;
+ try {
+ bb = ns.getBytes("UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ logger.error(e);
+ return query;
+ }
+
+ StringBuffer sb = new StringBuffer();
+
+ for (int i = 0; i < bb.length; i++) {
+ int b = bb[i] & 0xff;
+ if (!Character.isLetterOrDigit(b) || (b >= 0x80)) {
+ sb.append("%");
+ sb.append(Integer.toHexString(b).toUpperCase());
+ } else {
+ sb.append((char)b);
+ }
+ }
+ return sb.toString();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-common-activities/blob/433612be/taverna-rest-activity/src/main/resources/META-INF/spring/rest-activity-context.xml
----------------------------------------------------------------------
diff --git a/taverna-rest-activity/src/main/resources/META-INF/spring/rest-activity-context.xml b/taverna-rest-activity/src/main/resources/META-INF/spring/rest-activity-context.xml
index f1c900e..ee58f29 100644
--- a/taverna-rest-activity/src/main/resources/META-INF/spring/rest-activity-context.xml
+++ b/taverna-rest-activity/src/main/resources/META-INF/spring/rest-activity-context.xml
@@ -3,9 +3,9 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="RESTActivityHealthChecker" class="net.sf.taverna.t2.activities.rest.RESTActivityHealthChecker" />
+ <bean id="RESTActivityHealthChecker" class="org.apache.taverna.activities.rest.RESTActivityHealthChecker" />
- <bean id="restActivityFactory" class="net.sf.taverna.t2.activities.rest.RESTActivityFactory">
+ <bean id="restActivityFactory" class="org.apache.taverna.activities.rest.RESTActivityFactory">
<property name="credentialManager" ref="credentialManager" />
<property name="edits" ref="edits" />
</bean>
http://git-wip-us.apache.org/repos/asf/incubator-taverna-common-activities/blob/433612be/taverna-rest-activity/src/test/java/net/sf/taverna/t2/activities/rest/ApacheHttpClientUsageTest.java.bak
----------------------------------------------------------------------
diff --git a/taverna-rest-activity/src/test/java/net/sf/taverna/t2/activities/rest/ApacheHttpClientUsageTest.java.bak b/taverna-rest-activity/src/test/java/net/sf/taverna/t2/activities/rest/ApacheHttpClientUsageTest.java.bak
deleted file mode 100644
index 518fbc3..0000000
--- a/taverna-rest-activity/src/test/java/net/sf/taverna/t2/activities/rest/ApacheHttpClientUsageTest.java.bak
+++ /dev/null
@@ -1,215 +0,0 @@
-package net.sf.taverna.t2.activities.rest;
-
-import java.awt.BorderLayout;
-import java.awt.Container;
-import java.awt.Dimension;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.io.BufferedReader;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
-
-import javax.swing.BorderFactory;
-import javax.swing.JButton;
-import javax.swing.JComboBox;
-import javax.swing.JFrame;
-import javax.swing.JPanel;
-import javax.swing.JScrollPane;
-import javax.swing.JTextArea;
-import javax.swing.JTextField;
-
-import org.apache.commons.codec.binary.Base64;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpHost;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpDelete;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.client.methods.HttpPut;
-import org.apache.http.client.methods.HttpRequestBase;
-import org.apache.http.client.methods.HttpUriRequest;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.protocol.BasicHttpContext;
-import org.apache.http.protocol.ExecutionContext;
-import org.apache.http.protocol.HttpContext;
-
-/**
- *
- * @author Sergejs Aleksejevs
- */
-public class ApacheHttpClientUsageTest extends JFrame implements ActionListener
-{
- private JComboBox cbHttpMethod;
- private JTextField tfAddressBar;
- private JButton bGo;
- private JTextArea taResponse;
- private JScrollPane spResponse;
-
-
- /**
- * Constructor is solely involved in the UI initialisation.
- */
- public ApacheHttpClientUsageTest()
- {
- Container contentPane = this.getContentPane();
- contentPane.setLayout(new BorderLayout(5,5));
-
- bGo = new JButton("GO!");
- bGo.addActionListener(this);
- bGo.setDefaultCapable(true);
- this.getRootPane().setDefaultButton(bGo);
-
- tfAddressBar = new JTextField(50);
- tfAddressBar.setPreferredSize(new Dimension(0,bGo.getPreferredSize().height));
- tfAddressBar.setText("http://test.biocatalogue.org/");
-
- JPanel jpAddressBar = new JPanel();
- jpAddressBar.add(tfAddressBar);
- jpAddressBar.add(bGo);
-
- cbHttpMethod = new JComboBox(RESTActivity.HTTP_METHOD.values());
- cbHttpMethod.setBorder(BorderFactory.createCompoundBorder(
- BorderFactory.createEmptyBorder(5, 5, 5, 5),
- cbHttpMethod.getBorder()));
-
- JPanel jpAllNavigation = new JPanel(new BorderLayout());
- jpAllNavigation.add(jpAddressBar, BorderLayout.NORTH);
- jpAllNavigation.add(cbHttpMethod, BorderLayout.CENTER);
-
- contentPane.add(jpAllNavigation, BorderLayout.NORTH);
-
- taResponse = new JTextArea(20, 20);
- taResponse.setEditable(true);
-
- spResponse = new JScrollPane(taResponse);
- spResponse.setBorder(BorderFactory.createCompoundBorder(
- BorderFactory.createEmptyBorder(0, 5, 5, 5),
- BorderFactory.createEtchedBorder()));
- contentPane.add(spResponse, BorderLayout.CENTER);
-
- this.pack();
- this.setLocationRelativeTo(null); // center on screen
- }
-
-
- /**
- * Click handler for the only button there is - the "GO!" button
- */
- public void actionPerformed(ActionEvent e)
- {
- if (e.getSource().equals(bGo))
- {
- try {
- switch ((RESTActivity.HTTP_METHOD)cbHttpMethod.getSelectedItem()) {
- case GET: doGET(); break;
- case POST: doPOST(); break;
- case PUT: doPUT(); break;
- case DELETE: doDELETE(); break;
- }
- }
- catch (Exception ex) {
- taResponse.setText(ex + "\n\n" + ex.getStackTrace());
- }
- }
- }
-
-
- private void doGET() {
- HttpGet httpGet = new HttpGet(tfAddressBar.getText());
- httpGet.addHeader("Accept", "application/xml");
- performHTTPRequest(httpGet);
- }
-
-
- private void doPOST() throws UnsupportedEncodingException {
-// // POST TO MYEXPERIMENT - basic auth
-// HttpPost httpPost = new HttpPost("http://sandbox.myexperiment.org/comment.xml");
-// httpPost.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(("LOGIN" + ":" + "PASSWORD").getBytes())));
-// httpPost.addHeader("Accept", "application/xml");
-// httpPost.addHeader("Content-Type", "application/xml");
-// httpPost.setEntity(new StringEntity("<comment><subject resource=\"http://sandbox.myexperiment.org/files/226\"/><comment>1234567</comment></comment>"));
-// performHTTPRequest(httpPost);
-
- // POST TO BIOCATALOGUE - no auth
- HttpPost httpPost = new HttpPost(tfAddressBar.getText());
- httpPost.addHeader("Accept", "application/xml");
- httpPost.addHeader("Content-Type", "application/xml");
- httpPost.setEntity(new StringEntity("<searchByData><searchType>input</searchType><limit>20</limit><data>test</data></searchByData>"));
- performHTTPRequest(httpPost);
- }
-
-
- private void doPUT() throws UnsupportedEncodingException {
- HttpPut httpPut = new HttpPut("http://sandbox.myexperiment.org/comment.xml?id=251");
- httpPut.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(("LOGIN" + ":" + "PASSWORD").getBytes())));
- httpPut.addHeader("Accept", "application/xml");
- httpPut.addHeader("Content-Type", "application/xml");
- httpPut.setEntity(new StringEntity("<comment><subject resource=\"http://sandbox.myexperiment.org/files/226\"/><comment>12345678</comment></comment>"));
- performHTTPRequest(httpPut);
- }
-
-
- private void doDELETE() {
- HttpDelete httpDelete = new HttpDelete("http://sandbox.myexperiment.org/comment.xml?id=251");
- httpDelete.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(("LOGIN" + ":" + "PASSWORD").getBytes())));
- httpDelete.addHeader("Accept", "application/xml");
- performHTTPRequest(httpDelete);
- }
-
-
- private void performHTTPRequest(HttpRequestBase httpRequest) {
- try {
- StringBuilder responseStr = new StringBuilder();
- // ---------------------------------------------
-
- HttpClient httpClient = new DefaultHttpClient();
- HttpContext localContext = new BasicHttpContext();
-
- HttpResponse response = httpClient.execute(httpRequest, localContext);
- // ---
- // TRACK WHERE THE FINAL REDIRECT ENDS UP - target host + URI
- HttpHost target = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
- HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST);
-
- responseStr.append("Final request URI: " + req.getMethod() + " " + target + req.getURI() + "\n");
-// System.out.println("Target host: " + target);
-// System.out.println("Final request URI: " + req.getURI());
-// System.out.println("Final request method: " + req.getMethod());
- // ---
- responseStr.append(response.getStatusLine() + "\n");
-
- HttpEntity entity = response.getEntity();
- responseStr.append(entity.getContentType() + "\n\n");
-
- if (entity != null) {
- InputStream in = entity.getContent();
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
-
- String str;
- while ((str = reader.readLine()) != null) {
- responseStr.append(str + "\n");
- }
-
- taResponse.setText(responseStr.toString());
- taResponse.setCaretPosition(0);
- }
-
- httpClient.getConnectionManager().shutdown();
- }
- catch (Exception ex) {
- taResponse.setText(ex.getMessage() + "\n\n" + ex.getStackTrace());
- }
- }
-
-
- public static void main(String[] args)
- {
- ApacheHttpClientUsageTest frame = new ApacheHttpClientUsageTest();
- frame.setVisible(true);
- frame.tfAddressBar.setCaretPosition(frame.tfAddressBar.getText().length());
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-common-activities/blob/433612be/taverna-rest-activity/src/test/java/net/sf/taverna/t2/activities/rest/ExampleActivityTest.java.bak
----------------------------------------------------------------------
diff --git a/taverna-rest-activity/src/test/java/net/sf/taverna/t2/activities/rest/ExampleActivityTest.java.bak b/taverna-rest-activity/src/test/java/net/sf/taverna/t2/activities/rest/ExampleActivityTest.java.bak
deleted file mode 100644
index 56b6904..0000000
--- a/taverna-rest-activity/src/test/java/net/sf/taverna/t2/activities/rest/ExampleActivityTest.java.bak
+++ /dev/null
@@ -1,153 +0,0 @@
-package net.sf.taverna.t2.activities.rest;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.net.URI;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-import net.sf.taverna.t2.activities.testutils.ActivityInvoker;
-import net.sf.taverna.t2.reference.ErrorDocument;
-import net.sf.taverna.t2.reference.ExternalReferenceSPI;
-import net.sf.taverna.t2.workflowmodel.OutputPort;
-import net.sf.taverna.t2.workflowmodel.processor.activity.ActivityConfigurationException;
-import net.sf.taverna.t2.workflowmodel.processor.activity.ActivityInputPort;
-
-import org.junit.Before;
-import org.junit.Test;
-
-public class ExampleActivityTest {
-
- private RESTActivityConfigurationBean configBean;
-
- private RESTActivity activity = new RESTActivity();
-
- @Before
- public void makeConfigBean() throws Exception {
- configBean = new RESTActivityConfigurationBean();
- configBean.setExampleString("something");
- configBean
- .setExampleUri(URI.create("http://localhost:8080/myEndPoint"));
- }
-
- @Test(expected = ActivityConfigurationException.class)
- public void invalidConfiguration() throws ActivityConfigurationException {
- RESTActivityConfigurationBean invalidBean = new RESTActivityConfigurationBean();
- invalidBean.setExampleString("invalidExample");
- // Should throw ActivityConfigurationException
- activity.configure(invalidBean);
- }
-
- @Test
- public void executeAsynch() throws Exception {
- activity.configure(configBean);
-
- Map<String, Object> inputs = new HashMap<String, Object>();
- inputs.put("firstInput", "hello");
-
- Map<String, Class<?>> expectedOutputTypes = new HashMap<String, Class<?>>();
- expectedOutputTypes.put("simpleOutput", String.class);
- expectedOutputTypes.put("moreOutputs", ExternalReferenceSPI.class);
-
- Map<String, Object> outputs = ActivityInvoker.invokeAsyncActivity(
- activity, inputs, expectedOutputTypes);
-
- assertEquals("Unexpected outputs", 2, outputs.size());
- assertEquals("da39a3ee5e6b4b0d3255bfef95601890afd80709", outputs.get("simpleOutput"));
- ErrorDocument errorDoc = (ErrorDocument) outputs.get("moreOutputs");
- assertEquals("java.lang.Exception: There are no more values",
- errorDoc.getExceptionMessage());
-
- }
-
-
-
- @Test
- public void checksumOfExtraData() throws Exception {
- configBean.setExampleString("specialCase");
- activity.configure(configBean);
-
- Map<String, Object> inputs = new HashMap<String, Object>();
- inputs.put("firstInput", "hello");
- inputs.put("extraData", Arrays.asList("Test1".getBytes("utf8"),
- "Test2".getBytes("utf8")));
-
-
- Map<String, Class<?>> expectedOutputTypes = new HashMap<String, Class<?>>();
- expectedOutputTypes.put("simpleOutput", String.class);
- expectedOutputTypes.put("moreOutputs", ExternalReferenceSPI.class);
- expectedOutputTypes.put("report", String.class);
-
-
- Map<String, Object> outputs = ActivityInvoker.invokeAsyncActivity(
- activity, inputs, expectedOutputTypes);
-
- assertEquals("Unexpected outputs", 3, outputs.size());
- assertEquals("35bceb434ff8e69fb89b829e461c921a28b423b3", outputs.get("simpleOutput"));
- ErrorDocument errorDoc = (ErrorDocument) outputs.get("moreOutputs");
- assertEquals("java.lang.Exception: There are no more values",
- errorDoc.getExceptionMessage());
-
- }
-
-
-
- @Test
- public void reConfiguredActivity() throws Exception {
- assertEquals("Unexpected inputs", 0, activity.getInputPorts().size());
- assertEquals("Unexpected outputs", 0, activity.getOutputPorts().size());
-
- activity.configure(configBean);
- assertEquals("Unexpected inputs", 1, activity.getInputPorts().size());
- assertEquals("Unexpected outputs", 2, activity.getOutputPorts().size());
-
- activity.configure(configBean);
- // Should not change on reconfigure
- assertEquals("Unexpected inputs", 1, activity.getInputPorts().size());
- assertEquals("Unexpected outputs", 2, activity.getOutputPorts().size());
- }
-
- @Test
- public void reConfiguredSpecialPorts() throws Exception {
- activity.configure(configBean);
-
- RESTActivityConfigurationBean specialBean = new RESTActivityConfigurationBean();
- specialBean.setExampleString("specialCase");
- specialBean.setExampleUri(URI
- .create("http://localhost:8080/myEndPoint"));
- activity.configure(specialBean);
- // Should now have added the optional ports
- assertEquals("Unexpected inputs", 2, activity.getInputPorts().size());
- assertEquals("Unexpected outputs", 3, activity.getOutputPorts().size());
- }
-
- @Test
- public void configureActivity() throws Exception {
- Set<String> expectedInputs = new HashSet<String>();
- expectedInputs.add("firstInput");
-
- Set<String> expectedOutputs = new HashSet<String>();
- expectedOutputs.add("simpleOutput");
- expectedOutputs.add("moreOutputs");
-
- activity.configure(configBean);
-
- Set<ActivityInputPort> inputPorts = activity.getInputPorts();
- assertEquals(expectedInputs.size(), inputPorts.size());
- for (ActivityInputPort inputPort : inputPorts) {
- assertTrue("Wrong input : " + inputPort.getName(), expectedInputs
- .remove(inputPort.getName()));
- }
-
- Set<OutputPort> outputPorts = activity.getOutputPorts();
- assertEquals(expectedOutputs.size(), outputPorts.size());
- for (OutputPort outputPort : outputPorts) {
- assertTrue("Wrong output : " + outputPort.getName(),
- expectedOutputs.remove(outputPort.getName()));
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-common-activities/blob/433612be/taverna-rest-activity/src/test/java/net/sf/taverna/t2/activities/rest/URISignatureHandlerTest.java
----------------------------------------------------------------------
diff --git a/taverna-rest-activity/src/test/java/net/sf/taverna/t2/activities/rest/URISignatureHandlerTest.java b/taverna-rest-activity/src/test/java/net/sf/taverna/t2/activities/rest/URISignatureHandlerTest.java
deleted file mode 100644
index 8aad5ec..0000000
--- a/taverna-rest-activity/src/test/java/net/sf/taverna/t2/activities/rest/URISignatureHandlerTest.java
+++ /dev/null
@@ -1,594 +0,0 @@
-package net.sf.taverna.t2.activities.rest;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.junit.*;
-
-import static org.junit.Assert.*;
-
-public class URISignatureHandlerTest {
- // ==========================================================================
- // TEST URIs
- // ==========================================================================
-
- final String validURI_NoPlaceholders = "http://sysmo-db.org/sops/";
- final String validURI_PlaceholdersInMainPartOfURIOnly = "http://sysmo-db.org/sops/{sop_id}/experimental_conditions/{cond_id}";
- final String validURI_PlaceholdersInQueryStringOnly = "http://sandbox.myexperiment.org/user.xml?id={user_id}&verbose=true";
- final String validURI_3MixedPlaceholders = "http://sysmo-db.org/sops/{sop_id}/experimental_conditions/{cond_id}?condition_unit={unit}";
-
- final String badURI_nullURI = null;
- final String badURI_emptyURI = "";
- final String badURI_SingleOpeningSymbolNoClosingSymbol = "http://sysmo-db.org/sops/{sop_id/experimental_conditions";
- final String badURI_SingleClosingSymbolNoOpeningSymbol = "http://sysmo-db.org/sops/sop_id}/experimental_conditions";
- final String badURI_DoubleOpeningSymbolsNoClosingSymbol = "http://sysmo-db.org/sops/{{sop_id/experimental_conditions";
- final String badURI_DoubleOpeningSymbols = "http://sysmo-db.org/sops/{{sop_id}/experimental_conditions";
- final String badURI_DoubleOpeningSymbolsSpaced = "http://sysmo-db.org/sops/{sop_{id}/experimental_conditions";
- final String badURI_DoubleClosingSymbols = "http://sysmo-db.org/sops/{sop_id}}/experimental_conditions";
- final String badURI_DoubleClosingSymbolsSpaced = "http://sysmo-db.org/sops/{sop}_id}/experimental_conditions";
- final String badURI_NestedPlaceholders = "http://sandbox.myexperiment.org/user.xml?id={user_{id}}&verbose=true";
- final String badURI_NestedPlaceholdersSpaced = "http://sandbox.myexperiment.org/user.xml?id={us{er}_id}&verbose=true";
- final String badURI_DuplicatePlaceholders = "http://sandbox.myexperiment.org/user.xml?id={user_id}&verbose={user_id}";
- final String badURI_DuplicatePlaceholdersWithOthers = "http://sysmo-db.org/sops/{unit}/experimental_conditions/{cond_id}?condition_unit={unit}";
-
- final String validURI_MultipleQueryString =
- "http://dr-site.esrin.esa.int/{catalogue}/genesi/ASA_IMS_1P/rdf/?count={count?}&startPage={startPage?}&startIndex={startIndex?}&q={searchTerms?}";
-
- // ==========================================================================
- // TEST URI SIGNATURE BOOLEAN VALIDATION
- // ==========================================================================
-
- // success cases
-
- @Test
- public void isValid_validURI_NoPlaceholders() {
- assertTrue(URISignatureHandler.isValid(validURI_NoPlaceholders));
- }
-
- @Test
- public void isValid_validURI_PlaceholdersInMainPartOfURIOnly() {
- assertTrue(URISignatureHandler
- .isValid(validURI_PlaceholdersInMainPartOfURIOnly));
- }
-
- @Test
- public void isValid_validURI_PlaceholdersInQueryStringOnly() {
- assertTrue(URISignatureHandler
- .isValid(validURI_PlaceholdersInQueryStringOnly));
- }
-
- @Test
- public void isValid_validURI_MixedPlaceholders() {
- assertTrue(URISignatureHandler.isValid(validURI_3MixedPlaceholders));
- }
-
- @Test
- public void isValid_validURI_MultipleQueryString() {
- assertTrue(URISignatureHandler.isValid(validURI_MultipleQueryString));
- }
-
- // failure cases
-
- @Test
- public void isValid_badURI_nullURI() {
- assertFalse(URISignatureHandler.isValid(badURI_nullURI));
- }
-
- @Test
- public void isValid_badURI_emptyURI() {
- assertFalse(URISignatureHandler.isValid(badURI_emptyURI));
- }
-
- @Test
- public void isValid_badURI_SingleOpeningSymbolNoClosingSymbol() {
- assertFalse(URISignatureHandler
- .isValid(badURI_SingleOpeningSymbolNoClosingSymbol));
- }
-
- @Test
- public void isValid_badURI_SingleClosingSymbolNoOpeningSymbol() {
- assertFalse(URISignatureHandler
- .isValid(badURI_SingleClosingSymbolNoOpeningSymbol));
- }
-
- @Test
- public void isValid_badURI_DoubleOpeningSymbolsNoClosingSymbol() {
- assertFalse(URISignatureHandler
- .isValid(badURI_DoubleOpeningSymbolsNoClosingSymbol));
- }
-
- @Test
- public void isValid_badURI_DoubleOpeningSymbols() {
- assertFalse(URISignatureHandler.isValid(badURI_DoubleOpeningSymbols));
- }
-
- @Test
- public void isValid_badURI_DoubleOpeningSymbolsSpaced() {
- assertFalse(URISignatureHandler
- .isValid(badURI_DoubleOpeningSymbolsSpaced));
- }
-
- @Test
- public void isValid_badURI_DoubleClosingSymbols() {
- assertFalse(URISignatureHandler.isValid(badURI_DoubleClosingSymbols));
- }
-
- @Test
- public void isValid_badURI_DoubleClosingSymbolsSpaced() {
- assertFalse(URISignatureHandler
- .isValid(badURI_DoubleClosingSymbolsSpaced));
- }
-
- @Test
- public void isValid_badURI_NestedPlaceholders() {
- assertFalse(URISignatureHandler.isValid(badURI_NestedPlaceholders));
- }
-
- @Test
- public void isValid_badURI_NestedPlaceholdersSpaced() {
- assertFalse(URISignatureHandler
- .isValid(badURI_NestedPlaceholdersSpaced));
- }
-
- @Test
- public void isValid_badURI_DuplicatePlaceholders() {
- assertFalse(URISignatureHandler.isValid(badURI_DuplicatePlaceholders));
- }
-
- @Test
- public void isValid_badURI_DuplicatePlaceholdersWithOthers() {
- assertFalse(URISignatureHandler
- .isValid(badURI_DuplicatePlaceholdersWithOthers));
- }
-
- // ==========================================================================
- // TEST URI SIGNATURE DETAILED VALIDATION
- // ==========================================================================
-
- // success cases
-
- @Test
- public void validate_validURI_NoPlaceholders() {
- // nothing should happen when this is executed if validation succeeds
- URISignatureHandler.validate(validURI_NoPlaceholders);
- }
-
- @Test
- public void validate_validURI_PlaceholdersInMainPartOfURIOnly() {
- // nothing should happen when this is executed if validation succeeds
- URISignatureHandler.validate(validURI_PlaceholdersInMainPartOfURIOnly);
- }
-
- @Test
- public void validate_validURI_PlaceholdersInQueryStringOnly() {
- // nothing should happen when this is executed if validation succeeds
- URISignatureHandler.validate(validURI_PlaceholdersInQueryStringOnly);
- }
-
- @Test
- public void validate_validURI_MixedPlaceholders() {
- // nothing should happen when this is executed if validation succeeds
- URISignatureHandler.validate(validURI_3MixedPlaceholders);
- }
-
- @Test
- public void validate_validURI_validURI_Multiple() {
- URISignatureHandler.validate(validURI_MultipleQueryString);
- }
-
- // failure cases
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void validate_badURI_nullURI() {
- URISignatureHandler.validate(badURI_nullURI);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void validate_badURI_emptyURI() {
- URISignatureHandler.validate(badURI_emptyURI);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void validate_badURI_SingleOpeningSymbolNoClosingSymbol() {
- URISignatureHandler.validate(badURI_SingleOpeningSymbolNoClosingSymbol);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void validate_badURI_SingleClosingSymbolNoOpeningSymbol() {
- URISignatureHandler.validate(badURI_SingleClosingSymbolNoOpeningSymbol);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void validate_badURI_DoubleOpeningSymbolsNoClosingSymbol() {
- URISignatureHandler
- .validate(badURI_DoubleOpeningSymbolsNoClosingSymbol);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void validate_badURI_DoubleOpeningSymbols() {
- URISignatureHandler.validate(badURI_DoubleOpeningSymbols);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void validate_badURI_DoubleOpeningSymbolsSpaced() {
- URISignatureHandler.validate(badURI_DoubleOpeningSymbolsSpaced);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void validate_badURI_DoubleClosingSymbols() {
- URISignatureHandler.validate(badURI_DoubleClosingSymbols);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void validate_badURI_DoubleClosingSymbolsSpaced() {
- URISignatureHandler.validate(badURI_DoubleClosingSymbolsSpaced);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void validate_badURI_NestedPlaceholders() {
- URISignatureHandler.validate(badURI_NestedPlaceholders);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void validate_badURI_NestedPlaceholdersSpaced() {
- URISignatureHandler.validate(badURI_NestedPlaceholdersSpaced);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void validate_badURI_DuplicatePlaceholders() {
- URISignatureHandler.validate(badURI_DuplicatePlaceholders);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void validate_badURI_DuplicatePlaceholdersWithOthers() {
- URISignatureHandler.validate(badURI_DuplicatePlaceholdersWithOthers);
- }
-
- // ==========================================================================
- // TEST PLACEHOLDER EXTRACTION FROM URI SIGNATURE
- // ==========================================================================
-
- // success cases
-
- @Test
- public void extractPlaceholders_validURI_NoPlaceholders() {
- List<String> placeholders = URISignatureHandler
- .extractPlaceholders(validURI_NoPlaceholders);
- assertNotNull(placeholders);
- assertEquals(0, placeholders.size());
- }
-
- @Test
- public void extractPlaceholders_validURI_PlaceholdersInMainPartOfURIOnly() {
- List<String> placeholders = URISignatureHandler
- .extractPlaceholders(validURI_PlaceholdersInMainPartOfURIOnly);
- assertNotNull(placeholders);
- assertEquals(2, placeholders.size());
- assertEquals("Wrong first placeholder", "sop_id", placeholders.get(0));
- assertEquals("Wrong second placeholder", "cond_id", placeholders.get(1));
- }
-
- @Test
- public void extractPlaceholders_validURI_PlaceholdersInQueryStringOnly() {
- List<String> placeholders = URISignatureHandler
- .extractPlaceholders(validURI_PlaceholdersInQueryStringOnly);
- assertNotNull(placeholders);
- assertEquals(1, placeholders.size());
- assertEquals("Wrong first placeholder", "user_id", placeholders.get(0));
- }
-
- @Test
- public void extractPlaceholders_validURI_MixedPlaceholders() {
- List<String> placeholders = URISignatureHandler
- .extractPlaceholders(validURI_3MixedPlaceholders);
- assertNotNull(placeholders);
- assertEquals("Wrong number of placeholders extracted", 3, placeholders
- .size());
- assertEquals("Wrong first placeholder", "sop_id", placeholders.get(0));
- assertEquals("Wrong second placeholder", "cond_id", placeholders.get(1));
- assertEquals("Wrong third placeholder", "unit", placeholders.get(2));
- }
-
- @Test
- public void extractPlaceholders_validURI_MultipleQueryString() {
- List<String> placeholders = URISignatureHandler
- .extractPlaceholders(validURI_MultipleQueryString);
- assertNotNull(placeholders);
- assertEquals(5, placeholders.size());
- assertEquals("Wrong first placeholder", "catalogue", placeholders.get(0));
- assertEquals("Wrong second placeholder", "count?", placeholders.get(1));
- assertEquals("Wrong third placeholder", "startPage?", placeholders.get(2));
- assertEquals("Wrong fourth placeholder", "startIndex?", placeholders.get(3));
- assertEquals("Wrong fifth placeholder", "searchTerms?", placeholders.get(4));
- }
-
- // failure cases
-
- /*
- * These tests are all meant to generate an exception - therefore, no need
- * to evaluate generated values, as there will be none returned.
- */
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void extractPlaceholders_badURI_nullURI() {
- URISignatureHandler.extractPlaceholders(badURI_nullURI);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void extractPlaceholders_badURI_emptyURI() {
- URISignatureHandler.extractPlaceholders(badURI_emptyURI);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void extractPlaceholders_badURI_SingleOpeningSymbolNoClosingSymbol() {
- URISignatureHandler
- .extractPlaceholders(badURI_SingleOpeningSymbolNoClosingSymbol);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void extractPlaceholders_badURI_SingleClosingSymbolNoOpeningSymbol() {
- URISignatureHandler
- .extractPlaceholders(badURI_SingleClosingSymbolNoOpeningSymbol);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void extractPlaceholders_badURI_DoubleOpeningSymbolsNoClosingSymbol() {
- URISignatureHandler
- .extractPlaceholders(badURI_DoubleOpeningSymbolsNoClosingSymbol);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void extractPlaceholders_badURI_DoubleOpeningSymbols() {
- URISignatureHandler.extractPlaceholders(badURI_DoubleOpeningSymbols);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void extractPlaceholders_badURI_DoubleOpeningSymbolsSpaced() {
- URISignatureHandler
- .extractPlaceholders(badURI_DoubleOpeningSymbolsSpaced);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void extractPlaceholders_badURI_DoubleClosingSymbols() {
- URISignatureHandler.extractPlaceholders(badURI_DoubleClosingSymbols);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void extractPlaceholders_badURI_DoubleClosingSymbolsSpaced() {
- URISignatureHandler
- .extractPlaceholders(badURI_DoubleClosingSymbolsSpaced);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void extractPlaceholders_badURI_NestedPlaceholders() {
- URISignatureHandler.extractPlaceholders(badURI_NestedPlaceholders);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void extractPlaceholders_badURI_NestedPlaceholdersSpaced() {
- URISignatureHandler
- .extractPlaceholders(badURI_NestedPlaceholdersSpaced);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void extractPlaceholders_badURI_DuplicatePlaceholders() {
- URISignatureHandler.extractPlaceholders(badURI_DuplicatePlaceholders);
- }
-
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void extractPlaceholders_badURI_DuplicatePlaceholdersWithOthers() {
- URISignatureHandler
- .extractPlaceholders(badURI_DuplicatePlaceholdersWithOthers);
- }
-
- // ==========================================================================
- // TEST COMPLETE URI GENERATION FROM URI SIGNATURE + PARAMETERS
- // ==========================================================================
-
- // success cases
-
- @SuppressWarnings("serial")
- @Test
- public void generateCompleteURI_successfulURIGeneration() {
- String uriSignature = "http://sysmo-db.org/sops/{sop_id}/experimental_conditions/{cond_id}?condition_unit={unit}";
- Map<String, String> parameters = new HashMap<String, String>() {
- {
- put("sop_id", "111");
- put("unit", "33");
- put("cond_id", "2222");
- }
- };
-
- String completeURI = URISignatureHandler.generateCompleteURI(
- uriSignature, parameters, true);
- assertEquals(
- "http://sysmo-db.org/sops/111/experimental_conditions/2222?condition_unit=33",
- completeURI);
- }
-
- @SuppressWarnings("serial")
- @Test
- public void generateCompleteURI_successfulURIGeneration_URLParameterEscaping() {
- String uriSignature = "http://sysmo-db.org/sops/{sop_id}/experimental_conditions/{cond_id}?condition_unit={unit}";
- Map<String, String> parameters = new HashMap<String, String>() {
- {
- put("sop_id", "1 11");
- put("unit", "3;3");
- put("cond_id", "2/2$2&2:");
- }
- };
-
- String completeURI = URISignatureHandler.generateCompleteURI(
- uriSignature, parameters, true);
- System.err.println(completeURI);
- assertEquals(
- "http://sysmo-db.org/sops/1%2011/experimental_conditions/2%2F2%242%262%3A?condition_unit=3%3B3",
- completeURI);
- }
-
- @SuppressWarnings("serial")
- @Test
- public void generateCompleteURI_successfulURIGeneration_noURLParameterEscaping() {
- String uriSignature = "http://sysmo-db.org/sops/{sop_id}/experimental_conditions/{cond_id}?condition_unit={unit}";
- Map<String, String> parameters = new HashMap<String, String>() {
- {
- put("sop_id", "1 11");
- put("unit", "3;3");
- put("cond_id", "2/2$2&2:");
- }
- };
-
- String completeURI = URISignatureHandler.generateCompleteURI(
- uriSignature, parameters, false);
- assertEquals(
- "http://sysmo-db.org/sops/1 11/experimental_conditions/2/2$2&2:?condition_unit=3;3",
- completeURI);
- }
-
- @SuppressWarnings("serial")
- @Test
- public void generateCompleteURI_successfulURIGeneration_optionalParams() {
- String uriSignature = "http://dr-site.esrin.esa.int/{catalogue}/genesi/ASA_IMS_1P/rdf/?count={count?}&startPage={startPage?}&startIndex={startIndex?}&q={searchTerms?}";
- Map<String, String> allParameters = new HashMap<String, String>() {
- {
- put("catalogue", "catalogue");
- put("count?", "10");
- put("startPage?", "1");
- put("startIndex?", "1");
- put("searchTerms?", "term1");
- }
- };
-
- Map<String, String> parametersMissingOptional = new HashMap<String, String>() {
- {
- put("catalogue", "catalogue");
- put("count?", "10");
- put("searchTerms?", "term1");
- }
- };
-
- Map<String, String> parametersMissingFirstOptional = new HashMap<String, String>() {
- {
- put("catalogue", "catalogue");
- put("startPage?", "1");
- put("startIndex?", "1");
- put("searchTerms?", "term1");
- }
- };
-
- String completeURI1 = URISignatureHandler.generateCompleteURI(
- uriSignature, allParameters, false);
- assertEquals(
- "http://dr-site.esrin.esa.int/catalogue/genesi/ASA_IMS_1P/rdf/?count=10&startPage=1&startIndex=1&q=term1",
- completeURI1);
-
- String completeURI2 = URISignatureHandler.generateCompleteURI(
- uriSignature, parametersMissingOptional, false);
- assertEquals(
- "http://dr-site.esrin.esa.int/catalogue/genesi/ASA_IMS_1P/rdf/?count=10&q=term1",
- completeURI2);
-
- String completeURI3 = URISignatureHandler.generateCompleteURI(
- uriSignature, parametersMissingFirstOptional, false);
- assertEquals(
- "http://dr-site.esrin.esa.int/catalogue/genesi/ASA_IMS_1P/rdf/?startPage=1&startIndex=1&q=term1",
- completeURI3);
- }
-
- @Test
- public void generateCompleteURI_signatureWithNoPlaceholders_nullParameterMap() {
- String completeURI = URISignatureHandler.generateCompleteURI(
- validURI_NoPlaceholders, null, true);
- assertEquals(validURI_NoPlaceholders, completeURI);
- }
-
- @Test
- public void generateCompleteURI_signatureWithNoPlaceholders_emptyParameterMap() {
- String completeURI = URISignatureHandler.generateCompleteURI(
- validURI_NoPlaceholders, Collections
- .<String, String> emptyMap(), true);
- assertEquals(validURI_NoPlaceholders, completeURI);
- }
-
- // failure cases
-
- @Test(expected = URISignatureHandler.URIGenerationFromSignatureException.class)
- public void generateCompleteURI_signatureWithPlaceholders_nullParameterMap() {
- URISignatureHandler.generateCompleteURI(validURI_3MixedPlaceholders,
- null, true);
- }
-
- @Test(expected = URISignatureHandler.URIGenerationFromSignatureException.class)
- public void generateCompleteURI_signatureWithPlaceholders_emptyParameterMap() {
- URISignatureHandler.generateCompleteURI(validURI_3MixedPlaceholders,
- Collections.<String, String> emptyMap(), true);
- }
-
- @SuppressWarnings("serial")
- @Test
- public void generateCompleteURI_signatureWithPlaceholders_missingParameterURIGeneration_FailureNotExpected() {
- String uriSignature = "http://sysmo-db.org/sops/{sop_id}/experimental_conditions/{cond_id}?condition_unit={unit}";
- Map<String, String> parameters = new HashMap<String, String>() {
- {
- put("sop_id", "111");
- put("cond_id", "2222");
- }
- };
-
- String completeURI = URISignatureHandler.generateCompleteURI(
- uriSignature, parameters, true);
-
- assertEquals(
- "http://sysmo-db.org/sops/111/experimental_conditions/2222",
- completeURI);
- }
-
- // the following failure case is just to confirm the validation of the URI
- // signature by the validation mechanism, which is the same for
- // generateCompleteURI()
- // and extractPlaceholders()
-
- @SuppressWarnings("serial")
- @Test(expected = URISignatureHandler.URISignatureParsingException.class)
- public void generateCompleteURI_duplicatePlaceholderURIGeneration_FailureExpected() {
- String uriSignature = "http://sysmo-db.org/sops/{sop_id}/experimental_conditions/{cond_id}?condition_unit={sop_id}";
- Map<String, String> parameters = new HashMap<String, String>() {
- {
- put("sop_id", "111");
- put("unit", "33");
- put("cond_id", "2222");
- }
- };
-
- String completeURI = URISignatureHandler.generateCompleteURI(
- uriSignature, parameters, true);
-
- assertEquals(
- "http://sysmo-db.org/sops/111/experimental_conditions/2222?condition_unit=33",
- completeURI);
- }
-
- @SuppressWarnings("serial")
- @Test(expected = URISignatureHandler.URIGenerationFromSignatureException.class)
- public void generateCompleteURI_failureURIGeneration_optionalParams() {
- String uriSignature = "http://dr-site.esrin.esa.int/{catalogue}/genesi/ASA_IMS_1P/rdf/?count={count?}&startPage={startPage?}&startIndex={startIndex?}&q={searchTerms?}";
-
- Map<String, String> parametersMissingCompulsory = new HashMap<String, String>() {
- {
- put("count?", "10");
- put("startPage?", "1");
- put("startIndex?", "1");
- put("searchTerms?", "term1");
- }
- };
-
- String completeURI = URISignatureHandler.generateCompleteURI(
- uriSignature, parametersMissingCompulsory, false);
-
- assertEquals(
- "http://dr-site.esrin.esa.int/catalogue/genesi/ASA_IMS_1P/rdf/?count={count?}&startPage={startPage?}&startIndex={startIndex?}&q={searchTerms?}",
- completeURI);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-common-activities/blob/433612be/taverna-rest-activity/src/test/java/org/apache/taverna/activities/rest/ApacheHttpClientUsageTest.java.bak
----------------------------------------------------------------------
diff --git a/taverna-rest-activity/src/test/java/org/apache/taverna/activities/rest/ApacheHttpClientUsageTest.java.bak b/taverna-rest-activity/src/test/java/org/apache/taverna/activities/rest/ApacheHttpClientUsageTest.java.bak
new file mode 100644
index 0000000..c966a57
--- /dev/null
+++ b/taverna-rest-activity/src/test/java/org/apache/taverna/activities/rest/ApacheHttpClientUsageTest.java.bak
@@ -0,0 +1,215 @@
+package org.apache.taverna.activities.rest;
+
+import java.awt.BorderLayout;
+import java.awt.Container;
+import java.awt.Dimension;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+
+import javax.swing.BorderFactory;
+import javax.swing.JButton;
+import javax.swing.JComboBox;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
+import javax.swing.JTextField;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.ExecutionContext;
+import org.apache.http.protocol.HttpContext;
+
+/**
+ *
+ * @author Sergejs Aleksejevs
+ */
+public class ApacheHttpClientUsageTest extends JFrame implements ActionListener
+{
+ private JComboBox cbHttpMethod;
+ private JTextField tfAddressBar;
+ private JButton bGo;
+ private JTextArea taResponse;
+ private JScrollPane spResponse;
+
+
+ /**
+ * Constructor is solely involved in the UI initialisation.
+ */
+ public ApacheHttpClientUsageTest()
+ {
+ Container contentPane = this.getContentPane();
+ contentPane.setLayout(new BorderLayout(5,5));
+
+ bGo = new JButton("GO!");
+ bGo.addActionListener(this);
+ bGo.setDefaultCapable(true);
+ this.getRootPane().setDefaultButton(bGo);
+
+ tfAddressBar = new JTextField(50);
+ tfAddressBar.setPreferredSize(new Dimension(0,bGo.getPreferredSize().height));
+ tfAddressBar.setText("http://test.biocatalogue.org/");
+
+ JPanel jpAddressBar = new JPanel();
+ jpAddressBar.add(tfAddressBar);
+ jpAddressBar.add(bGo);
+
+ cbHttpMethod = new JComboBox(RESTActivity.HTTP_METHOD.values());
+ cbHttpMethod.setBorder(BorderFactory.createCompoundBorder(
+ BorderFactory.createEmptyBorder(5, 5, 5, 5),
+ cbHttpMethod.getBorder()));
+
+ JPanel jpAllNavigation = new JPanel(new BorderLayout());
+ jpAllNavigation.add(jpAddressBar, BorderLayout.NORTH);
+ jpAllNavigation.add(cbHttpMethod, BorderLayout.CENTER);
+
+ contentPane.add(jpAllNavigation, BorderLayout.NORTH);
+
+ taResponse = new JTextArea(20, 20);
+ taResponse.setEditable(true);
+
+ spResponse = new JScrollPane(taResponse);
+ spResponse.setBorder(BorderFactory.createCompoundBorder(
+ BorderFactory.createEmptyBorder(0, 5, 5, 5),
+ BorderFactory.createEtchedBorder()));
+ contentPane.add(spResponse, BorderLayout.CENTER);
+
+ this.pack();
+ this.setLocationRelativeTo(null); // center on screen
+ }
+
+
+ /**
+ * Click handler for the only button there is - the "GO!" button
+ */
+ public void actionPerformed(ActionEvent e)
+ {
+ if (e.getSource().equals(bGo))
+ {
+ try {
+ switch ((RESTActivity.HTTP_METHOD)cbHttpMethod.getSelectedItem()) {
+ case GET: doGET(); break;
+ case POST: doPOST(); break;
+ case PUT: doPUT(); break;
+ case DELETE: doDELETE(); break;
+ }
+ }
+ catch (Exception ex) {
+ taResponse.setText(ex + "\n\n" + ex.getStackTrace());
+ }
+ }
+ }
+
+
+ private void doGET() {
+ HttpGet httpGet = new HttpGet(tfAddressBar.getText());
+ httpGet.addHeader("Accept", "application/xml");
+ performHTTPRequest(httpGet);
+ }
+
+
+ private void doPOST() throws UnsupportedEncodingException {
+// // POST TO MYEXPERIMENT - basic auth
+// HttpPost httpPost = new HttpPost("http://sandbox.myexperiment.org/comment.xml");
+// httpPost.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(("LOGIN" + ":" + "PASSWORD").getBytes())));
+// httpPost.addHeader("Accept", "application/xml");
+// httpPost.addHeader("Content-Type", "application/xml");
+// httpPost.setEntity(new StringEntity("<comment><subject resource=\"http://sandbox.myexperiment.org/files/226\"/><comment>1234567</comment></comment>"));
+// performHTTPRequest(httpPost);
+
+ // POST TO BIOCATALOGUE - no auth
+ HttpPost httpPost = new HttpPost(tfAddressBar.getText());
+ httpPost.addHeader("Accept", "application/xml");
+ httpPost.addHeader("Content-Type", "application/xml");
+ httpPost.setEntity(new StringEntity("<searchByData><searchType>input</searchType><limit>20</limit><data>test</data></searchByData>"));
+ performHTTPRequest(httpPost);
+ }
+
+
+ private void doPUT() throws UnsupportedEncodingException {
+ HttpPut httpPut = new HttpPut("http://sandbox.myexperiment.org/comment.xml?id=251");
+ httpPut.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(("LOGIN" + ":" + "PASSWORD").getBytes())));
+ httpPut.addHeader("Accept", "application/xml");
+ httpPut.addHeader("Content-Type", "application/xml");
+ httpPut.setEntity(new StringEntity("<comment><subject resource=\"http://sandbox.myexperiment.org/files/226\"/><comment>12345678</comment></comment>"));
+ performHTTPRequest(httpPut);
+ }
+
+
+ private void doDELETE() {
+ HttpDelete httpDelete = new HttpDelete("http://sandbox.myexperiment.org/comment.xml?id=251");
+ httpDelete.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(("LOGIN" + ":" + "PASSWORD").getBytes())));
+ httpDelete.addHeader("Accept", "application/xml");
+ performHTTPRequest(httpDelete);
+ }
+
+
+ private void performHTTPRequest(HttpRequestBase httpRequest) {
+ try {
+ StringBuilder responseStr = new StringBuilder();
+ // ---------------------------------------------
+
+ HttpClient httpClient = new DefaultHttpClient();
+ HttpContext localContext = new BasicHttpContext();
+
+ HttpResponse response = httpClient.execute(httpRequest, localContext);
+ // ---
+ // TRACK WHERE THE FINAL REDIRECT ENDS UP - target host + URI
+ HttpHost target = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
+ HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST);
+
+ responseStr.append("Final request URI: " + req.getMethod() + " " + target + req.getURI() + "\n");
+// System.out.println("Target host: " + target);
+// System.out.println("Final request URI: " + req.getURI());
+// System.out.println("Final request method: " + req.getMethod());
+ // ---
+ responseStr.append(response.getStatusLine() + "\n");
+
+ HttpEntity entity = response.getEntity();
+ responseStr.append(entity.getContentType() + "\n\n");
+
+ if (entity != null) {
+ InputStream in = entity.getContent();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+
+ String str;
+ while ((str = reader.readLine()) != null) {
+ responseStr.append(str + "\n");
+ }
+
+ taResponse.setText(responseStr.toString());
+ taResponse.setCaretPosition(0);
+ }
+
+ httpClient.getConnectionManager().shutdown();
+ }
+ catch (Exception ex) {
+ taResponse.setText(ex.getMessage() + "\n\n" + ex.getStackTrace());
+ }
+ }
+
+
+ public static void main(String[] args)
+ {
+ ApacheHttpClientUsageTest frame = new ApacheHttpClientUsageTest();
+ frame.setVisible(true);
+ frame.tfAddressBar.setCaretPosition(frame.tfAddressBar.getText().length());
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-common-activities/blob/433612be/taverna-rest-activity/src/test/java/org/apache/taverna/activities/rest/ExampleActivityTest.java.bak
----------------------------------------------------------------------
diff --git a/taverna-rest-activity/src/test/java/org/apache/taverna/activities/rest/ExampleActivityTest.java.bak b/taverna-rest-activity/src/test/java/org/apache/taverna/activities/rest/ExampleActivityTest.java.bak
new file mode 100644
index 0000000..977db27
--- /dev/null
+++ b/taverna-rest-activity/src/test/java/org/apache/taverna/activities/rest/ExampleActivityTest.java.bak
@@ -0,0 +1,153 @@
+package org.apache.taverna.activities.rest;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.net.URI;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.taverna.activities.testutils.ActivityInvoker;
+import org.apache.taverna.reference.ErrorDocument;
+import org.apache.taverna.reference.ExternalReferenceSPI;
+import org.apache.taverna.workflowmodel.OutputPort;
+import org.apache.taverna.workflowmodel.processor.activity.ActivityConfigurationException;
+import org.apache.taverna.workflowmodel.processor.activity.ActivityInputPort;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class ExampleActivityTest {
+
+ private RESTActivityConfigurationBean configBean;
+
+ private RESTActivity activity = new RESTActivity();
+
+ @Before
+ public void makeConfigBean() throws Exception {
+ configBean = new RESTActivityConfigurationBean();
+ configBean.setExampleString("something");
+ configBean
+ .setExampleUri(URI.create("http://localhost:8080/myEndPoint"));
+ }
+
+ @Test(expected = ActivityConfigurationException.class)
+ public void invalidConfiguration() throws ActivityConfigurationException {
+ RESTActivityConfigurationBean invalidBean = new RESTActivityConfigurationBean();
+ invalidBean.setExampleString("invalidExample");
+ // Should throw ActivityConfigurationException
+ activity.configure(invalidBean);
+ }
+
+ @Test
+ public void executeAsynch() throws Exception {
+ activity.configure(configBean);
+
+ Map<String, Object> inputs = new HashMap<String, Object>();
+ inputs.put("firstInput", "hello");
+
+ Map<String, Class<?>> expectedOutputTypes = new HashMap<String, Class<?>>();
+ expectedOutputTypes.put("simpleOutput", String.class);
+ expectedOutputTypes.put("moreOutputs", ExternalReferenceSPI.class);
+
+ Map<String, Object> outputs = ActivityInvoker.invokeAsyncActivity(
+ activity, inputs, expectedOutputTypes);
+
+ assertEquals("Unexpected outputs", 2, outputs.size());
+ assertEquals("da39a3ee5e6b4b0d3255bfef95601890afd80709", outputs.get("simpleOutput"));
+ ErrorDocument errorDoc = (ErrorDocument) outputs.get("moreOutputs");
+ assertEquals("java.lang.Exception: There are no more values",
+ errorDoc.getExceptionMessage());
+
+ }
+
+
+
+ @Test
+ public void checksumOfExtraData() throws Exception {
+ configBean.setExampleString("specialCase");
+ activity.configure(configBean);
+
+ Map<String, Object> inputs = new HashMap<String, Object>();
+ inputs.put("firstInput", "hello");
+ inputs.put("extraData", Arrays.asList("Test1".getBytes("utf8"),
+ "Test2".getBytes("utf8")));
+
+
+ Map<String, Class<?>> expectedOutputTypes = new HashMap<String, Class<?>>();
+ expectedOutputTypes.put("simpleOutput", String.class);
+ expectedOutputTypes.put("moreOutputs", ExternalReferenceSPI.class);
+ expectedOutputTypes.put("report", String.class);
+
+
+ Map<String, Object> outputs = ActivityInvoker.invokeAsyncActivity(
+ activity, inputs, expectedOutputTypes);
+
+ assertEquals("Unexpected outputs", 3, outputs.size());
+ assertEquals("35bceb434ff8e69fb89b829e461c921a28b423b3", outputs.get("simpleOutput"));
+ ErrorDocument errorDoc = (ErrorDocument) outputs.get("moreOutputs");
+ assertEquals("java.lang.Exception: There are no more values",
+ errorDoc.getExceptionMessage());
+
+ }
+
+
+
+ @Test
+ public void reConfiguredActivity() throws Exception {
+ assertEquals("Unexpected inputs", 0, activity.getInputPorts().size());
+ assertEquals("Unexpected outputs", 0, activity.getOutputPorts().size());
+
+ activity.configure(configBean);
+ assertEquals("Unexpected inputs", 1, activity.getInputPorts().size());
+ assertEquals("Unexpected outputs", 2, activity.getOutputPorts().size());
+
+ activity.configure(configBean);
+ // Should not change on reconfigure
+ assertEquals("Unexpected inputs", 1, activity.getInputPorts().size());
+ assertEquals("Unexpected outputs", 2, activity.getOutputPorts().size());
+ }
+
+ @Test
+ public void reConfiguredSpecialPorts() throws Exception {
+ activity.configure(configBean);
+
+ RESTActivityConfigurationBean specialBean = new RESTActivityConfigurationBean();
+ specialBean.setExampleString("specialCase");
+ specialBean.setExampleUri(URI
+ .create("http://localhost:8080/myEndPoint"));
+ activity.configure(specialBean);
+ // Should now have added the optional ports
+ assertEquals("Unexpected inputs", 2, activity.getInputPorts().size());
+ assertEquals("Unexpected outputs", 3, activity.getOutputPorts().size());
+ }
+
+ @Test
+ public void configureActivity() throws Exception {
+ Set<String> expectedInputs = new HashSet<String>();
+ expectedInputs.add("firstInput");
+
+ Set<String> expectedOutputs = new HashSet<String>();
+ expectedOutputs.add("simpleOutput");
+ expectedOutputs.add("moreOutputs");
+
+ activity.configure(configBean);
+
+ Set<ActivityInputPort> inputPorts = activity.getInputPorts();
+ assertEquals(expectedInputs.size(), inputPorts.size());
+ for (ActivityInputPort inputPort : inputPorts) {
+ assertTrue("Wrong input : " + inputPort.getName(), expectedInputs
+ .remove(inputPort.getName()));
+ }
+
+ Set<OutputPort> outputPorts = activity.getOutputPorts();
+ assertEquals(expectedOutputs.size(), outputPorts.size());
+ for (OutputPort outputPort : outputPorts) {
+ assertTrue("Wrong output : " + outputPort.getName(),
+ expectedOutputs.remove(outputPort.getName()));
+ }
+ }
+}