Exemple #1
0
 /**
  * Add (overwrite) a query param and return a new URN.
  *
  * @param name Name of parameter
  * @param value The value of parameter
  * @return New URN
  */
 public URN param(final String name, final Object value) {
   if (name == null || value == null) {
     throw new IllegalArgumentException("Parameter name and value must be non-null.");
   }
   final Map<String, String> params = this.params();
   params.put(name, value.toString());
   return URN.create(String.format("%s%s", this.toString().split("\\?")[0], URN.enmap(params)));
 }
Exemple #2
0
 /**
  * Get just body of URN, without params.
  *
  * @return Clean version of it
  */
 public URN pure() {
   String urn = this.toString();
   if (this.hasParams()) {
     urn = urn.substring(0, urn.indexOf('?'));
   }
   return URN.create(urn);
 }
Exemple #3
0
 /**
  * Public constructor.
  *
  * @param nid The namespace ID
  * @param nss The namespace specific string
  */
 public URN(final String nid, final String nss) {
   if (nid == null || nss == null) {
     throw new IllegalArgumentException(
         "Namespace ID (nid) and namespace specific string (nss) must be non-null");
   }
   this.uri = URI.create(String.format("%s%s%s%2$s%s", URN.PREFIX, URN.SEP, nid, URN.encode(nss)));
   try {
     this.validate();
   } catch (URISyntaxException ex) {
     throw new IllegalArgumentException(ex);
   }
 }
Exemple #4
0
 /**
  * Perform proper URL encoding with the text.
  *
  * @param text The text to encode
  * @return The encoded text
  */
 private static String encode(final String text) {
   final StringBuilder encoded = new StringBuilder();
   byte[] bytes;
   try {
     bytes = text.getBytes(URN.UTF_8);
   } catch (java.io.UnsupportedEncodingException ex) {
     throw new IllegalStateException(ex);
   }
   for (byte chr : bytes) {
     if (URN.allowed(chr)) {
       encoded.append((char) chr);
     } else {
       encoded.append("%").append(String.format("%X", chr));
     }
   }
   return encoded.toString();
 }
Exemple #5
0
 /**
  * Encode map of params into query part of URN.
  *
  * @param params Map of params to convert to query suffix
  * @return The suffix of URN, starting with "?"
  */
 private static String enmap(final Map<String, String> params) {
   final StringBuilder query = new StringBuilder();
   if (!params.isEmpty()) {
     query.append("?");
     boolean first = true;
     for (Map.Entry<String, String> param : params.entrySet()) {
       if (!first) {
         query.append("&");
       }
       query.append(param.getKey());
       if (!param.getValue().isEmpty()) {
         query.append("=").append(URN.encode(param.getValue()));
       }
       first = false;
     }
   }
   return query.toString();
 }
Exemple #6
0
 /**
  * Get all params.
  *
  * @return The params
  */
 public Map<String, String> params() {
   return URN.demap(this.toString());
 }