securitytools.nessus.http.request.CreateScanRequest.java Source code

Java tutorial

Introduction

Here is the source code for securitytools.nessus.http.request.CreateScanRequest.java

Source

/*
 * The MIT License
 *
 * Copyright 2014 Security Tools SDK for Java.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package securitytools.nessus.http.request;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import securitytools.nessus.http.SequenceUtils;

/**
 * 
 *
 * @author Adam Parsons
 * @version 0.0.1, 05/26/14
 * @since 1.0.0
 */
public class CreateScanRequest extends HttpPost {

    public CreateScanRequest(String scanName, int policyId, String target) {
        this(scanName, policyId, new String[] { target }, SequenceUtils.getNextSequenceId());
    }

    public CreateScanRequest(String scanName, int policyId, String[] targets) {
        this(scanName, policyId, targets, SequenceUtils.getNextSequenceId());
    }

    /**
     *
     * @param scanName A name for the scan job.
     * @param policyId Numeric identifier of the policy to use for the scan.
     * @param targets
     * @param sequenceId A unique number that will be echoed back to identify
     * this response.
     */
    public CreateScanRequest(String scanName, int policyId, String[] targets, long sequenceId) {
        super("/scan/new");

        if (scanName == null || scanName.isEmpty()) {
            throw new IllegalArgumentException("\"scanName\" argument cannot be null or empty.");
        }
        if (policyId < 0) {
            throw new IllegalArgumentException("\"policyId\" argument cannot be less than zero.");
        }
        if (targets == null || targets.length == 0) {
            throw new IllegalArgumentException("\"targets\" argument cannot be null or empty.");
        }
        if (sequenceId < 0) {
            throw new IllegalArgumentException("\"sequenceId\" argument cannot be less than zero.");
        }

        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("scan_name", scanName));
        formparams.add(new BasicNameValuePair("policy_id", String.valueOf(policyId)));

        // Construct comma-separated list of targets
        StringBuilder sb = new StringBuilder();
        for (String target : targets) {
            if (target == null || target.isEmpty()) {
                throw new IllegalArgumentException("\"targets\" argument cannot contain null or empty values.");
            }
            sb.append(target);
            sb.append(",");
        }
        formparams.add(new BasicNameValuePair("target", sb.substring(0, sb.length() - 1)));

        formparams.add(new BasicNameValuePair("seq", String.valueOf(sequenceId)));

        setEntity(new UrlEncodedFormEntity(formparams, Consts.UTF_8));
    }

}