You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CertificatesTableModel.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright 2012 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.authority;
  17. import java.math.BigInteger;
  18. import java.security.cert.X509Certificate;
  19. import java.util.Collections;
  20. import java.util.Comparator;
  21. import java.util.Date;
  22. import javax.swing.table.AbstractTableModel;
  23. import com.gitblit.client.Translation;
  24. import com.gitblit.utils.X509Utils.RevocationReason;
  25. /**
  26. * Table model of a list of user certificate models.
  27. *
  28. * @author James Moger
  29. *
  30. */
  31. public class CertificatesTableModel extends AbstractTableModel {
  32. private static final long serialVersionUID = 1L;
  33. UserCertificateModel ucm;
  34. enum Columns {
  35. SerialNumber, Status, Reason, Issued, Expires;
  36. @Override
  37. public String toString() {
  38. return name().replace('_', ' ');
  39. }
  40. }
  41. public CertificatesTableModel() {
  42. }
  43. @Override
  44. public int getRowCount() {
  45. return ucm == null || ucm.certs == null ? 0 : ucm.certs.size();
  46. }
  47. @Override
  48. public int getColumnCount() {
  49. return Columns.values().length;
  50. }
  51. @Override
  52. public String getColumnName(int column) {
  53. Columns col = Columns.values()[column];
  54. switch (col) {
  55. case SerialNumber:
  56. return Translation.get("gb.serialNumber");
  57. case Issued:
  58. return Translation.get("gb.issued");
  59. case Expires:
  60. return Translation.get("gb.expires");
  61. case Status:
  62. return Translation.get("gb.status");
  63. case Reason:
  64. return Translation.get("gb.reason");
  65. }
  66. return "";
  67. }
  68. /**
  69. * Returns <code>Object.class</code> regardless of <code>columnIndex</code>.
  70. *
  71. * @param columnIndex
  72. * the column being queried
  73. * @return the Object.class
  74. */
  75. public Class<?> getColumnClass(int columnIndex) {
  76. Columns col = Columns.values()[columnIndex];
  77. switch (col) {
  78. case Status:
  79. return CertificateStatus.class;
  80. case Issued:
  81. return Date.class;
  82. case Expires:
  83. return Date.class;
  84. case SerialNumber:
  85. return BigInteger.class;
  86. default:
  87. return String.class;
  88. }
  89. }
  90. @Override
  91. public boolean isCellEditable(int rowIndex, int columnIndex) {
  92. Columns col = Columns.values()[columnIndex];
  93. switch (col) {
  94. default:
  95. return false;
  96. }
  97. }
  98. @Override
  99. public Object getValueAt(int rowIndex, int columnIndex) {
  100. X509Certificate cert = ucm.certs.get(rowIndex);
  101. Columns col = Columns.values()[columnIndex];
  102. switch (col) {
  103. case Status:
  104. return ucm.getStatus(cert);
  105. case SerialNumber:
  106. return cert.getSerialNumber();
  107. case Issued:
  108. return cert.getNotBefore();
  109. case Expires:
  110. return cert.getNotAfter();
  111. case Reason:
  112. if (ucm.getStatus(cert).equals(CertificateStatus.revoked)) {
  113. RevocationReason r = ucm.getRevocationReason(cert.getSerialNumber());
  114. return Translation.get("gb." + r.name());
  115. }
  116. }
  117. return null;
  118. }
  119. public X509Certificate get(int modelRow) {
  120. return ucm.certs.get(modelRow);
  121. }
  122. public void setUserCertificateModel(UserCertificateModel ucm) {
  123. this.ucm = ucm;
  124. Collections.sort(ucm.certs, new Comparator<X509Certificate>() {
  125. @Override
  126. public int compare(X509Certificate o1, X509Certificate o2) {
  127. // sort by issue date in reverse chronological order
  128. int result = o2.getNotBefore().compareTo(o1.getNotBefore());
  129. if (result == 0) {
  130. // same issue date, show expiring first
  131. boolean r1 = CertificatesTableModel.this.ucm.isRevoked(o1.getSerialNumber());
  132. boolean r2 = CertificatesTableModel.this.ucm.isRevoked(o2.getSerialNumber());
  133. if ((r1 && r2) || (!r1 && !r2)) {
  134. // both revoked or both not revoked
  135. // chronlogical order by expiration dates
  136. result = o1.getNotAfter().compareTo(o2.getNotAfter());
  137. } else if (r1) {
  138. // r1 is revoked, r2 first
  139. return 1;
  140. } else {
  141. // r2 is revoked, r1 first
  142. return -1;
  143. }
  144. }
  145. return result;
  146. }
  147. });
  148. }
  149. }