ContentId.java

  1. /**
  2.  *
  3.  * Copyright 2016-2017 Fernando Ramirez, Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.bob;

  18. import org.jivesoftware.smack.util.StringUtils;

  19. /**
  20.  * Content-ID class.
  21.  *
  22.  * @author Fernando Ramirez
  23.  * @author Florian Schmaus
  24.  * @see <a href="https://tools.ietf.org/html/rfc2392">RFC 2392: Content-ID and Message-ID Uniform Resource Locators</a>
  25.  */
  26. public class ContentId {

  27.     private final String hash;
  28.     private final String hashType;
  29.     private final String cid;

  30.     private ContentId(String hash, String hashType, String cid) {
  31.         this.hash = StringUtils.requireNotNullNorEmpty(hash, "hash must not be null nor empty");
  32.         this.hashType = StringUtils.requireNotNullNorEmpty(hashType, "hashType must not be null nor empty");
  33.         this.cid = cid;
  34.     }

  35.     /**
  36.      * BoB hash constructor.
  37.      *
  38.      * @param hash TODO javadoc me please
  39.      * @param hashType TODO javadoc me please
  40.      */
  41.     public ContentId(String hash, String hashType) {
  42.         this(hash, hashType, hashType + '+' + hash + "@bob.xmpp.org");
  43.     }

  44.     /**
  45.      * Get the hash.
  46.      *
  47.      * @return the hash
  48.      */
  49.     public String getHash() {
  50.         return hash;
  51.     }

  52.     /**
  53.      * Get the hash type.
  54.      *
  55.      * @return the hash type
  56.      */
  57.     public String getHashType() {
  58.         return hashType;
  59.     }

  60.     /**
  61.      * BoB hash to src attribute string.
  62.      *
  63.      * @return src attribute string
  64.      */
  65.     public String toSrc() {
  66.         return "cid:" + getCid();
  67.     }

  68.     /**
  69.      * BoB hash to cid attribute string.
  70.      *
  71.      * @return cid attribute string
  72.      */
  73.     public String getCid() {
  74.         return cid;
  75.     }

  76.     @Override
  77.     public boolean equals(Object other) {
  78.         if (other instanceof ContentId) {
  79.             ContentId otherBob = (ContentId) other;
  80.             return cid.equals(otherBob.cid);
  81.         }
  82.         return false;
  83.     }

  84.     @Override
  85.     public int hashCode() {
  86.         return cid.hashCode();
  87.     }

  88.     /**
  89.      * Get BoB hash from src attribute string.
  90.      *
  91.      * @param src TODO javadoc me please
  92.      * @return the BoB hash
  93.      */
  94.     public static ContentId fromSrc(String src) {
  95.         String hashType = src.substring(src.lastIndexOf("cid:") + 4, src.indexOf("+"));
  96.         String hash = src.substring(src.indexOf("+") + 1, src.indexOf("@bob.xmpp.org"));
  97.         return new ContentId(hash, hashType);
  98.     }

  99.     /**
  100.      * Get BoB hash from cid attribute string.
  101.      *
  102.      * @param cid TODO javadoc me please
  103.      * @return the BoB hash
  104.      */
  105.     public static ContentId fromCid(String cid) {
  106.         String hashType = cid.substring(0, cid.indexOf("+"));
  107.         String hash = cid.substring(cid.indexOf("+") + 1, cid.indexOf("@bob.xmpp.org"));
  108.         return new ContentId(hash, hashType, cid);
  109.     }

  110. }