1616
1717package org .hyperledger .fabric_ca .sdk ;
1818
19- import java .io .PrintWriter ;
20- import java .io .StringWriter ;
2119import java .util .ArrayList ;
20+ import java .util .Arrays ;
2221import java .util .Collection ;
22+ import java .util .Collections ;
23+ import java .util .HashSet ;
24+ import java .util .Set ;
2325
2426import javax .json .Json ;
2527import javax .json .JsonArray ;
2628import javax .json .JsonArrayBuilder ;
2729import javax .json .JsonObject ;
2830import javax .json .JsonObjectBuilder ;
29- import javax .json .JsonWriter ;
3031
3132import org .apache .commons .logging .Log ;
3233import org .apache .commons .logging .LogFactory ;
3334import org .hyperledger .fabric .sdk .User ;
3435import org .hyperledger .fabric .sdk .helper .Utils ;
35- import org .hyperledger .fabric_ca .sdk .exception .AffiliationException ;
3636import org .hyperledger .fabric_ca .sdk .exception .HTTPException ;
3737import org .hyperledger .fabric_ca .sdk .exception .IdentityException ;
3838import org .hyperledger .fabric_ca .sdk .exception .InvalidArgumentException ;
@@ -62,6 +62,8 @@ public class HFCAIdentity {
6262
6363 static final String HFCA_IDENTITY = HFCAClient .HFCA_CONTEXT_ROOT + "identities" ;
6464 private static final Log logger = LogFactory .getLog (HFCAIdentity .class );
65+ //These attributes can not be modified with REST put request.
66+ private static final Set <String > filtredUpdateAttrNames = new HashSet <>(Arrays .asList ("hf.EnrollmentID" , "hf.Type" , "hf.Affiliation" ));
6567
6668 HFCAIdentity (String enrollmentID , HFCAClient client ) throws InvalidArgumentException {
6769 if (Utils .isNullOrEmpty (enrollmentID )) {
@@ -145,21 +147,22 @@ public String getAffiliation() {
145147
146148 /**
147149 * Set affiliation of the identity
148- * @param affiliation Affiliation name
149150 *
151+ * @param affiliation Affiliation name
150152 */
151153 public void setAffiliation (String affiliation ) {
152154 this .affiliation = affiliation ;
153155 }
154156
155157 /**
156158 * Set affiliation of the identity
157- * @param affiliation Affiliation name
158159 *
160+ * @param affiliation Affiliation name
159161 */
160162 public void setAffiliation (HFCAAffiliation affiliation ) {
161163 this .affiliation = affiliation .getName ();
162164 }
165+
163166 /**
164167 * The attributes of the identity
165168 *
@@ -188,7 +191,7 @@ public boolean isDeleted() {
188191 *
189192 * @param registrar The identity of the registrar (i.e. who is performing the registration).
190193 * @return statusCode The HTTP status code in the response
191- * @throws IdentityException if retrieving an identity fails.
194+ * @throws IdentityException if retrieving an identity fails.
192195 * @throws InvalidArgumentException Invalid (null) argument specified
193196 */
194197
@@ -225,7 +228,7 @@ public int read(User registrar) throws IdentityException, InvalidArgumentExcepti
225228 }
226229 this .deleted = false ;
227230 return statusCode ;
228- } catch (HTTPException e ) {
231+ } catch (HTTPException e ) {
229232 String msg = format ("[Code: %d] - Error while getting user '%s' from url '%s': %s" , e .getStatusCode (), getEnrollmentId (), readIdURL , e .getMessage ());
230233 IdentityException identityException = new IdentityException (msg , e );
231234 logger .error (msg );
@@ -244,7 +247,7 @@ public int read(User registrar) throws IdentityException, InvalidArgumentExcepti
244247 *
245248 * @param registrar The identity of the registrar (i.e. who is performing the registration).
246249 * @return statusCode The HTTP status code in the response
247- * @throws IdentityException if creating an identity fails.
250+ * @throws IdentityException if creating an identity fails.
248251 * @throws InvalidArgumentException Invalid (null) argument specified
249252 */
250253
@@ -264,7 +267,7 @@ public int create(User registrar) throws IdentityException, InvalidArgumentExcep
264267 String body = client .toJson (idToJsonObject ());
265268 JsonObject result = client .httpPost (createURL , body , registrar );
266269 statusCode = result .getInt ("statusCode" );
267- if (statusCode >= 400 ) {
270+ if (statusCode < 400 ) {
268271 getHFCAIdentity (result );
269272 logger .debug (format ("identity url: %s, registrar: %s done." , createURL , registrar ));
270273 }
@@ -283,12 +286,12 @@ public int create(User registrar) throws IdentityException, InvalidArgumentExcep
283286 }
284287 }
285288
286- /**
289+ /**
287290 * update an identity
288291 *
289292 * @param registrar The identity of the registrar (i.e. who is performing the registration).
290293 * @return statusCode The HTTP status code in the response
291- * @throws IdentityException if adding an identity fails.
294+ * @throws IdentityException if adding an identity fails.
292295 * @throws InvalidArgumentException Invalid (null) argument specified
293296 */
294297
@@ -305,7 +308,7 @@ public int update(User registrar) throws IdentityException, InvalidArgumentExcep
305308 updateURL = client .getURL (HFCA_IDENTITY + "/" + getEnrollmentId ());
306309 logger .debug (format ("identity url: %s, registrar: %s" , updateURL , registrar .getName ()));
307310
308- String body = client .toJson (idToJsonObject ());
311+ String body = client .toJson (idToJsonObject (filtredUpdateAttrNames ));
309312 JsonObject result = client .httpPut (updateURL , body , registrar );
310313
311314 statusCode = result .getInt ("statusCode" );
@@ -332,7 +335,7 @@ public int update(User registrar) throws IdentityException, InvalidArgumentExcep
332335 *
333336 * @param registrar The identity of the registrar (i.e. who is performing the registration).
334337 * @return statusCode The HTTP status code in the response
335- * @throws IdentityException if adding an identity fails.
338+ * @throws IdentityException if adding an identity fails.
336339 * @throws InvalidArgumentException Invalid (null) argument specified
337340 */
338341
@@ -393,6 +396,10 @@ private void getHFCAIdentity(JsonObject result) {
393396
394397 // Convert the identity request to a JSON object
395398 private JsonObject idToJsonObject () {
399+ return idToJsonObject (Collections .emptySet ());
400+ }
401+
402+ private JsonObject idToJsonObject (Set <String > filteredAttrs ) {
396403 JsonObjectBuilder ob = Json .createObjectBuilder ();
397404 ob .add ("id" , enrollmentID );
398405 ob .add ("type" , type );
@@ -404,7 +411,9 @@ private JsonObject idToJsonObject() {
404411 }
405412 JsonArrayBuilder ab = Json .createArrayBuilder ();
406413 for (Attribute attr : attrs ) {
407- ab .add (attr .toJsonObject ());
414+ if (!filteredAttrs .contains (attr .getName ())) {
415+ ab .add (attr .toJsonObject ());
416+ }
408417 }
409418 ob .add ("attrs" , ab .build ());
410419 if (this .secret != null ) {
0 commit comments