|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package edu.harvard.iq.dataverse.pidproviders; |
| 7 | + |
| 8 | +import edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddress; |
| 9 | +import edu.harvard.iq.dataverse.batch.util.LoggingUtil; |
| 10 | +import java.io.UnsupportedEncodingException; |
| 11 | +import java.net.URLEncoder; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.sql.Timestamp; |
| 14 | +import java.text.SimpleDateFormat; |
| 15 | +import java.util.Date; |
| 16 | +import jakarta.enterprise.context.RequestScoped; |
| 17 | +import jakarta.inject.Named; |
| 18 | + |
| 19 | +/** |
| 20 | + * |
| 21 | + * @author qqmyers |
| 22 | + */ |
| 23 | + |
| 24 | +@Named |
| 25 | +@RequestScoped |
| 26 | +public class FailedPIDResolutionLoggingServiceBean { |
| 27 | + |
| 28 | + public static final String LOG_HEADER = "#Fields: pid\trequestURI\tHTTP method\tclient_ip\teventTime\n"; |
| 29 | + |
| 30 | + |
| 31 | + public void logEntry(FailedPIDResolutionEntry entry) { |
| 32 | + LoggingUtil.saveLogFileAppendWithHeader(entry.toString(), "../logs", getLogFileName(), LOG_HEADER); |
| 33 | + } |
| 34 | + |
| 35 | + public String getLogFileName() { |
| 36 | + return "PIDFailures_" + new SimpleDateFormat("yyyy-MM").format(new Timestamp(new Date().getTime())) + ".log"; |
| 37 | + } |
| 38 | + |
| 39 | + public static class FailedPIDResolutionEntry { |
| 40 | + |
| 41 | + private String eventTime; |
| 42 | + private String clientIp; |
| 43 | + private String requestUrl; |
| 44 | + private String identifier; |
| 45 | + private String method; |
| 46 | + |
| 47 | + public FailedPIDResolutionEntry() { |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + public FailedPIDResolutionEntry(String persistentId, String requestURI, String method, IpAddress sourceAddress) { |
| 52 | + try { |
| 53 | + setIdentifier(URLEncoder.encode(persistentId, StandardCharsets.UTF_8.toString())); |
| 54 | + } catch (UnsupportedEncodingException e) { |
| 55 | + // Should never happen |
| 56 | + e.printStackTrace(); |
| 57 | + } |
| 58 | + setRequestUrl(requestURI); |
| 59 | + setMethod(method); |
| 60 | + setClientIp(sourceAddress.toString()); |
| 61 | + setEventTime(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(new Timestamp(new Date().getTime()))); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public String toString() { |
| 66 | + return getIdentifier() + "\t" + |
| 67 | + getRequestUrl() + "\t" + |
| 68 | + getMethod() + "\t" + |
| 69 | + getClientIp() + "\t" + |
| 70 | + getEventTime() + "\n"; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @return the eventTime |
| 75 | + */ |
| 76 | + public String getEventTime() { |
| 77 | + if (eventTime == null) { |
| 78 | + return "-"; |
| 79 | + } |
| 80 | + return eventTime; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param eventTime |
| 85 | + * the eventTime to set |
| 86 | + */ |
| 87 | + public final void setEventTime(String eventTime) { |
| 88 | + this.eventTime = eventTime; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @return the clientIp |
| 93 | + */ |
| 94 | + public String getClientIp() { |
| 95 | + if (clientIp == null) { |
| 96 | + return "-"; |
| 97 | + } |
| 98 | + return clientIp; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @param clientIp |
| 103 | + * the clientIp to set |
| 104 | + */ |
| 105 | + public final void setClientIp(String clientIp) { |
| 106 | + this.clientIp = clientIp; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @return the HTTP Method |
| 111 | + */ |
| 112 | + public String getMethod() { |
| 113 | + return method; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @param method |
| 118 | + * - the HTTP Method used |
| 119 | + */ |
| 120 | + public final void setMethod(String method) { |
| 121 | + this.method = method; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @return the requestUrl |
| 126 | + */ |
| 127 | + public String getRequestUrl() { |
| 128 | + if (requestUrl == null) { |
| 129 | + return "-"; |
| 130 | + } |
| 131 | + return requestUrl; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @param requestUrl |
| 136 | + * the requestUrl to set |
| 137 | + */ |
| 138 | + public final void setRequestUrl(String requestUrl) { |
| 139 | + this.requestUrl = requestUrl; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @return the identifier |
| 144 | + */ |
| 145 | + public String getIdentifier() { |
| 146 | + if (identifier == null) { |
| 147 | + return "-"; |
| 148 | + } |
| 149 | + return identifier; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @param identifier |
| 154 | + * the identifier to set |
| 155 | + */ |
| 156 | + public final void setIdentifier(String identifier) { |
| 157 | + this.identifier = identifier; |
| 158 | + } |
| 159 | + |
| 160 | + } |
| 161 | +} |
0 commit comments