JAL-1517 fix copyright for 2.8.2
[jalview.git] / src / jalview / ws / rest / params / JobConstant.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ws.rest.params;
22
23 import jalview.ws.params.OptionI;
24 import jalview.ws.rest.InputType;
25 import jalview.ws.rest.NoValidInputDataException;
26 import jalview.ws.rest.RestJob;
27
28 import java.io.UnsupportedEncodingException;
29 import java.net.URLEncoder;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.apache.http.entity.mime.content.ContentBody;
34 import org.apache.http.entity.mime.content.StringBody;
35
36 /**
37  * defines a constant value always provided as a parameter.
38  * 
39  * @author JimP
40  * 
41  */
42 public class JobConstant extends InputType
43 {
44
45   String value;
46
47   /**
48    * 
49    * @param param
50    *          name of parameter
51    * @param val
52    *          value of parameter
53    */
54   public JobConstant(String param, String val)
55   {
56     // needs no data from the restJob
57     super(null);
58     this.token = param;
59     value = val;
60   }
61
62   @Override
63   public ContentBody formatForInput(RestJob rj)
64           throws UnsupportedEncodingException, NoValidInputDataException
65   {
66
67     return new StringBody(value);
68   }
69
70   @Override
71   public List<String> getURLEncodedParameter()
72   {
73     ArrayList<String> prm = new ArrayList<String>();
74
75     if (value != null && value.length() > 0)
76     {
77       try
78       {
79         prm.add(URLEncoder.encode(value, "UTF-8"));
80       } catch (UnsupportedEncodingException ex)
81       {
82         throw new Error("Couldn't encode '" + value + "' as UTF-8.", ex);
83
84       }
85     }
86     return prm;
87   }
88
89   @Override
90   public String getURLtokenPrefix()
91   {
92     return "";
93   }
94
95   @Override
96   public boolean configureFromURLtokenString(List<String> tokenstring,
97           StringBuffer warnings)
98   {
99     if (tokenstring.size() > 1)
100     {
101       warnings.append("IMPLEMENTATION ERROR: Constant POST parameters cannot have more than one value.");
102       return false;
103     }
104     if (tokenstring.size() == 1)
105     {
106       value = tokenstring.get(0);
107     }
108     return true;
109   }
110
111   @Override
112   public boolean configureProperty(String tok, String val,
113           StringBuffer warnings)
114   {
115     warnings.append("IMPLEMENTATION ERROR: No Properties to configure for a Constant parameter.");
116     return false;
117   }
118
119   @Override
120   public List<OptionI> getOptions()
121   {
122     // empty list - this parameter isn't configurable, so don't try.
123     return new ArrayList<OptionI>();
124   }
125 }