1 17 package org.alfresco.service.cmr.repository; 18 19 import java.io.Serializable ; 20 21 import org.alfresco.error.AlfrescoRuntimeException; 22 import org.alfresco.util.EqualsHelper; 23 24 29 public class ContentData implements Serializable 30 { 31 private static final long serialVersionUID = 8979634213050121462L; 32 33 private static char[] INVALID_CONTENT_URL_CHARS = new char[] {'|'}; 34 35 private final String contentUrl; 36 private final String mimetype; 37 private final long size; 38 private final String encoding; 39 40 46 public static ContentData createContentProperty(String contentPropertyStr) 47 { 48 int contentUrlIndex = contentPropertyStr.indexOf("contentUrl="); 50 if (contentUrlIndex == -1) 51 { 52 throw new AlfrescoRuntimeException( 53 "ContentData string does not have a content URL: " + 54 contentPropertyStr); 55 } 56 int mimetypeIndex = contentPropertyStr.indexOf("|mimetype=", contentUrlIndex + 11); 57 if (mimetypeIndex == -1) 58 { 59 throw new AlfrescoRuntimeException( 60 "ContentData string does not have a mimetype: " + 61 contentPropertyStr); 62 } 63 int sizeIndex = contentPropertyStr.indexOf("|size=", mimetypeIndex + 10); 64 if (sizeIndex == -1) 65 { 66 throw new AlfrescoRuntimeException( 67 "ContentData string does not have a size: " + 68 contentPropertyStr); 69 } 70 int encodingIndex = contentPropertyStr.indexOf("|encoding=", sizeIndex + 6); 71 if (encodingIndex == -1) 72 { 73 throw new AlfrescoRuntimeException( 74 "ContentData string does not have an encoding: " + 75 contentPropertyStr); 76 } 77 78 String contentUrl = contentPropertyStr.substring(contentUrlIndex + 11, mimetypeIndex); 79 if (contentUrl.length() == 0) 80 contentUrl = null; 81 String mimetype = contentPropertyStr.substring(mimetypeIndex + 10, sizeIndex); 82 if (mimetype.length() == 0) 83 mimetype = null; 84 String sizeStr = contentPropertyStr.substring(sizeIndex + 6, encodingIndex); 85 if (sizeStr.length() == 0) 86 sizeStr = "0"; 87 String encoding = contentPropertyStr.substring(encodingIndex + 10); 88 if (encoding.length() == 0) 89 encoding = null; 90 91 long size = Long.valueOf(sizeStr); 92 93 ContentData property = new ContentData(contentUrl, mimetype, size, encoding); 94 return property; 96 } 97 98 106 public static ContentData setMimetype(ContentData existing, String mimetype) 107 { 108 ContentData ret = new ContentData( 109 existing == null ? null : existing.contentUrl, 110 mimetype, 111 existing == null ? 0L : existing.size, 112 existing == null ? "UTF-8" : existing.encoding); 113 return ret; 115 } 116 117 129 public ContentData(String contentUrl, String mimetype, long size, String encoding) 130 { 131 checkContentUrl(contentUrl, mimetype); 132 133 this.contentUrl = contentUrl; 134 this.mimetype = mimetype; 135 this.size = size; 136 this.encoding = encoding; 137 } 138 139 public boolean equals(Object obj) 140 { 141 if (obj == this) 142 return true; 143 else if (obj == null) 144 return false; 145 else if (!(obj instanceof ContentData)) 146 return false; 147 ContentData that = (ContentData) obj; 148 return (EqualsHelper.nullSafeEquals(this.contentUrl, that.contentUrl) && 149 EqualsHelper.nullSafeEquals(this.mimetype, that.mimetype) && 150 this.size == that.size && 151 EqualsHelper.nullSafeEquals(this.encoding, that.encoding)); 152 } 153 154 157 public String toString() 158 { 159 StringBuilder sb = new StringBuilder (80); 160 sb.append("contentUrl=").append(contentUrl == null ? "" : contentUrl) 161 .append("|mimetype=").append(mimetype == null ? "" : mimetype) 162 .append("|size=").append(size) 163 .append("|encoding=").append(encoding == null ? "" : encoding); 164 return sb.toString(); 165 } 166 167 173 public String getContentUrl() 174 { 175 return contentUrl; 176 } 177 178 185 private void checkContentUrl(String contentUrl, String mimetype) 186 { 187 if (contentUrl != null && contentUrl.length() > 0) 189 { 190 for (int i = 0; i < INVALID_CONTENT_URL_CHARS.length; i++) 191 { 192 for (int j = contentUrl.length() - 1; j > -1; j--) 193 { 194 if (contentUrl.charAt(j) == INVALID_CONTENT_URL_CHARS[i]) 195 { 196 throw new IllegalArgumentException ( 197 "The content URL contains an invalid char: \n" + 198 " content URL: " + contentUrl + "\n" + 199 " char: " + INVALID_CONTENT_URL_CHARS[i] + "\n" + 200 " position: " + j); 201 } 202 } 203 } 204 if (mimetype == null) 206 { 207 throw new IllegalArgumentException ( 208 "The content mimetype must be set whenever the URL is set: \n" + 209 " content URL: " + contentUrl + "\n" + 210 " mimetype: " + mimetype); 211 } 212 } 213 } 214 215 221 public String getMimetype() 222 { 223 return mimetype; 224 } 225 226 231 public long getSize() 232 { 233 return size; 234 } 235 236 242 public String getEncoding() 243 { 244 return encoding; 245 } 246 } 247 | Popular Tags |