|
| 1 | +/* |
| 2 | + * Zed Attack Proxy (ZAP) and its related class files. |
| 3 | + * |
| 4 | + * ZAP is an HTTP/HTTPS proxy for assessing web application security. |
| 5 | + * |
| 6 | + * Copyright 2017 The ZAP Development Team |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | +package org.zaproxy.clientapi.ant; |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.OutputStream; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Locale; |
| 29 | + |
| 30 | +import org.apache.tools.ant.BuildException; |
| 31 | + |
| 32 | +public class ReportTask extends ZapTask { |
| 33 | + |
| 34 | + private static final String HTML_REPORT_TYPE = "html"; |
| 35 | + private static final String MD_REPORT_TYPE = "md"; |
| 36 | + private static final String XML_REPORT_TYPE = "xml"; |
| 37 | + private static final String DEFAULT_REPORT_TYPE = HTML_REPORT_TYPE; |
| 38 | + |
| 39 | + private static final List<String> SUPPORTED_REPORT_TYPES = Arrays.asList(HTML_REPORT_TYPE, MD_REPORT_TYPE, XML_REPORT_TYPE); |
| 40 | + |
| 41 | + private String type = DEFAULT_REPORT_TYPE; |
| 42 | + private File file; |
| 43 | + private boolean overwrite; |
| 44 | + |
| 45 | + public void setType(String type) { |
| 46 | + this.type = type; |
| 47 | + } |
| 48 | + |
| 49 | + public void setFile(String file) { |
| 50 | + this.file = new File(file); |
| 51 | + if (!this.file.isAbsolute()) { |
| 52 | + this.file = new File(getProject().getBaseDir(), file); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public void setOverwrite(boolean overwrite) { |
| 57 | + this.overwrite = overwrite; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void execute() throws BuildException { |
| 62 | + validateTaskAttributes(); |
| 63 | + |
| 64 | + byte[] reportContents; |
| 65 | + try { |
| 66 | + switch (type.toLowerCase()) { |
| 67 | + case MD_REPORT_TYPE: |
| 68 | + reportContents = this.getClientApi().core.mdreport(); |
| 69 | + break; |
| 70 | + case XML_REPORT_TYPE: |
| 71 | + reportContents = this.getClientApi().core.xmlreport(); |
| 72 | + break; |
| 73 | + case HTML_REPORT_TYPE: |
| 74 | + default: |
| 75 | + reportContents = this.getClientApi().core.htmlreport(); |
| 76 | + break; |
| 77 | + } |
| 78 | + } catch (Exception e) { |
| 79 | + throw new BuildException("Failed to obtain the report from ZAP: " + e.getMessage(), e); |
| 80 | + } |
| 81 | + |
| 82 | + try (OutputStream os = Files.newOutputStream(file.toPath())) { |
| 83 | + os.write(reportContents); |
| 84 | + } catch (IOException e) { |
| 85 | + throw new BuildException("Failed to save the report: " + e.getMessage(), e); |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + private void validateTaskAttributes() { |
| 91 | + if (type == null || type.isEmpty()) { |
| 92 | + type = DEFAULT_REPORT_TYPE; |
| 93 | + } else { |
| 94 | + type = type.toLowerCase(Locale.ROOT); |
| 95 | + if (!SUPPORTED_REPORT_TYPES.contains(type)) { |
| 96 | + throw new BuildException("Unknown report type [" + type + "], supported types: " + SUPPORTED_REPORT_TYPES); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if (file == null) { |
| 101 | + throw new BuildException("The 'file' attribute must be provided."); |
| 102 | + } |
| 103 | + |
| 104 | + if (file.isFile()) { |
| 105 | + if (!file.canWrite()) { |
| 106 | + throw new BuildException("The provided file is not writable: " + file.getAbsolutePath()); |
| 107 | + } |
| 108 | + |
| 109 | + if (!overwrite) { |
| 110 | + throw new BuildException( |
| 111 | + "The file already exists but 'overwrite' attribute is not set to 'true': " + file.getAbsolutePath()); |
| 112 | + } |
| 113 | + } else if (!file.exists()) { |
| 114 | + File dir = file.getParentFile(); |
| 115 | + if (dir == null) { |
| 116 | + throw new BuildException( |
| 117 | + "Unable to determine parent directory of the file provided: " + file.getAbsolutePath()); |
| 118 | + } |
| 119 | + |
| 120 | + dir.mkdirs(); |
| 121 | + if (!dir.canWrite()) { |
| 122 | + throw new BuildException("The provided directory does not exist or is not writable: " + dir.getAbsolutePath()); |
| 123 | + } |
| 124 | + } else { |
| 125 | + throw new BuildException("The 'file' attribute does not specify a file: " + file.getAbsolutePath()); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments