001/**
002 * Copyright 2014 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package core.tut.pori.http.parameters;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.util.List;
021
022import org.apache.commons.io.IOUtils;
023import org.apache.commons.lang3.StringUtils;
024import org.apache.log4j.Logger;
025
026/**
027 * Converts parameter value String to boolean performing a case insensitive comparison, 
028 * values "no", "0" and "false", empty String and null are interpreted as false, everything else as true
029 *
030 */
031public class BooleanParameter extends HTTPParameter{
032  private static final Logger LOGGER = Logger.getLogger(BooleanParameter.class);
033  private static String FALSE_NO = "no";
034  private static String FALSE_0 = "0";
035  private static String FALSE_FALSE = "false";
036  private boolean[] _values = null;
037
038  @Override
039  public void initialize(List<String> parameterValues) throws IllegalArgumentException {
040    int count = parameterValues.size();
041    _values = new boolean[count];
042    for(int i=0;i<count;++i){
043      _values[i] = toBoolean(parameterValues.get(i));
044    }
045  }
046
047  /**
048   * 
049   * @return the values of this parameter or null if none
050   */
051  public boolean[] getValues(){
052    return _values;
053  }
054  
055  /**
056   * 
057   * @return true if true value was given to this parameter, otherwise false
058   */
059  @Override
060  public Boolean getValue(){
061    return (hasValues() ? _values[0] : null);
062  }
063
064  @Override
065  public boolean hasValues() {
066    return (_values != null);
067  }
068
069  @Override
070  public void initialize(String parameterValue) throws IllegalArgumentException {
071    _values = new boolean[]{toBoolean(parameterValue)};
072  }
073  
074  /**
075   * 
076   * @return true if the value is true, false if value is value or does not exist
077   */
078  public boolean isTrue(){
079    return (hasValues() ? _values[0] : false);
080  }
081  
082  /**
083   * 
084   * @param s
085   * @return the string converted to boolean
086   */
087  public static boolean toBoolean(String s){
088    if(StringUtils.isBlank(s) || FALSE_0.equalsIgnoreCase(s) || FALSE_FALSE.equalsIgnoreCase(s) || FALSE_NO.equalsIgnoreCase(s)){
089      return false;
090    }else{
091      return true;
092    }
093  }
094  
095  @Override
096  public void initialize(InputStream stream) throws IllegalArgumentException {
097    try {
098      initialize(IOUtils.toString(stream));
099    } catch (IOException ex) {
100      LOGGER.error(ex, ex);
101      throw new IllegalArgumentException("Failed to read HTTP body.");
102    }
103  }
104}