/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2008-2014 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * OpenNMS(R) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <license@opennms.org>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.netmgt.provision.detector.simple.response;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * <p>HttpStatusResponse class.</p>
 *
 * @author ranger
 * @version $Id: $
 */
public class HttpStatusResponse extends LineOrientedResponse {
    
    private static final Logger LOG = LoggerFactory.getLogger(HttpStatusResponse.class);
    
    private static final Pattern DEFAULT_REGEX = Pattern.compile("([H][T][T][P+]/[1].[0-1]) ([0-6]+) ([a-zA-Z ]+)");

    /**
     * <p>Constructor for HttpStatusResponse.</p>
     *
     * @param response a {@link java.lang.String} object.
     */
    public HttpStatusResponse(final String response) {
        super(response);
        
    }

    /**
     * <p>validateResponse</p>
     *
     * @param pattern a {@link java.lang.String} object.
     * @param url a {@link java.lang.String} object.
     * @param isCheckCode a boolean.
     * @param maxRetCode a int.
     * @return a boolean.
     * @throws java.lang.Exception if any.
     */
    public boolean validateResponse(final String pattern, final String url, final boolean isCheckCode, final int maxRetCode) throws Exception {
        String[] codeArray = Integer.toString(maxRetCode).split("");
        if(codeArray.length < 3) {
            throw new IllegalArgumentException("Maximum HTTP return code is too short, must be at least 3 digits");
        }
        
        final Pattern p;
        
        if (isCheckCode) {
            p = Pattern.compile(String.format("([H][T][T][P+]/[1].[0-1]) ([0-%s][0-2][0-%s]) ([a-zA-Z ]+)", codeArray[1], codeArray[3]));
        } else {
            p = DEFAULT_REGEX;
        }

        final Matcher m = p.matcher(getResponse().trim());
        LOG.info("HTTP status regex: {}\n", p.pattern());
        return m.matches();
    }

}

@@ -134,25 +134,25 @@
 (C) 2008-201
-4
+5
  The OpenNMS
@@ -203,17 +203,17 @@
 1999-201
-4
+5
  The Ope
@@ -1333,312 +1333,70 @@
 n;%0A%0A
-import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A%0A/**%0A * %3Cp%3EHttpStatusResponse class.%3C/p%3E%0A *%0A * @author ranger%0A * @version $Id: $%0A */%0Apublic class HttpStatusResponse extends LineOrientedResponse %7B%0A    %0A    private static final Logger LOG = LoggerFactory.getLogger(HttpStatusResponse.class);%0A    
+public class HttpStatusResponse extends LineOrientedResponse %7B
 %0A   
@@ -1425,23 +1425,29 @@
 Pattern 
-DEFAULT
+HTTP_RESPONSE
 _REGEX =
@@ -1498,11 +1498,20 @@
 (%5B0-
-6%5D+
+9%5D%5B0-9%5D%5B0-9%5D
 ) (%5B
@@ -1529,139 +1529,8 @@
 );%0A%0A
-    /**%0A     * %3Cp%3EConstructor for HttpStatusResponse.%3C/p%3E%0A     *%0A     * @param response a %7B@link java.lang.String%7D object.%0A     */%0A
     
@@ -1605,25 +1605,16 @@
 ponse);%0A
-        %0A
     %7D%0A%0A 
@@ -1650,266 +1650,311 @@
 onse
-%3C/p%3E%0A     *%0A     * @param pattern a %7B@link java.lang.String%7D object.%0A     * @param url a %7B@link java.lang.String%7D object.%0A     * @param isCheckCode a boolean.%0A     * @param maxRetCode a int.%0A     * @return a boolean.%0A     * @throws java.lang.Exception if 
+: Validate the HTTP response from %7B@link #getResponse()%7D.%3C/p%3E%0A     *%0A     * @param pattern Unused.%0A     * @param url Unused.%0A     * @param isCheckCode Whether to check the response code against maxRetCode for validity.%0A     * @param maxRetCode The maximum return code to accept.%0A     * @return a boole
 an
-y
 .%0A  
@@ -2115,229 +2115,134 @@
     
-String%5B%5D codeArray = Integer.toString(maxRetCode).split(%22%22);%0A        if(codeArray.length %3C 3) %7B%0A            throw new IllegalArgumentException(%22Maximum HTTP return code is too short, must be at least 3 digits%22);%0A        %7D
+final Matcher m = HTTP_RESPONSE_REGEX.matcher(getResponse().trim());%0A        if (m.matches()) %7B%0A            if (isCheckCode) %7B
 %0A   
@@ -2238,33 +2238,32 @@
 Code) %7B%0A        
-%0A
         final Pa
@@ -2264,193 +2264,120 @@
 nal 
-Pattern p;%0A        %0A        if (isCheckCode) %7B%0A            p = Pattern.compile(String.format(%22(%5BH%5D%5BT%5D%5BT%5D%5BP+%5D/%5B1%5D.%5B0-1%5D) (%5B0-%25s%5D%5B0-2%5D%5B0-%25s%5D) (%5Ba-zA-Z %5D+)%22, codeArray%5B1%5D, codeArray%5B3%5D));%0A
+int returnCode = Integer.valueOf(m.group(2)).intValue();%0A                return (returnCode %3C= maxRetCode);%0A    
     
@@ -2405,25 +2405,23 @@
     
-p = DEFAULT_REGEX
+    return true
 ;%0A  
@@ -2430,126 +2430,55 @@
     
-%7D%0A%0A
    
+ %7D%0A
      
-final Matcher m = p.matcher(getResponse().trim());%0A        LOG.info(%22HTTP status regex: %7B%7D%5Cn%22, p.pattern())
+   %7D else %7B%0A            return false
 ;%0A  
@@ -2486,28 +2486,10 @@
     
- 
-return m.matches();
+ %7D
 %0A   

fix HTTP response validation under Java 8