| | 461 | } |
|---|
| | 462 | |
|---|
| | 463 | protected static void addQueryParameter(StringBuilder sb, String name, |
|---|
| | 464 | String value, boolean isFirst) { |
|---|
| | 465 | if (isFirst) { |
|---|
| | 466 | sb.append("?"); |
|---|
| | 467 | } else { |
|---|
| | 468 | sb.append("&"); |
|---|
| | 469 | } |
|---|
| | 470 | sb.append(name); |
|---|
| | 471 | sb.append("="); |
|---|
| | 472 | sb.append(value); |
|---|
| | 473 | } |
|---|
| | 474 | |
|---|
| | 475 | /** |
|---|
| | 476 | * Build the nxedit URL for the "edit existing document" use case for a |
|---|
| | 477 | * document using the file:content field as Blob holder |
|---|
| | 478 | * |
|---|
| | 479 | * @return the encoded URL string |
|---|
| | 480 | * @throws ClientException if the URL encoding fails |
|---|
| | 481 | */ |
|---|
| | 482 | public static String liveEditUrl(DocumentModel doc) throws ClientException { |
|---|
| | 483 | return liveEditUrl(doc, DEFAULT_SCHEMA, DEFAULT_BLOB_FIELD, |
|---|
| | 484 | DEFAULT_FILENAME_FIELD); |
|---|
| | 485 | } |
|---|
| | 486 | |
|---|
| | 487 | /** |
|---|
| | 488 | * Build the nxedit URL for the "edit existing document" use case |
|---|
| | 489 | * |
|---|
| | 490 | * @return the encoded URL string |
|---|
| | 491 | * @throws ClientException if the URL encoding fails |
|---|
| | 492 | */ |
|---|
| | 493 | public static String liveEditUrl(DocumentModel doc, String schemaName, |
|---|
| | 494 | String blobFieldName, String filenameFieldName) |
|---|
| | 495 | throws ClientException { |
|---|
| | 496 | |
|---|
| | 497 | // TODO: move constant string in the LiveEditConstants interface |
|---|
| | 498 | StringBuilder queryParamBuilder = new StringBuilder(); |
|---|
| | 499 | addQueryParameter(queryParamBuilder, "action", |
|---|
| | 500 | LiveEditConstants.ACTION_EDIT_DOCUMENT, true); |
|---|
| | 501 | addQueryParameter(queryParamBuilder, "repoID", doc.getRepositoryName(), |
|---|
| | 502 | false); |
|---|
| | 503 | addQueryParameter(queryParamBuilder, "docRef", doc.getRef().toString(), |
|---|
| | 504 | false); |
|---|
| | 505 | addQueryParameter(queryParamBuilder, "schema", schemaName, false); |
|---|
| | 506 | addQueryParameter(queryParamBuilder, "blobField", blobFieldName, false); |
|---|
| | 507 | addQueryParameter(queryParamBuilder, "filenameField", |
|---|
| | 508 | filenameFieldName, false); |
|---|
| | 509 | return buildEncodedUrl(queryParamBuilder); |
|---|
| | 510 | } |
|---|
| | 511 | |
|---|
| | 512 | /** |
|---|
| | 513 | * Build the nxedit URL for the "create new document" use case with a |
|---|
| | 514 | * document using the file:content field as Blob holder |
|---|
| | 515 | * |
|---|
| | 516 | * @param mimetype the mime type of the newly created document |
|---|
| | 517 | * @return the encoded URL string |
|---|
| | 518 | * @throws ClientException if the URL encoding fails |
|---|
| | 519 | */ |
|---|
| | 520 | public static String liveCreateUrl(String mimetype) throws ClientException { |
|---|
| | 521 | return liveCreateUrl(mimetype, DEFAULT_DOCTYPE, DEFAULT_SCHEMA, |
|---|
| | 522 | DEFAULT_BLOB_FIELD, DEFAULT_FILENAME_FIELD); |
|---|
| | 523 | } |
|---|
| | 524 | |
|---|
| | 525 | /** |
|---|
| | 526 | * Build the nxedit URL for the "create new document" use case |
|---|
| | 527 | * |
|---|
| | 528 | * @param mimetype the mime type of the newly created document |
|---|
| | 529 | * @param docType the document type of the document to create |
|---|
| | 530 | * @param schemaName the schema of the blob to hold the new attachment |
|---|
| | 531 | * @param blobFieldName the field name of the blob to hold the new |
|---|
| | 532 | * attachment |
|---|
| | 533 | * @param filenameFieldName the field name of the filename of the new |
|---|
| | 534 | * attachment |
|---|
| | 535 | * @return the encoded URL string |
|---|
| | 536 | * @throws ClientException if the URL encoding fails |
|---|
| | 537 | */ |
|---|
| | 538 | public static String liveCreateUrl(String mimetype, String docType, |
|---|
| | 539 | String schemaName, String blobFieldName, String filenameFieldName) |
|---|
| | 540 | throws ClientException { |
|---|
| | 541 | |
|---|
| | 542 | // TODO: move constant string in the LiveEditConstants interface |
|---|
| | 543 | StringBuilder queryParamBuilder = new StringBuilder(); |
|---|
| | 544 | addQueryParameter(queryParamBuilder, "action", |
|---|
| | 545 | LiveEditConstants.ACTION_CREATE_DOCUMENT, true); |
|---|
| | 546 | addQueryParameter(queryParamBuilder, "mimetype", mimetype, false); |
|---|
| | 547 | addQueryParameter(queryParamBuilder, "schema", schemaName, false); |
|---|
| | 548 | addQueryParameter(queryParamBuilder, "blobField", blobFieldName, false); |
|---|
| | 549 | addQueryParameter(queryParamBuilder, "filenameField", |
|---|
| | 550 | filenameFieldName, false); |
|---|
| | 551 | addQueryParameter(queryParamBuilder, "docType", docType, false); |
|---|
| | 552 | return buildEncodedUrl(queryParamBuilder); |
|---|
| | 553 | } |
|---|
| | 554 | |
|---|
| | 555 | /** |
|---|
| | 556 | * Build the nxedit URL for the "create new document from template" use case |
|---|
| | 557 | * with "File" doc type and "file" schema |
|---|
| | 558 | * |
|---|
| | 559 | * @param template the document holding the blob to be used as template |
|---|
| | 560 | * @return the encoded URL string |
|---|
| | 561 | * @throws ClientException if the URL encoding fails |
|---|
| | 562 | */ |
|---|
| | 563 | public static String liveCreateFromTemplateUrl(DocumentModel template) |
|---|
| | 564 | throws ClientException { |
|---|
| | 565 | return liveCreateFromTemplateUrl(template, DEFAULT_SCHEMA, |
|---|
| | 566 | DEFAULT_BLOB_FIELD, DEFAULT_DOCTYPE, DEFAULT_SCHEMA, |
|---|
| | 567 | DEFAULT_BLOB_FIELD, DEFAULT_FILENAME_FIELD); |
|---|
| | 568 | } |
|---|
| | 569 | |
|---|
| | 570 | /** |
|---|
| | 571 | * Build the nxedit URL for the "create new document from template" use case |
|---|
| | 572 | * |
|---|
| | 573 | * @param template the document holding the blob to be used as template |
|---|
| | 574 | * @param templateSchemaName the schema of the blob holding the template |
|---|
| | 575 | * @param templateBlobFieldName the field name of the blob holding the |
|---|
| | 576 | * template |
|---|
| | 577 | * @param docType the document type of the new document to create |
|---|
| | 578 | * @param schemaName the schema of the new blob to be saved as attachment |
|---|
| | 579 | * @param blobFieldName the field name of the new blob to be saved as |
|---|
| | 580 | * attachment |
|---|
| | 581 | * @param filenameFieldName the field name of the filename of the attachment |
|---|
| | 582 | * @return the encoded URL string |
|---|
| | 583 | * @throws ClientException if the URL encoding fails |
|---|
| | 584 | */ |
|---|
| | 585 | public static String liveCreateFromTemplateUrl(DocumentModel template, |
|---|
| | 586 | String templateSchemaName, String templateBlobFieldName, |
|---|
| | 587 | String docType, String schemaName, String blobFieldName, |
|---|
| | 588 | String filenameFieldName) throws ClientException { |
|---|
| | 589 | // TODO: move constant string in the LiveEditConstants interface |
|---|
| | 590 | StringBuilder queryParamBuilder = new StringBuilder(); |
|---|
| | 591 | addQueryParameter(queryParamBuilder, "action", |
|---|
| | 592 | LiveEditConstants.ACTION_CREATE_DOCUMENT_FROM_TEMPLATE, true); |
|---|
| | 593 | addQueryParameter(queryParamBuilder, "templateRepoID", |
|---|
| | 594 | template.getRepositoryName(), false); |
|---|
| | 595 | addQueryParameter(queryParamBuilder, "templateDocRef", |
|---|
| | 596 | template.getRef().toString(), false); |
|---|
| | 597 | addQueryParameter(queryParamBuilder, "templateSchema", |
|---|
| | 598 | templateSchemaName, false); |
|---|
| | 599 | addQueryParameter(queryParamBuilder, "templateBlobField", |
|---|
| | 600 | templateBlobFieldName, false); |
|---|
| | 601 | addQueryParameter(queryParamBuilder, "schema", schemaName, false); |
|---|
| | 602 | addQueryParameter(queryParamBuilder, "blobField", blobFieldName, false); |
|---|
| | 603 | addQueryParameter(queryParamBuilder, "filenameField", |
|---|
| | 604 | filenameFieldName, false); |
|---|
| | 605 | addQueryParameter(queryParamBuilder, "docType", docType, false); |
|---|
| | 606 | return buildEncodedUrl(queryParamBuilder); |
|---|
| | 607 | } |
|---|
| | 608 | |
|---|
| | 609 | private static String buildEncodedUrl(StringBuilder queryParamBuilder) |
|---|
| | 610 | throws ClientException { |
|---|
| | 611 | StringBuilder sb = new StringBuilder(); |
|---|
| | 612 | FacesContext context = FacesContext.getCurrentInstance(); |
|---|
| | 613 | HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest(); |
|---|
| | 614 | sb.append(composeBaseURL(request)); |
|---|
| | 615 | queryParamBuilder.append(composeTailURL(request)); |
|---|
| | 616 | String queryParams = queryParamBuilder.toString(); |
|---|
| | 617 | try { |
|---|
| | 618 | // do not encode the nxedit:// part since URLEncoder will not handle |
|---|
| | 619 | // it correctly |
|---|
| | 620 | String encodedUrlTail = URLEncoder.encode(queryParams, |
|---|
| | 621 | URL_ENCODE_CHARSET); |
|---|
| | 622 | sb.append(encodedUrlTail); |
|---|
| | 623 | return sb.toString(); |
|---|
| | 624 | } catch (Exception e) { |
|---|
| | 625 | throw new ClientException("failed to encode nxedit URL", e); |
|---|
| | 626 | } |
|---|
| | 627 | } |
|---|
| | 628 | |
|---|
| | 629 | /** |
|---|
| | 630 | * Build the base nxedit:* url based on the current URL from the servlet |
|---|
| | 631 | * request context |
|---|
| | 632 | * |
|---|
| | 633 | * @param request the current HttpServletRequest request |
|---|
| | 634 | * @return a raw nxedit string url with the current server name, port and |
|---|
| | 635 | * URL prefix |
|---|
| | 636 | */ |
|---|
| | 637 | private static String composeBaseURL(HttpServletRequest request) { |
|---|
| | 638 | String baseURL = BaseURL.getBaseURL(request); |
|---|
| | 639 | |
|---|
| | 640 | if (baseURL.contains("http://")) { |
|---|
| | 641 | baseURL = baseURL.replace("http://", NXEDIT_URL_SCHEME + "://"); |
|---|
| | 642 | } else if (baseURL.contains("https://")) { |
|---|
| | 643 | baseURL = baseURL.replace("https://", NXEDIT_URL_SCHEME + "://"); |
|---|
| | 644 | } else { |
|---|
| | 645 | // fallback that should never happen: raise an exception instead? |
|---|
| | 646 | baseURL = NXEDIT_URL_SCHEME + "://" + baseURL; |
|---|
| | 647 | } |
|---|
| | 648 | return baseURL.concat(NXEDIT_URL_VIEW_ID); |
|---|
| | 649 | } |
|---|
| | 650 | |
|---|
| | 651 | /** |
|---|
| | 652 | * Extract the current JSESSIONID value from the request context |
|---|
| | 653 | * |
|---|
| | 654 | * @param request the current HttpServletRequest request |
|---|
| | 655 | * @return the current JSESSIONID string |
|---|
| | 656 | */ |
|---|
| | 657 | private static String extractJSessionId(HttpServletRequest request) { |
|---|
| | 658 | for (Cookie cookie : request.getCookies()) { |
|---|
| | 659 | if (cookie.getName().equalsIgnoreCase("jsessionid")) { |
|---|
| | 660 | return cookie.getValue(); |
|---|
| | 661 | } |
|---|
| | 662 | } |
|---|
| | 663 | return null; |
|---|
| | 664 | } |
|---|
| | 665 | |
|---|
| | 666 | /** |
|---|
| | 667 | * Extract conversation and session ids and build a query parameters string |
|---|
| | 668 | * |
|---|
| | 669 | * @param request the current HttpServletRequest request |
|---|
| | 670 | * @return the query parameter string |
|---|
| | 671 | */ |
|---|
| | 672 | private static String composeTailURL(HttpServletRequest request) { |
|---|
| | 673 | StringBuilder sb = new StringBuilder(); |
|---|
| | 674 | sb.append(Manager.instance().getCurrentConversationId()); |
|---|
| | 675 | addQueryParameter(sb, Manager.instance().getConversationIdParameter(), |
|---|
| | 676 | Manager.instance().getCurrentConversationId(), false); |
|---|
| | 677 | addQueryParameter(sb, JSESSIONID, extractJSessionId(request), false); |
|---|
| | 678 | return sb.toString(); |
|---|