[Demonstration 1]

[Buggy Method]
public void deployArtifact(String applicationKey, String stage, String version, String tagName, String deployAction) throws Exception {
    Map<String, String> msgBody = new HashMap<String, String>();
    msgBody.put("action", "deployArtifact");
    msgBody.put("applicationKey", applicationKey);
    msgBody.put("stage", stage);
    msgBody.put("version", version);
    msgBody.put("tagName", tagName);
    msgBody.put("deployAction", deployAction);
    HttpResponse response = doPostRequest(REPOANDBUILD_ADD_ENDPOINT, msgBody);
    if (response.getResponseCode() == HttpStatus.SC_OK) {
        return;
    } else {
        throw new AppFactoryIntegrationTestException("Deploy Artifact failed " + response.getData());
    }
}

[Buggy Hunk]
throw new AppFactoryIntegrationTestException("Deploy Artifact failed " + response.getData());

[Patch]
throw new AFIntegrationTestException("Deploy Artifact failed " + response.getData());

[Demonstration 2]

[Buggy Method]
public static HttpHeaders removeSdchEncoding(HttpHeaders headers) {
    if (headers == null) {
        throw new IllegalArgumentException("Headers is null");
    }
    String ae = headers.get(HttpHeaders.Names.ACCEPT_ENCODING);
    if (StringUtils.isNotBlank(ae)) {
        // The former regex should remove occurrences of 'sdch' while the
        // latter regex should take care of the dangling comma case when
        // 'sdch' was the first element in the list and there are other
        // encodings.
        String noSdch = ae.replaceAll(",? *(sdch|SDCH)", "").replace("^ *, *", "");
        headers.set(HttpHeaders.Names.ACCEPT_ENCODING, noSdch);
        LOG.debug("Removed sdch and inserted: {}", noSdch);
    }
    return headers;
}

[Buggy Hunk]
String noSdch = ae.replaceAll(",? *(sdch|SDCH)", "").replace("^ *, *", "");

[Patch]
String noSdch = ae.replaceAll(",? *(sdch|SDCH)", "").replaceFirst("^ *, *", "");

[Demonstration 3]

[Buggy Method]
private boolean connectToOverDriveAPI(boolean getNewToken) {
    //Check to see if we already have a valid token
    if (overDriveAPIToken != null && getNewToken == false) {
        if (overDriveAPIExpiration - new Date().getTime() > 0) {
            return true;
        }
    }
    //Connect to the API to get our token
    HttpURLConnection conn = null;
    try {
        URL emptyIndexURL = new URL("https://oauth.overdrive.com/token");
        logger.info("OverDrive url is https://oauth.overdrive.com/token");
        conn = (HttpURLConnection) emptyIndexURL.openConnection();
        if (conn instanceof HttpsURLConnection) {
            HttpsURLConnection sslConn = (HttpsURLConnection) conn;
            sslConn.setHostnameVerifier(new HostnameVerifier() {

                @Override
                public boolean verify(String hostname, SSLSession session) {
                    //Do not verify host names
                    return true;
                }
            });
        }
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        String encoded = Base64.encodeBase64String(new String(clientKey + ":" + clientSecret).getBytes());
        conn.setRequestProperty("Authorization", "Basic " + encoded);
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(), "UTF8");
        wr.write("grant_type=client_credentials");
        wr.flush();
        wr.close();
        StringBuffer response = new StringBuffer();
        if (conn.getResponseCode() == 200) {
            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                response.append(line);
            }
            rd.close();
            JSONObject parser = new JSONObject(response.toString());
            overDriveAPIToken = parser.getString("access_token");
            overDriveAPITokenType = parser.getString("token_type");
            overDriveAPIExpiration = new Date().getTime() + (parser.getLong("expires_in") * 1000) - 10000;
            //logger.info("OverDrive token is " + overDriveAPIToken);
        } else {
            logger.error("Received error " + conn.getResponseCode() + " connecting to overdrive API");
            // Lessa : Added the request URL to the error log
            logger.error("Request URL: https://oauth.overdrive.com/token");
            // Get any errors
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                response.append(line);
            }
            //logger.debug("  Finished reading response");
            // Lessa : Added any response found in the Error Stream to the error log
            if (response != null) {
                logger.error("Response (Error Stream): " + response.toString());
            }
            // Lessa : Added any response found in the Input Stream to the error log
            response = new StringBuffer();
            rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = rd.readLine()) != null) {
                response.append(line);
            }
            if (response != null) {
                logger.error("Response (Input Stream): " + response.toString());
            }
            rd.close();
            return false;
        }
    } catch (Exception e) {
        logger.error("Error connecting to overdrive API", e);
        return false;
    }
    return true;
}

[Buggy Hunk]
JSONObject parser = new JSONObject(response.toString());

[Patch]
JSONObject parser = new JSONObject(response.toString().trim());