Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf committed Sep 13, 2024
1 parent f7d74cc commit 8f00bd2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 51 deletions.
14 changes: 6 additions & 8 deletions src/qz/auth/Certificate.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ public enum Algorithm {
// Valid date range allows UI to only show "Expired" text for valid certificates
private static final Instant UNKNOWN_MIN = LocalDateTime.MIN.toInstant(ZoneOffset.UTC);
private static final Instant UNKNOWN_MAX = LocalDateTime.MAX.toInstant(ZoneOffset.UTC);

// This `dateFormat` corresponds to the `dateParse` in CertificateTable. If this line is changed, change it there as well.
private static DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static DateTimeFormatter dateParse = DateTimeFormatter.ofPattern("uuuu-MM-dd['T'][ ]HH:mm:ss[.n]['Z']"); //allow parsing of both ISO and custom formatted dates
public static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static final DateTimeFormatter DATE_PARSE = DateTimeFormatter.ofPattern("uuuu-MM-dd['T'][ ]HH:mm:ss[.n]['Z']"); //allow parsing of both ISO and custom formatted dates

private X509Certificate theCertificate;
private boolean sponsored;
Expand Down Expand Up @@ -324,8 +322,8 @@ public static Certificate loadCertificate(HashMap<String,String> data) {
cert.organization = data.get("organization");

try {
cert.validFrom = Instant.from(LocalDateTime.from(dateParse.parse(data.get("validFrom"))).atZone(ZoneOffset.UTC));
cert.validTo = Instant.from(LocalDateTime.from(dateParse.parse(data.get("validTo"))).atZone(ZoneOffset.UTC));
cert.validFrom = Instant.from(LocalDateTime.from(DATE_PARSE.parse(data.get("validFrom"))).atZone(ZoneOffset.UTC));
cert.validTo = Instant.from(LocalDateTime.from(DATE_PARSE.parse(data.get("validTo"))).atZone(ZoneOffset.UTC));
}
catch(DateTimeException e) {
cert.validFrom = UNKNOWN_MIN;
Expand Down Expand Up @@ -421,15 +419,15 @@ public String getOrganization() {

public String getValidFrom() {
if (validFrom.isAfter(UNKNOWN_MIN)) {
return dateFormat.format(validFrom.atZone(ZoneOffset.UTC));
return DATE_FORMAT.format(validFrom.atZone(ZoneOffset.UTC));
} else {
return "Not Provided";
}
}

public String getValidTo() {
if (validTo.isBefore(UNKNOWN_MAX)) {
return dateFormat.format(validTo.atZone(ZoneOffset.UTC));
return DATE_FORMAT.format(validTo.atZone(ZoneOffset.UTC));
} else {
return "Not Provided";
}
Expand Down
2 changes: 1 addition & 1 deletion src/qz/ui/SiteManagerDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ public ArrayList<CertificateDisplay> readCertificates(ArrayList<CertificateDispl
String[] data = line.split("\\t");

if (data.length == Certificate.saveFields.length) {
HashMap<String, String> dataMap = new HashMap<>();
HashMap<String,String> dataMap = new HashMap<>();
for(int i = 0; i < data.length; i++) {
dataMap.put(Certificate.saveFields[i], data[i]);
}
Expand Down
87 changes: 45 additions & 42 deletions src/qz/ui/component/CertificateTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,23 @@
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.TimeZone;
import java.util.function.Function;

import static qz.auth.Certificate.*;

/**
* Created by Tres on 2/22/2015.
* Displays Certificate information in a JTable
*/
public class CertificateTable extends DisplayTable implements Themeable {
private Certificate cert;

private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC");
private static DateTimeFormatter dateParse = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

private static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone("UTC");
private static final TimeZone ALTERNATE_TIME_ZONE = TimeZone.getDefault();
private Instant warn;
private Instant now;
private boolean useLocalTimezoneValidTo = false;
private boolean useLocalTimezoneValidFrom = false;

enum CertificateField {
ORGANIZATION("Organization", (Certificate cert) -> cert.getOrganization()),
Expand All @@ -41,21 +39,29 @@ enum CertificateField {

String description;
Function<Certificate, Object> getter;
TimeZone timeZone = DEFAULT_TIME_ZONE; // Date fields only

CertificateField(String description, Function<Certificate, Object> getter) {
this.description = description;
this.getter = getter;
}

public String getValue(Certificate cert, TimeZone timeZone) {
public String getValue(Certificate cert) {
String certFieldValue = getter.apply(cert).toString();
switch(this) {
case VALID_FROM:
case VALID_TO:
if (certFieldValue.equals("Not Provided")) return certFieldValue;
ZonedDateTime utcTime = LocalDateTime.from(dateParse.parse(certFieldValue)).atZone(ZoneOffset.UTC); // Parse the date string as UTC (Z/GMT)
ZonedDateTime zonedTime = Instant.from(utcTime).atZone(timeZone.toZoneId()); // Shift to the new timezone
return dateParse.format(zonedTime) + " " + timeZone.getDisplayName(false, TimeZone.SHORT); // Append a short timezone name e.g. "EST"
if (!certFieldValue.equals("Not Provided")) {
try {
// Parse the date string as UTC (Z/GMT)
ZonedDateTime utcTime = LocalDateTime.from(DATE_PARSE.parse(certFieldValue)).atZone(ZoneOffset.UTC);
// Shift to the new timezone
ZonedDateTime zonedTime = Instant.from(utcTime).atZone(timeZone.toZoneId());
// Append a short timezone name e.g. "EST"
return DATE_PARSE.format(zonedTime) + " " + timeZone.getDisplayName(false, TimeZone.SHORT);
} catch (Exception ignore) {}
}
// fallthrough
default:
return certFieldValue;
}
Expand All @@ -73,36 +79,44 @@ public String getDescription() {
public static int size() {
return values().length;
}

public void toggleTimeZone() {
switch(this) {
case VALID_TO:
case VALID_FROM:
this.timeZone = (timeZone == DEFAULT_TIME_ZONE? ALTERNATE_TIME_ZONE:DEFAULT_TIME_ZONE);
break;
default:
throw new UnsupportedOperationException("TimeZone is only supported for date fields");
}
}
}

public CertificateTable(IconCache iconCache) {
super(iconCache);
setDefaultRenderer(Object.class, new CertificateTableCellRenderer());
addMouseListener(new MouseAdapter() {
int lastRow = -1;
int lastCol = -1;
Point loc = new Point(-1, -1);

@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
JTable target = (JTable)e.getSource();
int row = target.getSelectedRow();
int col = target.getSelectedColumn();
// Only trigger after the cell is click AND highlighted. This make copying text easier.
if (row == lastRow && col == lastCol) {
CertificateField rowKey = (CertificateField)target.getValueAt(row, 0);
if (rowKey == CertificateField.VALID_TO) {
useLocalTimezoneValidTo = !useLocalTimezoneValidTo;
refreshComponents();
changeSelection(row, col, false, false);
} else if (rowKey == CertificateField.VALID_FROM) {
useLocalTimezoneValidFrom = !useLocalTimezoneValidFrom;
refreshComponents();
changeSelection(row, col, false, false);
int x = target.getSelectedColumn();
int y = target.getSelectedRow();
// Only trigger after the cell is click AND highlighted.
if (loc.distance(x, y) == 0) {
CertificateField rowKey = (CertificateField)target.getValueAt(y, 0);
switch(rowKey) {
case VALID_FROM:
case VALID_TO:
rowKey.toggleTimeZone();
refreshComponents();
changeSelection(y, x, false, false);
break;
}
}
lastRow = row;
lastCol = col;
loc.setLocation(x, y);
}
});

Expand All @@ -128,21 +142,10 @@ public void refreshComponents() {

// First Column
for(CertificateField field : CertificateField.values()) {
TimeZone timeZone = null;
switch(field){
case TRUSTED:
if (!Certificate.isTrustBuiltIn()) continue; // Remove "Verified by" text; uncertain in strict mode
break;
case VALID_TO:
timeZone = useLocalTimezoneValidTo ? TimeZone.getDefault() : UTC_TIME_ZONE;
break;
case VALID_FROM:
timeZone = useLocalTimezoneValidFrom ? TimeZone.getDefault() : UTC_TIME_ZONE;
break;
default:
break;
if(field.equals(CertificateField.TRUSTED) && !Certificate.isTrustBuiltIn()) {
continue; // Remove "Verified by" text; uncertain in strict mode
}
model.addRow(new Object[] {field, field.getValue(cert, timeZone)});
model.addRow(new Object[] {field, field.getValue(cert)});
}

repaint();
Expand Down

0 comments on commit 8f00bd2

Please sign in to comment.