1 /*
2  * Copyright 2008-2019 by Emeric Vernat
3  *
4  *     This file is part of Java Melody.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.bull.javamelody.internal.web.pdf;
19
20 import java.text.DateFormat;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24
25 import com.lowagie.text.Document;
26 import com.lowagie.text.DocumentException;
27 import com.lowagie.text.Element;
28 import com.lowagie.text.Font;
29 import com.lowagie.text.Phrase;
30
31 import net.bull.javamelody.internal.common.I18N;
32 import net.bull.javamelody.internal.model.Counter;
33 import net.bull.javamelody.internal.model.CounterError;
34 import net.bull.javamelody.internal.web.html.HtmlCounterErrorReport;
35
36 /**
37  * Partie du rapport pdf pour les erreurs http et dans les logs.
38  * @author Emeric Vernat
39  */

40 class PdfCounterErrorReport extends PdfAbstractTableReport {
41     private final Counter counter;
42     private final DateFormat dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT,
43             DateFormat.MEDIUM, I18N.getCurrentLocale());
44     private final Font severeFont = PdfFonts.SEVERE_CELL.getFont();
45     private final Font normalFont = PdfFonts.NORMAL.getFont();
46
47     PdfCounterErrorReport(Counter counter, Document document) {
48         super(document);
49         assert counter != null;
50         assert counter.isErrorCounter();
51         this.counter = counter;
52     }
53
54     @Override
55     void toPdf() throws DocumentException {
56         final List<CounterError> errors = counter.getErrors();
57         if (errors.isEmpty()) {
58             addToDocument(new Phrase(getString("Aucune_erreur"), normalFont));
59         } else {
60             writeErrors(errors);
61         }
62     }
63
64     private void writeErrors(List<CounterError> errors) throws DocumentException {
65         assert errors != null;
66         final boolean displayUser = HtmlCounterErrorReport.shouldDisplayUser(errors);
67         final boolean displayHttpRequest = HtmlCounterErrorReport.shouldDisplayHttpRequest(errors);
68         if (errors.size() >= Counter.MAX_ERRORS_COUNT) {
69             addToDocument(new Phrase(
70                     getFormattedString("Dernieres_erreurs_seulement", Counter.MAX_ERRORS_COUNT)
71                             + '\n',
72                     severeFont));
73         }
74         writeHeader(displayUser, displayHttpRequest);
75
76         for (final CounterError error : errors) {
77             nextRow();
78             writeError(error, displayUser, displayHttpRequest);
79         }
80         addTableToDocument();
81     }
82
83     private void writeHeader(boolean displayUser, boolean displayHttpRequest)
84             throws DocumentException {
85         final List<String> headers = createHeaders(displayUser, displayHttpRequest);
86         final int[] relativeWidths = new int[headers.size()];
87         Arrays.fill(relativeWidths, 0, headers.size(), 1);
88         if (displayHttpRequest) {
89             relativeWidths[1] = 4; // requĂȘte http
90         }
91         relativeWidths[headers.size() - 1] = 4; // message d'erreur
92
93         initTable(headers, relativeWidths);
94     }
95
96     private List<String> createHeaders(boolean displayUser, boolean displayHttpRequest) {
97         final List<String> headers = new ArrayList<>();
98         headers.add(getString("Date"));
99         if (displayHttpRequest) {
100             headers.add(getString("Requete"));
101         }
102         if (displayUser) {
103             headers.add(getString("Utilisateur"));
104         }
105         headers.add(getString("Erreur"));
106         return headers;
107     }
108
109     private void writeError(CounterError error, boolean displayUser, boolean displayHttpRequest) {
110         getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
111         addCell(dateTimeFormat.format(error.getDate()));
112         getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
113         if (displayHttpRequest) {
114             if (error.getHttpRequest() == null) {
115                 addCell("");
116             } else {
117                 addCell(error.getHttpRequest());
118             }
119         }
120         if (displayUser) {
121             if (error.getRemoteUser() == null) {
122                 addCell("");
123             } else {
124                 addCell(error.getRemoteUser());
125             }
126         }
127         addCell(error.getMessage());
128     }
129 }
130