import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.StringBody;
{
return (inputData==null || inputData.size()==0);
}
+ /**
+ * return a url encoded version of this parameter's value, or an empty string if the parameter has no ='value' content.
+ * @return
+ */
+ public abstract List<String> getURLEncodedParameter();
+
+ /**
+ * set the property known as tok, possibly by assigning it with a given val
+ * @param tok
+ * @param val (may be empty or null)
+ * @param warnings place where parse warnings are reported
+ * @return true if property was set
+ */
+ public abstract boolean configureProperty(String tok, String val, StringBuffer warnings);
+
+ /**
+ * Get unique key for this type of parameter in a URL encoding.
+ * @return the string that prefixes an input parameter of InputType<T> type in the string returned from getURLEncodedParameter
+ */
+ public abstract String getURLtokenPrefix();
+ /**
+ * parse the given token String and set InputParameter properties appropriately
+ * @param tokenstring - urlencoded parameter string as returned from getURLEncodedParameter
+ * @param warnings - place where any warning messages about bad property values are written
+ * @return true if configuration succeeded, false otherwise.
+ */
+ public boolean configureFromURLtokenString(List<String> tokenstring, StringBuffer warnings) {
+ boolean valid=true;
+ for (String tok:tokenstring)
+ {
+ Matcher mtch = Pattern.compile("^([^=]+)=?'?([^']*)?'?").matcher(tok);
+ if (mtch.find()) {
+ try {
+ if (mtch.group(1).equals("min"))
+ {
+ min = Integer.parseInt(mtch.group(2));
+ continue;
+
+ } else
+ if (mtch.group(1).equals("max"))
+ {
+ max = Integer.parseInt(mtch.group(2));
+ continue;
+ }
+ }
+ catch (NumberFormatException x)
+ {
+ valid=false;
+ warnings.append("Invalid value for parameter "+mtch.group(1).toLowerCase()+" '"+mtch.group(2)+"' (expected an integer)\n");
+ }
+
+ valid = valid && configureProperty(mtch.group(1), mtch.group(2), warnings);
+ }
+ }
+ return valid;
+ }
+ public void addBaseParams(ArrayList<String> prms)
+ {
+ // todo : check if replaceids should be a global for the service, rather than for a specific parameter.
+ if (min!=1) {
+ prms.add("min='"+min+"'");
+ }
+ if (max!=0) {
+ prms.add("min='"+max+"'");
+ }
+ }
}
\ No newline at end of file
import jalview.ws.rest.NoValidInputDataException;
import jalview.ws.rest.RestJob;
import jalview.ws.rest.InputType.molType;
+import jalview.ws.rest.RestServiceDescription;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
/**
* format an alignment for input to rest service.
+ *
* @author JimP
- *
+ *
*/
-public class Alignment extends InputType {
+public class Alignment extends InputType
+{
public Alignment()
{
- super(new Class[] { AlignmentI.class} );
+ super(new Class[]
+ { AlignmentI.class });
}
- String format="FASTA";
+ String format = "FASTA";
+
molType type;
- boolean jvsuffix=false;
+
+ boolean jvsuffix = false;
+
/**
* input data as a file upload rather than inline content
*/
public boolean writeAsFile;
+
@Override
- public ContentBody formatForInput(RestJob rj) throws UnsupportedEncodingException, NoValidInputDataException
+ public ContentBody formatForInput(RestJob rj)
+ throws UnsupportedEncodingException, NoValidInputDataException
{
- AlignmentI alignment = rj.getAlignmentForInput(token,type);
+ AlignmentI alignment = rj.getAlignmentForInput(token, type);
if (writeAsFile)
{
- try {
- File fa = File.createTempFile("jvmime", ".fa");
- PrintWriter pw = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(fa)), "UTF-8"));
- pw.append(new jalview.io.FormatAdapter().formatSequences(format, alignment, jvsuffix));
- pw.close();
- return new FileBody(fa, "text/plain");
- } catch (Exception ex)
- {
- throw new NoValidInputDataException("Couldn't write out alignment to file.",ex);
+ try
+ {
+ File fa = File.createTempFile("jvmime", ".fa");
+ PrintWriter pw = new PrintWriter(
+ new OutputStreamWriter(new BufferedOutputStream(
+ new FileOutputStream(fa)), "UTF-8"));
+ pw.append(new jalview.io.FormatAdapter().formatSequences(format,
+ alignment, jvsuffix));
+ pw.close();
+ return new FileBody(fa, "text/plain");
+ } catch (Exception ex)
+ {
+ throw new NoValidInputDataException(
+ "Couldn't write out alignment to file.", ex);
+ }
}
- } else {
+ else
+ {
jalview.io.FormatAdapter fa = new jalview.io.FormatAdapter();
fa.setNewlineString("\r\n");
- return new StringBody((fa.formatSequences(format, alignment, jvsuffix)));
- //,
- //"text/plain",Charset.forName("UTF-8"));
+ return new StringBody(
+ (fa.formatSequences(format, alignment, jvsuffix)));
+ // ,
+ // "text/plain",Charset.forName("UTF-8"));
// , "text/plain", Charset.forName("UTF-8"));
- // sb.getContentTypeParameters().put("filename", "alignment.fa");
+ // sb.getContentTypeParameters().put("filename", "alignment.fa");
+ }
+ }
+
+ @Override
+ public List<String> getURLEncodedParameter()
+ {
+ ArrayList<String> prms = new ArrayList<String>();
+ prms.add("format='" + format + "'");
+ if (type != null)
+ {
+ prms.add("type='" + type.toString() + "'");
+ }
+ if (jvsuffix)
+ {
+ prms.add("jvsuffix");
+ }
+ ;
+ if (writeAsFile)
+ {
+ prms.add("writeasfile");
+ }
+ ;
+ return prms;
+ }
+
+ @Override
+ public String getURLtokenPrefix()
+ {
+ return "ALIGNMENT";
+ }
+
+ @Override
+ public boolean configureProperty(String tok, String val,
+ StringBuffer warnings)
+ {
+ if (tok.startsWith("jvsuffix"))
+ {
+ jvsuffix = true;
+ return true;
+ }
+ if (tok.startsWith("writeasfile"))
+ {
+ writeAsFile = true;
+ return true;
+ }
+
+ if (tok.startsWith("format"))
+ {
+ for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
+ {
+ if (val.equalsIgnoreCase(fmt))
+ {
+ format = fmt;
+ return true;
+ }
+ }
+ warnings.append("Invalid alignment format '" + val
+ + "'. Must be one of (");
+ for (String fmt : jalview.io.FormatAdapter.WRITEABLE_FORMATS)
+ {
+ warnings.append(" " + fmt);
+ }
+ warnings.append(")\n");
+ }
+ if (tok.startsWith("type"))
+ {
+ try
+ {
+ type = molType.valueOf(val);
+ return true;
+ } catch (Exception x)
+ {
+ warnings.append("Invalid molecule type '" + val
+ + "'. Must be one of (");
+ for (molType v : molType.values())
+ {
+ warnings.append(" " + v);
+ }
+ warnings.append(")\n");
+ }
}
+ return false;
}
}
\ No newline at end of file
import jalview.ws.rest.InputType;
import jalview.ws.rest.NoValidInputDataException;
import jalview.ws.rest.RestJob;
+import jalview.ws.rest.RestServiceDescription;
import jalview.ws.rest.InputType.molType;
import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.StringBody;
return new StringBody(new jalview.io.AnnotationFile().printCSVAnnotations(al.getAlignmentAnnotation()));
}
}
-}
\ No newline at end of file
+ @Override
+ public List<String> getURLEncodedParameter()
+ {
+ ArrayList<String> prms = new ArrayList<String>();
+ super.addBaseParams(prms);
+ prms.add("format='"+format+"'");
+ return prms;
+ }
+ @Override
+ public String getURLtokenPrefix()
+ {
+ return "ALANNOTATION";
+ }
+ @Override
+ public boolean configureProperty(String tok, String val,
+ StringBuffer warnings)
+ {
+
+ if (tok.startsWith("format"))
+ {
+ for (String fmt : new String[] { CSVANNOT, JVANNOT})
+ {
+ if (val.equalsIgnoreCase(fmt))
+ {
+ format = fmt;
+ return true;
+ }
+ }
+ warnings.append("Invalid annotation file format '" + val
+ + "'. Must be one of (");
+ for (String fmt : new String[] { CSVANNOT, JVANNOT})
+ {
+ warnings.append(" " + fmt);
+ }
+ warnings.append(")\n");
+ }
+ return false;
+ }
+}
import jalview.ws.rest.RestJob;
import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.StringBody;
return new StringBody(value);
}
+ @Override
+ public List<String> getURLEncodedParameter()
+ {
+ ArrayList<String> prm = new ArrayList<String>();
+
+ if (value!=null && value.length()>0)
+ {
+ prm.add(value);
+ }
+ return prm;
+ }
+
+ @Override
+ public String getURLtokenPrefix()
+ {
+ return "";
+ }
+
+ @Override
+ public boolean configureFromURLtokenString(List<String> tokenstring,
+ StringBuffer warnings)
+ {
+ if (tokenstring.size()>1) {
+ warnings.append("IMPLEMENTATION ERROR: Constant POST parameters cannot have more than one value.");
+ return false;
+ }
+ if (tokenstring.size()==1) {
+ value = tokenstring.get(0);
+ }
+ return true;
+ }
+
+ @Override
+ public boolean configureProperty(String tok, String val,
+ StringBuffer warnings)
+ {
+ warnings.append("IMPLEMENTATION ERROR: No Properties to configure for a Constant parameter.");
+ return false;
+ }
}
import jalview.ws.rest.InputType;
import jalview.ws.rest.NoValidInputDataException;
import jalview.ws.rest.RestJob;
+import jalview.ws.rest.RestServiceDescription;
import jalview.ws.rest.InputType.molType;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
+import java.util.List;
import java.util.Vector;
import org.apache.http.entity.mime.content.ContentBody;
minsize=1;
}
}
-}
\ No newline at end of file
+ @Override
+ public List<String> getURLEncodedParameter()
+ {
+ ArrayList<String> prms = new ArrayList<String>();
+ super.addBaseParams(prms);
+ prms.add("minsize='"+ minsize+"'");
+ prms.add("sep='"+ sep+"'");
+ if (type!=null)
+ {
+ prms.add("type='"+type+"'");
+ }
+ return prms;
+ }
+
+ @Override
+ public String getURLtokenPrefix()
+ {
+ return "PARTITION";
+ }
+
+ @Override
+ public boolean configureProperty(String tok, String val,
+ StringBuffer warnings)
+ {
+
+ if (tok.startsWith("sep"))
+ {
+ sep=val;
+ return true;
+ }
+ if (tok.startsWith("minsize"))
+ {
+ try {
+ minsize=Integer.valueOf(val);
+ if (minsize>=0)
+ return true;
+ } catch (Exception x)
+ {
+
+ }
+ warnings.append("Invalid minsize value '"+val+"'. Must be a positive integer.\n");
+ }
+ if (tok.startsWith("type"))
+ {
+ try {
+ type=molType.valueOf(val);
+ return true;
+ } catch (Exception x)
+ {
+ warnings.append("Invalid molecule type '"+val+"'. Must be one of (");
+ for (molType v:molType.values())
+ {
+ warnings.append(" "+v);
+ }
+ warnings.append(")\n");
+ }
+ }
+ return false;
+ }
+
+}
import jalview.ws.rest.InputType;
import jalview.ws.rest.NoValidInputDataException;
import jalview.ws.rest.RestJob;
+import jalview.ws.rest.RestServiceDescription;
import jalview.ws.rest.InputType.molType;
import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.StringBody;
* @author JimP
*
*/
-class SeqIdVector extends InputType {
+public class SeqIdVector extends InputType {
public SeqIdVector()
{
super(new Class[] { AlignmentI.class} );
idvector.append(seq.getName());
}
return new StringBody(idvector.toString());
+ }
+ @Override
+ public List<String> getURLEncodedParameter()
+ {
+ ArrayList<String> prms = new ArrayList<String>();
+ super.addBaseParams(prms);
+ prms.add("sep='"+ sep+"'");
+ prms.add("type='"+type+"'");
+ return prms;
+ }
+ @Override
+ public String getURLtokenPrefix()
+ {
+ return "SEQIDS";
+ }
+ @Override
+ public boolean configureProperty(String tok, String val, StringBuffer warnings)
+ {
+ if (tok.startsWith("sep"))
+ {
+ sep=val;
+ return true;
+ }
+ if (tok.startsWith("type"))
+ {
+ try {
+ type=molType.valueOf(val);
+ return true;
+ } catch (Exception x)
+ {
+ warnings.append("Invalid molecule type '"+val+"'. Must be one of (");
+ for (molType v:molType.values())
+ {
+ warnings.append(" "+v);
+ }
+ warnings.append(")\n");
+ }
+ }
+ return false;
}
-}
\ No newline at end of file
+}
import jalview.ws.rest.InputType;
import jalview.ws.rest.NoValidInputDataException;
import jalview.ws.rest.RestJob;
+import jalview.ws.rest.RestServiceDescription;
import jalview.ws.rest.InputType.molType;
import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.StringBody;
* @author JimP
*
*/
-class SeqVector extends InputType {
+public class SeqVector extends InputType {
String sep;
molType type;
public SeqVector()
}
return new StringBody(idvector.toString());
}
+ @Override
+ public List<String> getURLEncodedParameter()
+ {
+ ArrayList<String> prms = new ArrayList<String>();
+ super.addBaseParams(prms);
+ prms.add("sep='"+ sep+"'");
+ prms.add("type='"+type+"'");
+ return prms;
+ }
+
+ @Override
+ public String getURLtokenPrefix()
+ {
+ return "SEQS";
+ }
+
+ @Override
+ public boolean configureProperty(String tok, String val,
+ StringBuffer warnings)
+ {
+
+ if (tok.startsWith("sep"))
+ {
+ sep=val;
+ return true;
+ }
+ if (tok.startsWith("type"))
+ {
+ try {
+ type=molType.valueOf(val);
+ return true;
+ } catch (Exception x)
+ {
+ warnings.append("Invalid molecule type '"+val+"'. Must be one of (");
+ for (molType v:molType.values())
+ {
+ warnings.append(" "+v);
+ }
+ warnings.append(")\n");
+ }
+ }
+ return false;
+ }
+
}
\ No newline at end of file
import jalview.datamodel.AlignmentI;
import jalview.ws.rest.InputType;
import jalview.ws.rest.RestJob;
+import jalview.ws.rest.RestServiceDescription;
+import jalview.ws.rest.InputType.molType;
import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.http.entity.mime.content.ContentBody;
* @author JimP
*
*/
-class Tree extends InputType {
+public class Tree extends InputType {
public Tree()
{
super(new Class[] { jalview.analysis.NJTree.class} );
throw new Error("Tree InputType not yet implemented");
//return null;
}
+ public String getURLtokenPrefix()
+ {
+ return "NEWICK";
+ }
+ @Override
+ public List<String> getURLEncodedParameter()
+ {
+ ArrayList<String> prms = new ArrayList<String>();
+ super.addBaseParams(prms);
+ return prms;
+ }
+
+ @Override
+ public boolean configureProperty(String tok, String val,
+ StringBuffer warnings)
+ {
+ return true;
+ }
+
}
\ No newline at end of file