How To Programmatically Create Vanity URLs

image

Since version 11gR1, WebCenter Sites has natively provided the capability to allow editors to create vanity URLs for assets out of the box.  Even before that frameworks such as the GSF extended the core product to provide that functionality.  With the SEO benefits and pleasant visitor experience, Vanity URLs have become a necessary feature without which websites’ cannot launch.

WebCenter Sites has the built-in capability to automatically generate vanity URLs upon asset creation via a set pattern.  However there are situations where content is created prior to this mechanism being set up or there was an error in the configured pattern that requires URLs to be generated or amended for existing assets. 

The following code excerpt uses the out of the box Asset API to update the vanity URL for an individual asset:

// name of the Webroot

String newWebRoot = "Avi";

//Publication ID as in WCS DB

Long siteid = 1322052581735L;

//Sitename of the Publication

String sitename="avisports";

//Asset Type

String assettype="Page";

//Asset Id

Long assetid=1329851332601L;

//Default Template Name, if none is set

String templatename = "SectionLayoutGreen";             

List<AssetData> updateAssets = new ArrayList<AssetData>();

Session ses = SessionFactory.getSession();

AssetDataManager mgr =(AssetDataManager) ses.getManager( AssetDataManager.class.getName() );

//create new AssetId instance.

AssetId id = new AssetIdImpl( assettype, assetid );

Iterable<AssetData> assetdataItr = mgr.read( Arrays.asList(id));

     for(AssetData data: assetdataItr){

            // Get Webreference object that contains data needed to generate and resolve the URL

            AttributeData attrDataWebRef = data.getAttributeData("Webreference");

            AttributeData attrDataTemplate = data.getAttributeData("template");

            // If Template is set, use the assigned template for the asset instead of the default

            if(attrDataTemplate != null && attrDataTemplate.getData() != null){

                    templatename = attrDataTemplate.getData().toString();

            }

            // URI for the asset.  This needs to be unique for each asset.

            String newAssetURL = "test-new-vanity-url";

            if(attrDataWebRef != null){

                    List<WebReference> mappingArray = (List<WebReference>) attrDataWebRef.getData();

                    /* If there is an existing Webreference Object, i.e. an existing entry in the

                    Webreference table, update the existing object with new values. */

                    if(mappingArray != null && mappingArray.size()>0){

                           // We assume that there is only one existing entry and we overwrite it.

                           for (int i=0; i < mappingArray.size(); i++)

                           {

                                  WebReference webRef = (WebReference) mappingArray.get(i);

                                  String currentAssetURL = webRef.getAssetUrl();

                                  String currentWebRoot = webRef.getWebRoot();

                                  if( currentWebRoot != null )

                                  {     

                                         webRef.setAssetUrl(newAssetURL);

                                         webRef.setWebRoot(newWebRoot);

                                         webRef.setTemplate(templatename);

                                         updateAssets.add(data);

                                  }

                           }

                                      

                    }

                    // If there is no Webreference object, create a new instance one and attach it to the asset

                    else{

                                WebReference webRef = new WebReferenceImpl(newWebRoot,newAssetURL, new java.lang.Integer(200), templatename);

                                webRef.setWebRoot(newWebRoot);

                                mappingArray.add(webRef);

                                updateAssets.add(data);

                                  }     

                           }     

                    }

 mgr.update(updateAssets);

The code above will add a vanity URL to the asset if there isn’t one and it will amend the exist URL if present.   The above example is a rudimentary example that updates a single asset, hardcodes the URL and assumes that there is only one existing URL if one is present for demonstration purposes.  The code can be modified in the following ways to enhance it for practical uses:

  1. Query multiple assets.
  1. Create business logic to dynamically generate the URL for the asset. 
  1. Assume multiple URLs and update the correct URLs.    

Subscribe to Our Newsletter

Stay In Touch