3838import java .security .cert .CertificateException ;
3939import java .security .cert .CertificateFactory ;
4040import java .security .cert .X509Certificate ;
41- import java .text .SimpleDateFormat ;
4241import java .util .ArrayList ;
4342import java .util .Base64 ;
4443import java .util .Collection ;
4544import java .util .Date ;
4645import java .util .Map ;
4746import java .util .Map .Entry ;
4847import java .util .Properties ;
49- import java .util .TimeZone ;
5048
5149import javax .json .Json ;
5250import javax .json .JsonArray ;
104102import org .hyperledger .fabric_ca .sdk .exception .AffiliationException ;
105103import org .hyperledger .fabric_ca .sdk .exception .EnrollmentException ;
106104import org .hyperledger .fabric_ca .sdk .exception .GenerateCRLException ;
105+ import org .hyperledger .fabric_ca .sdk .exception .HFCACertificateException ;
107106import org .hyperledger .fabric_ca .sdk .exception .HTTPException ;
108107import org .hyperledger .fabric_ca .sdk .exception .IdentityException ;
109108import org .hyperledger .fabric_ca .sdk .exception .InfoException ;
110109import org .hyperledger .fabric_ca .sdk .exception .InvalidArgumentException ;
111110import org .hyperledger .fabric_ca .sdk .exception .RegistrationException ;
112111import org .hyperledger .fabric_ca .sdk .exception .RevocationException ;
113112import org .hyperledger .fabric_ca .sdk .helper .Config ;
113+ import org .hyperledger .fabric_ca .sdk .helper .Util ;
114114
115115import static java .lang .String .format ;
116116import static java .nio .charset .StandardCharsets .UTF_8 ;
@@ -189,6 +189,7 @@ public class HFCAClient {
189189 private static final String HFCA_REVOKE = HFCA_CONTEXT_ROOT + "revoke" ;
190190 private static final String HFCA_INFO = HFCA_CONTEXT_ROOT + "cainfo" ;
191191 private static final String HFCA_GENCRL = HFCA_CONTEXT_ROOT + "gencrl" ;
192+ private static final String HFCA_CERTIFICATE = HFCAClient .HFCA_CONTEXT_ROOT + "certificates" ;
192193
193194 private final String url ;
194195 private final boolean isSSL ;
@@ -912,16 +913,16 @@ public String generateCRL(User registrar, Date revokedBefore, Date revokedAfter,
912913 //---------------------------------------
913914 JsonObjectBuilder factory = Json .createObjectBuilder ();
914915 if (revokedBefore != null ) {
915- factory .add ("revokedBefore" , toJson (revokedBefore ));
916+ factory .add ("revokedBefore" , Util . dateToString (revokedBefore ));
916917 }
917918 if (revokedAfter != null ) {
918- factory .add ("revokedAfter" , toJson (revokedAfter ));
919+ factory .add ("revokedAfter" , Util . dateToString (revokedAfter ));
919920 }
920921 if (expireBefore != null ) {
921- factory .add ("expireBefore" , toJson (expireBefore ));
922+ factory .add ("expireBefore" , Util . dateToString (expireBefore ));
922923 }
923924 if (expireAfter != null ) {
924- factory .add ("expireAfter" , toJson (expireAfter ));
925+ factory .add ("expireAfter" , Util . dateToString (expireAfter ));
925926 }
926927 if (caName != null ) {
927928 factory .add (HFCAClient .FABRIC_CA_REQPROP , caName );
@@ -1055,14 +1056,55 @@ public HFCAAffiliation getHFCAAffiliations(User registrar) throws AffiliationExc
10551056
10561057 }
10571058
1058- private String toJson (Date date ) {
1059- final TimeZone utc = TimeZone .getTimeZone ("UTC" );
1059+ /**
1060+ * @return HFCACertificateRequest object
1061+ */
1062+ public HFCACertificateRequest newHFCACertificateRequest () {
1063+ return new HFCACertificateRequest ();
1064+ }
10601065
1061- SimpleDateFormat tformat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSSXXX" );
1062- tformat .setTimeZone (utc );
1063- return tformat .format (date );
1066+ /**
1067+ * Gets all certificates that the registrar is allowed to see and based on filter parameters that
1068+ * are part of the certificate request.
1069+ *
1070+ * @param registrar The identity of the registrar (i.e. who is performing the registration).
1071+ * @param req The certificate request that contains filter parameters
1072+ * @return HFCACertificateResponse object
1073+ * @throws HFCACertificateException Failed to process get certificate request
1074+ */
1075+ public HFCACertificateResponse getHFCACertificates (User registrar , HFCACertificateRequest req ) throws HFCACertificateException {
1076+ try {
1077+ logger .debug (format ("certificate url: %s, registrar: %s" , HFCA_CERTIFICATE , registrar .getName ()));
1078+
1079+ JsonObject result = httpGet (HFCA_CERTIFICATE , registrar , req .getQueryParameters ());
1080+
1081+ int statusCode = result .getInt ("statusCode" );
1082+ Collection <HFCACredential > certs = new ArrayList <>();
1083+ if (statusCode < 400 ) {
1084+ JsonArray certificates = result .getJsonArray ("certs" );
1085+ if (certificates != null && !certificates .isEmpty ()) {
1086+ for (int i = 0 ; i < certificates .size (); i ++) {
1087+ String certPEM = certificates .getJsonObject (i ).getString ("PEM" );
1088+ certs .add (new HFCAX509Certificate (certPEM ));
1089+ }
1090+ }
1091+ logger .debug (format ("certificate url: %s, registrar: %s done." , HFCA_CERTIFICATE , registrar ));
1092+ }
1093+ return new HFCACertificateResponse (statusCode , certs );
1094+ } catch (HTTPException e ) {
1095+ String msg = format ("[Code: %d] - Error while getting certificates from url '%s': %s" , e .getStatusCode (), HFCA_CERTIFICATE , e .getMessage ());
1096+ HFCACertificateException certificateException = new HFCACertificateException (msg , e );
1097+ logger .error (msg );
1098+ throw certificateException ;
1099+ } catch (Exception e ) {
1100+ String msg = format ("Error while getting certificates from url '%s': %s" , HFCA_CERTIFICATE , e .getMessage ());
1101+ HFCACertificateException certificateException = new HFCACertificateException (msg , e );
1102+ logger .error (msg );
1103+ throw certificateException ;
1104+ }
10641105 }
10651106
1107+
10661108 /**
10671109 * Http Post Request.
10681110 *
@@ -1167,8 +1209,12 @@ JsonObject httpPost(String url, String body, User registrar) throws Exception {
11671209 }
11681210
11691211 JsonObject httpGet (String url , User registrar ) throws Exception {
1212+ return httpGet (url , registrar , null );
1213+ }
1214+
1215+ JsonObject httpGet (String url , User registrar , Map <String , String > queryMap ) throws Exception {
11701216 String authHTTPCert = getHTTPAuthCertificate (registrar .getEnrollment (), "" );
1171- url = getURL (url );
1217+ url = getURL (url , queryMap );
11721218 HttpGet httpGet = new HttpGet (url );
11731219 httpGet .setConfig (getRequestConfig ());
11741220 logger .debug (format ("httpGet %s, authHTTPCert: %s" , url , authHTTPCert ));
@@ -1438,13 +1484,7 @@ public Socket createSocket() throws IOException {
14381484 }
14391485
14401486 String getURL (String endpoint ) throws URISyntaxException , MalformedURLException , InvalidArgumentException {
1441- setUpSSL ();
1442- String url = this .url + endpoint ;
1443- URIBuilder uri = new URIBuilder (url );
1444- if (caName != null ) {
1445- uri .addParameter ("ca" , caName );
1446- }
1447- return uri .build ().toURL ().toString ();
1487+ return getURL (endpoint , null );
14481488 }
14491489
14501490 String getURL (String endpoint , Map <String , String > queryMap ) throws URISyntaxException , MalformedURLException , InvalidArgumentException {
@@ -1456,7 +1496,9 @@ String getURL(String endpoint, Map<String, String> queryMap) throws URISyntaxExc
14561496 }
14571497 if (queryMap != null ) {
14581498 for (Map .Entry <String , String > param : queryMap .entrySet ()) {
1459- uri .addParameter (param .getKey (), param .getValue ());
1499+ if (!Utils .isNullOrEmpty (param .getValue ())) {
1500+ uri .addParameter (param .getKey (), param .getValue ());
1501+ }
14601502 }
14611503 }
14621504 return uri .build ().toURL ().toString ();
0 commit comments