001/**
002 *
003 * Copyright 2016-2017 Fernando Ramirez, Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.bob;
018
019import org.jivesoftware.smack.util.StringUtils;
020
021/**
022 * Content-ID class.
023 *
024 * @author Fernando Ramirez
025 * @author Florian Schmaus
026 * @see <a href="https://tools.ietf.org/html/rfc2392">RFC 2392: Content-ID and Message-ID Uniform Resource Locators</a>
027 */
028public class ContentId {
029
030    private final String hash;
031    private final String hashType;
032    private final String cid;
033
034    private ContentId(String hash, String hashType, String cid) {
035        this.hash = StringUtils.requireNotNullNorEmpty(hash, "hash must not be null nor empty");
036        this.hashType = StringUtils.requireNotNullNorEmpty(hashType, "hashType must not be null nor empty");
037        this.cid = cid;
038    }
039
040    /**
041     * BoB hash constructor.
042     *
043     * @param hash TODO javadoc me please
044     * @param hashType TODO javadoc me please
045     */
046    public ContentId(String hash, String hashType) {
047        this(hash, hashType, hashType + '+' + hash + "@bob.xmpp.org");
048    }
049
050    /**
051     * Get the hash.
052     *
053     * @return the hash
054     */
055    public String getHash() {
056        return hash;
057    }
058
059    /**
060     * Get the hash type.
061     *
062     * @return the hash type
063     */
064    public String getHashType() {
065        return hashType;
066    }
067
068    /**
069     * BoB hash to src attribute string.
070     *
071     * @return src attribute string
072     */
073    public String toSrc() {
074        return "cid:" + getCid();
075    }
076
077    /**
078     * BoB hash to cid attribute string.
079     *
080     * @return cid attribute string
081     */
082    public String getCid() {
083        return cid;
084    }
085
086    @Override
087    public boolean equals(Object other) {
088        if (other instanceof ContentId) {
089            ContentId otherBob = (ContentId) other;
090            return cid.equals(otherBob.cid);
091        }
092        return false;
093    }
094
095    @Override
096    public int hashCode() {
097        return cid.hashCode();
098    }
099
100    /**
101     * Get BoB hash from src attribute string.
102     *
103     * @param src TODO javadoc me please
104     * @return the BoB hash
105     */
106    public static ContentId fromSrc(String src) {
107        String hashType = src.substring(src.lastIndexOf("cid:") + 4, src.indexOf("+"));
108        String hash = src.substring(src.indexOf("+") + 1, src.indexOf("@bob.xmpp.org"));
109        return new ContentId(hash, hashType);
110    }
111
112    /**
113     * Get BoB hash from cid attribute string.
114     *
115     * @param cid TODO javadoc me please
116     * @return the BoB hash
117     */
118    public static ContentId fromCid(String cid) {
119        String hashType = cid.substring(0, cid.indexOf("+"));
120        String hash = cid.substring(cid.indexOf("+") + 1, cid.indexOf("@bob.xmpp.org"));
121        return new ContentId(hash, hashType, cid);
122    }
123
124}