| 1 16 package org.outerj.daisy.repository.clientimpl; 17 18 import org.outerj.daisy.repository.*; 19 import org.outerj.daisy.repository.Credentials; 20 import org.outerj.daisy.repository.clientimpl.infrastructure.AbstractRemoteStrategy; 21 import org.outerj.daisy.repository.clientimpl.infrastructure.DaisyHttpClient; 22 import org.outerj.daisy.repository.schema.FieldType; 23 import org.outerj.daisy.repository.commonimpl.*; 24 import org.outerj.daisy.repository.commonimpl.schema.CommonRepositorySchema; 25 import org.outerj.daisy.util.VersionHelper; 26 import org.apache.commons.httpclient.*; 27 import org.apache.commons.httpclient.methods.GetMethod; 28 import org.apache.commons.httpclient.methods.MultipartPostMethod; 29 import org.apache.commons.httpclient.methods.PostMethod; 30 import org.apache.commons.httpclient.methods.DeleteMethod; 31 import org.apache.commons.httpclient.methods.multipart.PartSource; 32 import org.apache.commons.httpclient.methods.multipart.FilePart; 33 import org.outerx.daisy.x10.*; 34 35 import java.io.InputStream ; 36 import java.io.IOException ; 37 import java.io.ByteArrayInputStream ; 38 import java.io.ByteArrayOutputStream ; 39 import java.util.ArrayList ; 40 import java.util.Properties ; 41 42 public class RemoteDocumentStrategy extends AbstractRemoteStrategy implements DocumentStrategy { 43 44 public RemoteDocumentStrategy(RemoteRepositoryManager.Context context) { 45 super(context); 46 } 47 48 public Document load(AuthenticatedUser user, long documentId, long branchId, long languageId) throws RepositoryException { 49 DaisyHttpClient httpClient = getClient(user); 50 HttpMethod method = new GetMethod("/repository/document/" + documentId); 51 method.setQueryString(getBranchLangParams(branchId, languageId)); 52 53 DocumentDocument documentDocument = (DocumentDocument)httpClient.executeMethod(method, DocumentDocument.class, true); 54 DocumentDocument.Document documentXml = documentDocument.getDocument(); 55 Document document = instantiateDocumentFromXml(documentXml, user); 56 return document; 57 } 58 59 private Document instantiateDocumentFromXml(DocumentDocument.Document documentXml, AuthenticatedUser user) throws RepositoryException { 60 DocumentImpl document = new DocumentImpl(this, context.getCommonRepository(), user, documentXml.getTypeId(), documentXml.getBranchId(), documentXml.getLanguageId()); 61 DocumentImpl.IntimateAccess documentInt = document.getIntimateAccess(this); 62 documentInt.load(documentXml.getId(), documentXml.getLastModified().getTime(), documentXml.getLastModifier(), documentXml.getCreated().getTime(), documentXml.getOwner(), documentXml.getPrivate(), documentXml.getUpdateCount()); 63 64 DocumentVariantImpl variant = documentInt.getVariant(); 65 DocumentVariantImpl.IntimateAccess variantInt = variant.getIntimateAccess(this); 66 67 variantInt.load(documentXml.getTypeId(), 68 documentXml.getRetired(), 69 documentXml.getLastVersionId(), 70 documentXml.isSetLiveVersionId() ? documentXml.getLiveVersionId() : -1, 71 documentXml.getVariantLastModified().getTime(), 72 documentXml.getVariantLastModifier(), 73 documentXml.getCreatedFromBranchId(), 74 documentXml.getCreatedFromLanguageId(), 75 documentXml.getCreatedFromVersionId(), 76 documentXml.getVariantUpdateCount()); 77 78 long[] collectionIds = documentXml.getCollectionIds().getCollectionIdArray(); 79 for (int i=0; i < collectionIds.length; i++) { 80 DocumentCollectionImpl collection = context.getCommonRepository().getCollectionManager().getCollection(collectionIds[i], false, user); 84 variantInt.addCollection(collection); 85 } 86 87 DocumentDocument.Document.CustomFields.CustomField[] customFieldsXml = documentXml.getCustomFields().getCustomFieldArray(); 88 for (int i = 0; i < customFieldsXml.length; i++) { 89 variantInt.setCustomField(customFieldsXml[i].getName(), customFieldsXml[i].getValue()); 90 } 91 92 variantInt.setLockInfo(instantiateLockInfo(documentXml.getLockInfo())); 93 94 if (documentXml.isSetDataVersionId()) { variantInt.setName(documentXml.getName()); 96 97 if (documentXml.getSummary() != null) 98 variantInt.setSummary(documentXml.getSummary()); 99 100 FieldImpl[] fields = instantiateFields(variantInt, documentXml.getFields().getFieldArray()); 101 for (int i = 0; i < fields.length; i++) 102 variantInt.addField(fields[i]); 103 104 PartImpl[] parts = instantiateParts(variantInt, documentXml.getParts().getPartArray(), variantInt.getLastVersionId()); 105 for (int i = 0; i < parts.length; i++) 106 variantInt.addPart(parts[i]); 107 108 LinkImpl[] links = instantiateLinks(documentXml.getLinks().getLinkArray()); 109 for (int i = 0; i < links.length; i++) 110 variantInt.addLink(links[i]); 111 112 return document; 113 } else { 114 return new ReadLiveOnlyDocument(document, this); 116 } 117 } 118 119 private FieldImpl[] instantiateFields(DocumentVariantImpl.IntimateAccess variantInt, FieldDocument.Field[] fieldsXml) { 120 CommonRepositorySchema repositorySchema = context.getCommonRepositorySchema(); 121 ArrayList fields = new ArrayList (); 122 for (int i = 0; i < fieldsXml.length; i++) { 123 long typeId = fieldsXml[i].getTypeId(); 124 FieldType fieldType; 125 try { 126 fieldType = repositorySchema.getFieldTypeById(typeId, false, variantInt.getCurrentUser()); 127 } catch (RepositoryException e) { 128 throw new RuntimeException (DocumentImpl.ERROR_ACCESSING_REPOSITORY_SCHEMA, e); 129 } 130 ValueType valueType = fieldType.getValueType(); 131 Object value = FieldHelper.getFieldValueFromXml(valueType, fieldType.isMultiValue(), fieldsXml[i]); 132 FieldImpl field = new FieldImpl(variantInt, typeId, value); 133 fields.add(field); 134 } 135 return (FieldImpl[])fields.toArray(new FieldImpl[fields.size()]); 136 } 137 138 private PartImpl[] instantiateParts(DocumentVariantImpl.IntimateAccess variantInt, PartDocument.Part[] partsXml, long versionId) { 139 ArrayList parts = new ArrayList (); 140 for (int i = 0; i < partsXml.length; i++) { 141 PartImpl part = new PartImpl(variantInt, partsXml[i].getTypeId(), versionId); 142 PartImpl.IntimateAccess partInt = part.getIntimateAccess(this); 143 partInt.setMimeType(partsXml[i].getMimeType()); 144 partInt.setFileName(partsXml[i].getFileName()); 145 partInt.setSize(partsXml[i].getSize()); 146 parts.add(part); 147 } 148 return (PartImpl[])parts.toArray(new PartImpl[parts.size()]); 149 } 150 151 private LinkImpl[] instantiateLinks(LinksDocument.Links.Link[] linksXml) { 152 ArrayList links = new ArrayList (); 153 for (int i = 0; i < linksXml.length; i++) { 154 LinkImpl link = new LinkImpl(linksXml[i].getTitle(), linksXml[i].getTarget()); 155 links.add(link); 156 } 157 return (LinkImpl[])links.toArray(new LinkImpl[links.size()]); 158 } 159 160 private LockInfoImpl instantiateLockInfo(LockInfoDocument.LockInfo lockInfoXml) { 161 if (!lockInfoXml.getHasLock()) 162 return new LockInfoImpl(); 163 164 return new LockInfoImpl(lockInfoXml.getUserId(), lockInfoXml.getTimeAcquired().getTime(), 165 lockInfoXml.getDuration(), LockType.fromString(lockInfoXml.getType().toString())); 166 } 167 168 public void store(DocumentImpl document) throws RepositoryException { 169 DocumentImpl.IntimateAccess documentInt = document.getIntimateAccess(this); 170 DocumentVariantImpl.IntimateAccess variantInt = documentInt.getVariant().getIntimateAccess(this); 171 DaisyHttpClient httpClient = getClient(documentInt.getCurrentUser()); 172 String url; 173 if (document.getId() == -1) 174 url = "/repository/document"; 175 else 176 url = "/repository/document/" + document.getId(); 177 178 179 MultipartPostMethod method = new MultipartPostMethod(url); 180 181 if (document.isVariantNew()) { 182 method.setQueryString(new NameValuePair[] { 183 new NameValuePair("createVariant", "yes"), 184 new NameValuePair("startBranch", String.valueOf(variantInt.getStartBranchId())), 185 new NameValuePair("startLanguage", String.valueOf(variantInt.getStartLanguageId())) 186 }); 187 } 188 189 DocumentDocument documentDocument = document.getXml(); 191 PartDocument.Part[] partsXml = documentDocument.getDocument().getParts().getPartArray(); 192 PartImpl[] parts = variantInt.getPartImpls(); 193 for (int i = 0; i < parts.length; i++) { 194 PartImpl.IntimateAccess partInt = parts[i].getIntimateAccess(this); 195 if (partInt.isDataUpdated()) { 196 String uploadPartName = String.valueOf(i); 197 PartDataSource partDataSource = partInt.getPartDataSource(); 198 method.addPart(new FilePart(uploadPartName, new PartPartSource(partDataSource, uploadPartName))); 199 for (int z = 0; z < partsXml.length; z++) { 201 if (partsXml[z].getTypeId() == parts[i].getTypeId()) 202 partsXml[z].setDataRef(uploadPartName); 203 } 204 } 205 } 206 207 ByteArrayOutputStream xmlOS = new ByteArrayOutputStream (5000); 209 try { 210 documentDocument.save(xmlOS); 211 } catch (IOException e) { 212 throw new RepositoryException("Error serializing document XML.", e); 213 } 214 method.addPart(new FilePart("xml", new ByteArrayPartSource(xmlOS.toByteArray(), "xml"))); 215 216 DocumentDocument responseDocumentDocument = (DocumentDocument)httpClient.executeMethod(method, DocumentDocument.class, true); 218 DocumentDocument.Document documentXml = responseDocumentDocument.getDocument(); 219 if (documentXml.getUpdateCount() != document.getUpdateCount()) 220 documentInt.saved(documentXml.getId(), documentXml.getLastModified().getTime(), documentXml.getCreated().getTime(), documentXml.getUpdateCount()); 221 if (documentXml.getVariantUpdateCount() != document.getVariantUpdateCount()) 222 variantInt.saved(documentXml.getLastVersionId(), 223 documentXml.isSetLiveVersionId() ? documentXml.getLiveVersionId() : -1, 224 documentXml.getVariantLastModified().getTime(), 225 documentXml.getSummary(), documentXml.getVariantUpdateCount()); 226 } 227 228 public void deleteDocument(long documentId, AuthenticatedUser user) throws RepositoryException { 229 DaisyHttpClient httpClient = getClient(user); 230 231 String url = "/repository/document/" + documentId; 232 233 DeleteMethod method = new DeleteMethod(url); 234 httpClient.executeMethod(method, null, true); 235 context.getCommonRepository().fireRepositoryEvent(RepositoryEventType.DOCUMENT_DELETED, documentId, -1); 236 } 237 238 public InputStream getBlob(long documentId, long branchId, long languageId, long versionId, long partTypeId, AuthenticatedUser user) throws RepositoryException { 239 return getBlob(documentId, branchId, languageId, String.valueOf(versionId), String.valueOf(partTypeId), user); 240 } 241 242 public InputStream getBlob(String blobKey) throws RepositoryException { 243 throw new RepositoryException("This method is not supported."); 244 } 245 246 public InputStream getBlob(long documentId, long branchId, long languageId, String version, String partType, AuthenticatedUser user) throws RepositoryException { 247 DaisyHttpClient httpClient = getClient(user); 248 HttpMethod method = new GetMethod("/repository/document/" + documentId + "/version/" + version + "/part/" + partType + "/data"); 249 method.setQueryString(getBranchLangParams(branchId, languageId)); 250 251 httpClient.executeMethod(method, null, false); 252 try { 253 InputStream is = method.getResponseBodyAsStream(); 254 return new ReleaseHttpConnectionOnStreamCloseInputStream(is, method); 255 } catch (IOException e) { 256 method.releaseConnection(); 257 throw new RepositoryException("Error getting response input stream.", e); 258 } 259 } 260 261 static class ReleaseHttpConnectionOnStreamCloseInputStream extends InputStream { 262 private final InputStream delegate; 263 private final HttpMethod method; 264 265 public ReleaseHttpConnectionOnStreamCloseInputStream(InputStream delegate, HttpMethod method) { 266 this.delegate = delegate; 267 this.method = method; 268 } 269 270 public int available() throws IOException { 271 return delegate.available(); 272 } 273 274 275 public void close() throws IOException { 276 method.releaseConnection(); 277 delegate.close(); 278 } 279 280 public synchronized void reset() throws IOException { 281 delegate.reset(); 282 } 283 284 public boolean markSupported() { 285 return delegate.markSupported(); 286 } 287 288 public synchronized void mark(int readlimit) { 289 delegate.mark(readlimit); 290 } 291 292 public long skip(long n) throws IOException { 293 return delegate.skip(n); 294 } 295 296 public int read(byte b[]) throws IOException { 297 return delegate.read(b); 298 } 299 300 public int read(byte b[], int off, int len) throws IOException { 301 return delegate.read(b, off, len); 302 } 303 304 public int read() throws IOException { 305 return delegate.read(); 306 } 307 } 308 309 public Document createVariant(long documentId, long startBranchId, long startLanguageId, long startVersionId, long newBranchId, long newLanguageId, AuthenticatedUser user) throws RepositoryException { 310 DaisyHttpClient httpClient = getClient(user); 311 String url = "/repository/document/" + documentId; 312 PostMethod method = new PostMethod(url); 313 314 NameValuePair[] queryString = { 315 new NameValuePair("action", "createVariant"), 316 new NameValuePair("startBranch", String.valueOf(startBranchId)), 317 new NameValuePair("startLanguage", String.valueOf(startLanguageId)), 318 new NameValuePair("startVersion", String.valueOf(startVersionId)), 319 new NameValuePair("newBranch", String.valueOf(newBranchId)), 320 new NameValuePair("newLanguage", String.valueOf(newLanguageId)) 321 }; 322 323 method.setQueryString(queryString); 324 DocumentDocument responseDocumentDocument = (DocumentDocument)httpClient.executeMethod(method, DocumentDocument.class, true); 325 DocumentDocument.Document documentXml = responseDocumentDocument.getDocument(); 326 return instantiateDocumentFromXml(documentXml, user); 327 } 328 329 public AvailableVariantImpl[] getAvailableVariants(long documentId, AuthenticatedUser user) throws RepositoryException { 330 DaisyHttpClient httpClient = getClient(user); 331 String url = "/repository/document/" + documentId + "/availableVariants"; 332 GetMethod method = new GetMethod(url); 333 AvailableVariantsDocument availableVariantsDocument = (AvailableVariantsDocument)httpClient.executeMethod(method, AvailableVariantsDocument.class, true); 334 AvailableVariantDocument.AvailableVariant[] availableVariantsXml = availableVariantsDocument.getAvailableVariants().getAvailableVariantArray(); 335 336 AvailableVariantImpl[] availableVariants = new AvailableVariantImpl[availableVariantsXml.length]; 337 for (int i = 0; i < availableVariantsXml.length; i++) { 338 availableVariants[i] = instantiateAvailableVariantFromXml(availableVariantsXml[i], user); 339 } 340 return availableVariants; 341 } 342 343 private AvailableVariantImpl instantiateAvailableVariantFromXml(AvailableVariantDocument.AvailableVariant availableVariantXml, AuthenticatedUser user) { 344 return new AvailableVariantImpl(availableVariantXml.getBranchId(), availableVariantXml.getLanguageId(), 345 availableVariantXml.getRetired(), availableVariantXml.getLiveVersionId(), 346 context.getCommonRepository().getVariantManager(), user); 347 } 348 349 public void deleteVariant(long documentId, long branchId, long languageId, AuthenticatedUser user) throws RepositoryException { 350 DaisyHttpClient httpClient = getClient(user); 351 352 String url = "/repository/document/" + documentId; 353 354 DeleteMethod method = new DeleteMethod(url); 355 method.setQueryString(getBranchLangParams(branchId, languageId)); 356 httpClient.executeMethod(method, null, true); 357 context.getCommonRepository().fireVariantEvent(DocumentVariantEventType.DOCUMENT_VARIANT_DELETED, documentId, branchId, languageId, -1); 358 } 359 360 public VersionImpl loadVersion(DocumentVariantImpl variant, long versionId) throws RepositoryException { 361 DocumentVariantImpl.IntimateAccess variantInt = variant.getIntimateAccess(this); 362 DaisyHttpClient httpClient = getClient(variantInt.getCurrentUser()); 363 HttpMethod method = new GetMethod("/repository/document/" + variant.getDocumentId() + "/version/" + versionId); 364 method.setQueryString(getBranchLangParams(variant.getBranchId(), variant.getLanguageId())); 365 366 VersionDocument versionDocument = (VersionDocument)httpClient.executeMethod(method, VersionDocument.class, true); 367 VersionDocument.Version versionXml = versionDocument.getVersion(); 368 return instantiateVersion(variantInt, versionXml); 369 } 370 371 public void completeVersion(DocumentVariantImpl variant, VersionImpl version) throws RepositoryException { 372 VersionImpl loadedVersion = loadVersion(variant, version.getId()); 373 VersionImpl.IntimateAccess versionInt = version.getIntimateAccess(this); 374 versionInt.setFields((FieldImpl[])loadedVersion.getFields().getArray()); 375 versionInt.setParts((PartImpl[])loadedVersion.getParts().getArray()); 376 versionInt.setLinks((LinkImpl[])loadedVersion.getLinks().getArray()); 377 } 378 379 public VersionImpl[] loadShallowVersions(DocumentVariantImpl variant) throws RepositoryException { 380 DocumentVariantImpl.IntimateAccess variantInt = variant.getIntimateAccess(this); 381 DaisyHttpClient httpClient = getClient(variantInt.getCurrentUser()); 382 HttpMethod method = new GetMethod("/repository/document/" + variant.getDocumentId() + "/version"); 383 method.setQueryString(getBranchLangParams(variant.getBranchId(), variant.getLanguageId())); 384 385 VersionsDocument versionsDocument = (VersionsDocument)httpClient.executeMethod(method, VersionsDocument.class, true); 386 VersionDocument.Version[] versionsXml = versionsDocument.getVersions().getVersionArray(); 387 VersionImpl[] versions = new VersionImpl[versionsXml.length]; 388 for (int i = 0; i < versionsXml.length; i++) { 389 versions[i] = instantiateVersion(variantInt, versionsXml[i]); 390 } 391 return versions; 392 } 393 394 public void setVersionState(DocumentImpl document, VersionImpl version, VersionState versionState) throws RepositoryException { 395 DocumentImpl.IntimateAccess documentInt = document.getIntimateAccess(this); 396 VersionImpl.IntimateAccess versionInt = version.getIntimateAccess(this); 397 DaisyHttpClient httpClient = getClient(documentInt.getCurrentUser()); 398 HttpMethod method = new PostMethod("/repository/document/" + document.getId() + "/version/" + version.getId()); 399 400 NameValuePair[] queryString = new NameValuePair[4]; 401 queryString[0] = new NameValuePair("action", "changeState"); 402 queryString[1] = new NameValuePair("newState", versionState.toString()); 403 queryString[2] = new NameValuePair("branch", String.valueOf(document.getBranchId())); 404 queryString[3] = new NameValuePair("language", String.valueOf(document.getLanguageId())); 405 method.setQueryString(queryString); 406 407 VersionDocument versionDocument = (VersionDocument)httpClient.executeMethod(method, VersionDocument.class, true); 408 VersionDocument.Version versionXml = versionDocument.getVersion(); 409 versionInt.stateChanged(VersionState.fromString(versionXml.getState()), versionXml.getStateLastModified().getTime(), 410 versionXml.getStateLastModifier()); 411 } 412 413 private VersionImpl instantiateVersion(DocumentVariantImpl.IntimateAccess variantInt, 414 VersionDocument.Version versionXml) { 415 VersionImpl version = new VersionImpl(variantInt, versionXml.getId(), versionXml.getDocumentName(), 416 versionXml.getCreated().getTime(), versionXml.getCreator(), 417 VersionState.fromString(versionXml.getState()), versionXml.getStateLastModified().getTime(), 418 versionXml.getStateLastModifier(), versionXml.getTotalSizeOfParts()); 419 420 VersionImpl.IntimateAccess versionInt = version.getIntimateAccess(this); 421 422 if (versionXml.getParts() != null) 423 versionInt.setParts(instantiateParts(variantInt, versionXml.getParts().getPartArray(), version.getId())); 424 425 if (versionXml.getFields() != null) 426 versionInt.setFields(instantiateFields(variantInt, versionXml.getFields().getFieldArray())); 427 428 if (versionXml.getLinks() != null) 429 versionInt.setLinks(instantiateLinks(versionXml.getLinks().getLinkArray())); 430 431 return version; 432 } 433 434 public LockInfoImpl lock(DocumentVariantImpl variant, long duration, LockType lockType) throws RepositoryException { 435 DocumentVariantImpl.IntimateAccess variantInt = variant.getIntimateAccess(this); 436 DaisyHttpClient httpClient = getClient(variantInt.getCurrentUser()); 437 PostMethod method = new PostMethod("/repository/document/" + variant.getDocumentId() + "/lock"); 438 method.setQueryString(getBranchLangParams(variant.getBranchId(), variant.getLanguageId())); 439 440 { 441 LockInfoDocument lockInfoDocument = LockInfoDocument.Factory.newInstance(); 442 LockInfoDocument.LockInfo lockInfoXml = lockInfoDocument.addNewLockInfo(); 443 lockInfoXml.setDuration(duration); 444 lockInfoXml.setType(LockInfoDocument.LockInfo.Type.Enum.forString(lockType.toString())); 445 method.setRequestBody(lockInfoDocument.newInputStream()); 446 } 447 448 LockInfoDocument lockInfoDocument = (LockInfoDocument)httpClient.executeMethod(method, LockInfoDocument.class, true); 449 LockInfoDocument.LockInfo lockInfoXml = lockInfoDocument.getLockInfo(); 450 return instantiateLockInfo(lockInfoXml); 451 } 452 453 public LockInfoImpl getLockInfo(DocumentVariantImpl variant) throws RepositoryException { 454 DocumentVariantImpl.IntimateAccess variantInt = variant.getIntimateAccess(this); 455 DaisyHttpClient httpClient = getClient(variantInt.getCurrentUser()); 456 GetMethod method = new GetMethod("/repository/document/" + variant.getDocumentId() + "/lock"); 457 method.setQueryString(getBranchLangParams(variant.getBranchId(), variant.getLanguageId())); 458 459 LockInfoDocument lockInfoDocument = (LockInfoDocument)httpClient.executeMethod(method, LockInfoDocument.class, true); 460 LockInfoDocument.LockInfo lockInfoXml = lockInfoDocument.getLockInfo(); 461 return instantiateLockInfo(lockInfoXml); 462 } 463 464 public LockInfoImpl releaseLock(DocumentVariantImpl variant) throws RepositoryException { 465 DocumentVariantImpl.IntimateAccess variantInt = variant.getIntimateAccess(this); 466 DaisyHttpClient httpClient = getClient(variantInt.getCurrentUser()); 467 DeleteMethod method = new DeleteMethod("/repository/document/" + variant.getDocumentId() + "/lock"); 468 method.setQueryString(getBranchLangParams(variant.getBranchId(), variant.getLanguageId())); 469 470 LockInfoDocument lockInfoDocument = (LockInfoDocument)httpClient.executeMethod(method, LockInfoDocument.class, true); 471 LockInfoDocument.LockInfo lockInfoXml = lockInfoDocument.getLockInfo(); 472 return instantiateLockInfo(lockInfoXml); 473 } 474 475 static class ByteArrayPartSource implements PartSource { 476 private final byte[] data; 477 private final String fileName; 478 479 public ByteArrayPartSource(byte[] data, String fileName) { 480 this.data = data; 481 this.fileName = fileName; 482 } 483 484 public String getFileName() { 485 return fileName; 486 } 487 488 public long getLength() { 489 return data.length; 490 } 491 492 public InputStream createInputStream() throws IOException { 493 return new ByteArrayInputStream (data); 494 } 495 } 496 497 static class PartPartSource implements PartSource { 498 private final PartDataSource partDataSource; 499 private final String fileName; 500 501 public PartPartSource(PartDataSource partDataSource, String fileName) { 502 this.partDataSource = partDataSource; 503 this.fileName = fileName; 504 } 505 506 public String getFileName() { 507 return fileName; 508 } 509 510 public long getLength() { 511 return partDataSource.getSize(); 512 } 513 514 public InputStream createInputStream() throws IOException { 515 try { 516 return partDataSource.createInputStream(); 517 } catch (RepositoryException e) { 518 throw new IOException ("RepositoryException: " + e.toString()); 519 } 520 } 521 } 522 523 526 public AuthenticatedUser getUser(Credentials credentials) throws RepositoryException { 527 HttpState httpState = new HttpState(); 528 httpState.setAuthenticationPreemptive(true); 529 String login = credentials.getLogin().replaceAll("@", "@@"); 530 UsernamePasswordCredentials httpClientCredentials = new UsernamePasswordCredentials(login, credentials.getPassword()); 531 httpState.setCredentials(null, null, httpClientCredentials); 532 DaisyHttpClient httpClient = new DaisyHttpClient(context.getSharedHttpClient(), context.getSharedHostConfiguration(), httpState); 533 534 HttpMethod method = new GetMethod("/repository/userinfo"); 535 UserInfoDocument userInfoDocument = (UserInfoDocument)httpClient.executeMethod(method, UserInfoDocument.class, true); 536 UserInfoDocument.UserInfo userInfoXml = userInfoDocument.getUserInfo(); 537 AuthenticatedUserImpl user = new AuthenticatedUserImpl(userInfoXml.getUserId(), credentials.getPassword(), userInfoXml.getActiveRoleIds().getRoleIdArray(), userInfoXml.getAvailableRoleIds().getRoleIdArray(), credentials.getLogin()); 538 return user; 539 } 540 541 public String getClientVersion(AuthenticatedUser user) { 542 Properties versionProps; 543 try { 544 versionProps = VersionHelper.getVersionProperties(getClass().getClassLoader(), "org/outerj/daisy/repository/clientimpl/versioninfo.properties"); 545 } catch (IOException e) { 546 throw new RepositoryRuntimeException("Error getting version information.", e); 547 } 548 String version = VersionHelper.getVersion(versionProps); 549 if (version != null) 550 return version; 551 else 552 throw new RepositoryRuntimeException("Version unknown."); 553 } 554 555 public String getServerVersion(AuthenticatedUser user) { 556 DaisyHttpClient httpClient = getClient(user); 557 HttpMethod method = new GetMethod("/repository/userinfo"); 560 try { 561 httpClient.executeMethod(method, null, true); 562 } catch (RepositoryException e) { 563 throw new RepositoryRuntimeException("Error getting version info.", e); 564 } 565 Header header = method.getResponseHeader("X-Daisy-Version"); 566 if (header != null && header.getValue() != null && header.getValue().length() > 0) { 567 String version = header.getValue(); 568 int spacePos = version.indexOf(' '); 569 if (spacePos > 0) 570 return version.substring(0, spacePos); 571 else 572 return version; 573 } else { 574 throw new RepositoryRuntimeException("Repository server did not communicate version (missing X-Daisy-Version header)."); 575 } 576 } 577 } 578 | Popular Tags |