Changeset 30255

Show
Ignore:
Timestamp:
02/18/08 22:39:15 (9 months ago)
Author:
ogrisel
Message:

NXP-2074 Create new JSF functions to generate LiveEdit? v2 URLs (using the nxedit:// scheme)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • org.nuxeo.ecm.platform/trunk/nuxeo-platform-ui-web/src/main/java/org/nuxeo/ecm/platform/ui/web/tag/fn/DocumentModelFunctions.java

    r30232 r30255  
    2525import java.util.Map; 
    2626import javax.faces.context.FacesContext; 
     27import javax.servlet.http.Cookie; 
     28import javax.servlet.http.HttpServletRequest; 
    2729 
    2830import org.apache.commons.logging.Log; 
    2931import org.apache.commons.logging.LogFactory; 
     32import org.jboss.seam.core.Manager; 
    3033import org.nuxeo.ecm.core.api.Blob; 
    3134import org.nuxeo.ecm.core.api.ClientException; 
     
    6063 * 
    6164 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 
    62  * 
     65 * @author <a href="mailto:og@nuxeo.com">Olivier Grisel</a> 
    6366 */ 
    6467public final class DocumentModelFunctions { 
     
    6669    private static final Log log = LogFactory.getLog(DocumentModelFunctions.class); 
    6770 
     71    private static final String DEFAULT_DOCTYPE = "File"; 
     72 
     73    // to be deprecated once all filenames are stored in the blob itself 
     74    private static final String DEFAULT_FILENAME_FIELD = "filename"; 
     75 
     76    private static final String DEFAULT_BLOB_FIELD = "content"; 
     77 
     78    private static final String DEFAULT_SCHEMA = "file"; 
     79 
     80    private static final String JSESSIONID = "JSESSIONID"; 
     81 
     82    private static final String URL_ENCODE_CHARSET = "UTF-8"; 
     83 
    6884    private static final String i18n_prefix = "%i18n"; 
     85 
     86    private static final String NXEDIT_URL_VIEW_ID = "nxliveedit.faces"; 
     87 
     88    private static final String NXEDIT_URL_SCHEME = "nxedit"; 
    6989 
    7090    private static transient MimetypeRegistry mimetypeService; 
     
    236256    public static boolean hasPermission(DocumentModel document, 
    237257            String permission) throws ClientException { 
     258        if (document == null) 
     259            return false; 
    238260        String sid = document.getSessionId(); 
    239261        CoreSession session = null; 
     
    341363     * @param patternName 
    342364     * @param doc The document model. 
    343      * @param index index of the element containing the blob. <code>index</code> starts at 0. 
     365     * @param index index of the element containing the blob. <code>index</code> 
     366     *            starts at 0. 
    344367     * @param filename The filename of the blob. 
    345368     * @return the REST URL for the blob, or <code>null</code> if an arror 
     
    348371    public static String complexFileUrl(String patternName, DocumentModel doc, 
    349372            int index, String filename) { 
    350         return complexFileUrl(patternName, doc, "files:files", index, "file", 
    351                 filename); 
     373        return complexFileUrl(patternName, doc, "files:files", index, 
     374                DEFAULT_FILENAME_FIELD, filename); 
    352375    } 
    353376 
     
    361384     * @param doc The document model. 
    362385     * @param listElement Element containing a list of complex type. 
    363      * @param index Index of the element containing the blob inside the list. <code>index</code> starts at 0. 
     386     * @param index Index of the element containing the blob inside the list. 
     387     *            <code>index</code> starts at 0. 
    364388     * @param blobPropertyName The property containging the blob. 
    365389     * @param filename Filename of the blob. 
     
    435459 
    436460        return null; 
     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(); 
    437679    } 
    438680 
  • org.nuxeo.ecm.platform/trunk/nuxeo-platform-ui-web/src/main/resources/WEB/nxweb-document.taglib.xml

    r27902 r30255  
    3030    </function-class> 
    3131    <function-signature> 
    32       java.lang.String typeView(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String) 
     32      java.lang.String typeView(org.nuxeo.ecm.core.api.DocumentModel, 
     33      java.lang.String) 
    3334    </function-signature> 
    3435  </function> 
     
    4849    </function-class> 
    4950    <function-signature> 
    50       java.lang.String iconExpandedPath(org.nuxeo.ecm.core.api.DocumentModel) 
     51      java.lang.String 
     52      iconExpandedPath(org.nuxeo.ecm.core.api.DocumentModel) 
    5153    </function-signature> 
    5254  </function> 
     
    104106    <function-signature> 
    105107      java.lang.String fileUrl(java.lang.String, 
    106       org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, java.lang.String) 
     108      org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, 
     109      java.lang.String) 
    107110    </function-signature> 
    108111  </function> 
     
    137140    </function-signature> 
    138141  </function> 
     142 
     143  <function> 
     144    <function-name>liveEditUrl</function-name> 
     145    <function-class> 
     146      org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions 
     147    </function-class> 
     148    <function-signature> 
     149      java.lang.String liveEditUrl(org.nuxeo.ecm.core.api.DocumentModel) 
     150    </function-signature> 
     151  </function> 
     152 
     153  <function> 
     154    <function-name>liveEditUrl</function-name> 
     155    <function-class> 
     156      org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions 
     157    </function-class> 
     158    <function-signature> 
     159      java.lang.String liveEditUrl(org.nuxeo.ecm.core.api.DocumentModel, 
     160      java.lang.String, java.lang.String, java.lang.String) 
     161    </function-signature> 
     162  </function> 
     163 
     164  <function> 
     165    <function-name>liveCreateUrl</function-name> 
     166    <function-class> 
     167      org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions 
     168    </function-class> 
     169    <function-signature> 
     170      java.lang.String liveCreateUrl(java.lang.String) 
     171    </function-signature> 
     172  </function> 
     173 
     174  <function> 
     175    <function-name>liveCreateUrl</function-name> 
     176    <function-class> 
     177      org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions 
     178    </function-class> 
     179    <function-signature> 
     180      java.lang.String liveCreateUrl(java.lang.String, java.lang.String, 
     181      java.lang.String, java.lang.String, java.lang.String) 
     182    </function-signature> 
     183  </function> 
     184 
     185  <function> 
     186    <function-name>liveCreateFromTemplateUrl</function-name> 
     187    <function-class> 
     188      org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions 
     189    </function-class> 
     190    <function-signature> 
     191      java.lang.String 
     192      liveCreateFromTemplateUrl(org.nuxeo.ecm.core.api.DocumentModel) 
     193    </function-signature> 
     194  </function> 
     195 
     196  <function> 
     197    <function-name>liveCreateFromTemplateUrl</function-name> 
     198    <function-class> 
     199      org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions 
     200    </function-class> 
     201    <function-signature> 
     202      java.lang.String 
     203      liveCreateFromTemplateUrl(org.nuxeo.ecm.core.api.DocumentModel, 
     204      java.lang.String, java.lang.String, java.lang.String, 
     205      java.lang.String, java.lang.String, java.lang.String) 
     206    </function-signature> 
     207  </function> 
     208 
    139209  <tag> 
    140210    <tag-name>documentLink</tag-name> 
  • org.nuxeo.ecm.platform/trunk/nuxeo-platform-ui-web/src/main/resources/WEB/nxweb-document.tld

    r21783 r30255  
    5151           a blob.</li> 
    5252      <li> documentUrl(DocumentModel): get the rest url for a document.</li> 
     53      <li> liveEditUrl(DocumentModel): get the nxedit:// URL to edit a document 
     54           file attachement (default File-like types)</li> 
     55      <li> liveEditUrl(DocumentModel, String, String, String): get the nxedit:// 
     56           URL to edit a document proviging schema, blob field and filename 
     57           field names</li> 
     58      <li> liveCreateUrl(String): get the nxedit:// url to create a new document 
     59           of type File providing the mimetype as argument</li> 
     60      <li> liveCreateUrl(String, String, String, String, String): get the nxedit:// 
     61            url to create a new document with parameters: mimetype, doctype, schema, 
     62            blob and filename field names</li> 
     63      <li> liveCreateFromTemplateUrl(DocumentModel): get the nxedit:// URL to create 
     64           a new document of type File reusing the content of the blob of the provided 
     65           template DocumentModel (assumed to have the "file" schema).</li> 
     66      <li> liveCreateFromTemplateUrl(DocumentModel, String, String, String, String, 
     67            String, String, String): get the nxedit:// URL to create a new document 
     68            from template. Parameters are: template DocumentModel, template schema, 
     69            template blob field, target document type, target schema, target, blob 
     70            field name, target filename field. </li> 
    5371    </ul> 
    5472    ]]>