/*
 * Zed Attack Proxy (ZAP) and its related class files.
 * 
 * ZAP is an HTTP/HTTPS proxy for assessing web application security.
 * 
 * Copyright 2015 The ZAP Development Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zaproxy.zap.extension.fuzz.httpfuzzer;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.core.scanner.Alert;
import org.parosproxy.paros.db.DatabaseException;
import org.parosproxy.paros.model.HistoryReference;
import org.parosproxy.paros.model.Model;
import org.parosproxy.paros.network.HttpMalformedHeaderException;
import org.parosproxy.paros.network.HttpMessage;
import org.zaproxy.zap.extension.alert.ExtensionAlert;

public class HttpFuzzerTaskProcessorUtils {

    private static final Logger LOGGER = Logger.getLogger(HttpFuzzerTaskProcessorUtils.class);

    private static final int PLUGIN_ID = 50002;

    private final HttpFuzzer httpFuzzer;
    private final HttpMessage originalMessage;
    private final long taskId;
    private final List<Object> payloads;
    private final HttpFuzzResult fuzzResult;
    private final ExtensionAlert extensionAlert;
    private HistoryReference historyReference;
    private String processorName;

    protected HttpFuzzerTaskProcessorUtils(
            HttpFuzzer httpFuzzer,
            HttpMessage originalMessage,
            long taskId,
            List<Object> payloads) {
        this(httpFuzzer, originalMessage, taskId, payloads, null, null);
    }

    protected HttpFuzzerTaskProcessorUtils(
            HttpFuzzer httpFuzzer,
            HttpMessage originalMessage,
            long taskId,
            List<Object> payloads,
            HttpFuzzResult fuzzResult,
            ExtensionAlert extensionAlert) {
        this.httpFuzzer = httpFuzzer;
        this.originalMessage = originalMessage;
        this.taskId = taskId;
        this.payloads = payloads;
        this.fuzzResult = fuzzResult;
        this.extensionAlert = extensionAlert;
    }

    protected void setCurrentProcessorName(String name) {
        processorName = name;
    }

    public HttpMessage getOriginalMessage() {
        return originalMessage;
    }

    public void raiseAlert(int risk, int confidence, String name, String description) {
        raiseAlert(risk, confidence, name, description, null, null, null, null, null, null, 0, 0);
    }

    public void raiseAlert(
            int risk,
            int confidence,
            String name,
            String description,
            String param,
            String attack,
            String otherInfo,
            String solution,
            String reference,
            String evidence,
            int cweId,
            int wascId) {
        if (fuzzResult == null) {
            return;
        }

        if (extensionAlert != null) {
            Alert alert = new Alert(PLUGIN_ID, risk, confidence, name);
            String uri = fuzzResult.getHttpMessage().getRequestHeader().getURI().toString();

            alert.setDetail(
                    description,
                    uri,
                    param,
                    attack,
                    otherInfo,
                    solution,
                    reference,
                    evidence,
                    cweId,
                    wascId,
                    fuzzResult.getHttpMessage());
            if (historyReference == null) {
                try {
                    historyReference = new HistoryReference(
                            Model.getSingleton().getSession(),
                            HistoryReference.TYPE_FUZZER,
                            fuzzResult.getHttpMessage());
                } catch (HttpMalformedHeaderException | DatabaseException e) {
                    httpFuzzer.increaseErrorCount(
                            taskId,
                            processorName,
                            Constant.messages.getString("fuzz.httpfuzzer.processor.scriptProcessor.error.persistMessageForAlert"));
                    LOGGER.warn("Failed to persist the message and raise the alert:", e);
                    return;
                }
            }
            fuzzResult.getHttpMessage().setHistoryRef(historyReference);
            extensionAlert.alertFound(alert, historyReference);
        }
    }

    public void stopFuzzer() {
        httpFuzzer.stopScan();
    }

    public List<Object> getPaylaods() {
        return payloads;
    }

    public void increaseErrorCount(String reason) {
        httpFuzzer.increaseErrorCount(taskId, processorName, reason);
    }

    public boolean sendMessage(HttpMessage message) {
        return sendMessage(message, true);
    }

    public boolean sendMessage(HttpMessage message, boolean followRedirects) {
        try {
            httpFuzzer.getHttpSender().sendAndReceive(message, followRedirects);
            httpFuzzer.messageSent(taskId, message);
            return true;
        } catch (IOException e) {
            LOGGER.warn("Failed to send a message:", e);
            return false;
        }
    }

    public void addMessageToResults(String type, HttpMessage httpMessage) {
        addMessageToResults(type, httpMessage, null, null);
    }

    public void addMessageToResults(String type, HttpMessage httpMessage, String customStateKey, Object customState) {
        HttpFuzzResult result = new HttpFuzzResult(taskId, type, httpMessage);
        if (customStateKey != null) {
            result.addCustomState(customStateKey, customState);
        }
        httpFuzzer.fuzzResultAvailable(result);
    }

}

@@ -4943,32 +4943,232 @@
 pScan();%0A    %7D%0A%0A
+    public List%3CObject%3E getPayloads() %7B%0A        return payloads;%0A    %7D%0A%0A    /**%0A     * @deprecated (2.0.0) Use %7B@link #getPayloads()%7D instead.%0A     */%0A    @Deprecated%0A    @SuppressWarnings(%22javadoc%22)%0A
     public List%3C

fix typo in method name of HttpFuzzerTaskProcessorUtils