Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
* Created on Feb 20, 2013 by Ryan Griffith
*
* This software is offered as-is with no license and is free to reproduce or use as anyone sees fit.
*/
package com.mycompany.cascade.plugin;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import com.cms.publish.PublishTrigger;
import com.cms.publish.PublishTriggerException;
import com.cms.publish.PublishTriggerInformation;
import com.hannonhill.cascade.api.asset.home.FolderContainedAsset;
import com.hannonhill.cascade.api.operation.Read;
import com.hannonhill.cascade.api.operation.result.ReadOperationResult;
/**
* This plug-in sends asset information to an external script via POST.
* @author Ryan Griffith
*/
public class CallExternalScriptPublishTrigger implements PublishTrigger
{
private Map<String, String> parameters = new HashMap<String, String>();
private PublishTriggerInformation information;
/* (non-Javadoc)
* @see com.cms.publish.PublishTrigger#invoke()
*/
public void invoke() throws PublishTriggerException
{
HttpURLConnection connection = null;
FolderContainedAsset asset = null;
Read read = new Read();
CustomIdentifier id = new CustomIdentifier();
try {
id.setId(information.getEntityId());
id.setType(information.getEntityType());
read.setToRead(id);
read.setUsername("admin"); // This should be a utility/admin user that has access to the asset(s).
ReadOperationResult result = (ReadOperationResult) read.perform();
asset = (FolderContainedAsset) result.getAsset();
String urlParameters = "id=" + URLEncoder.encode(information.getEntityId(), "UTF-8") +
"&path=" + URLEncoder.encode(information.getEntityPath(), "UTF-8") +
"&siteId=" + URLEncoder.encode(asset.getSiteId(), "UTF-8");
// Open connection
URL url = new URL(parameters.get("external-script-url"));
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
// Send request
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(urlParameters);
wr.flush();
wr.close();
// Complete the request by reading the result.
connection.getInputStream();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
/* (non-Javadoc)
* @see com.cms.publish.PublishTrigger#setParameter(java.lang.String, java.lang.String)
*/
public void setParameter(String name, String value)
{
// let's just store our parameters in a Map for access later
parameters.put(name, value);
}
/* (non-Javadoc)
* @see com.cms.publish.PublishTrigger#setPublishInformation(com.cms.publish.PublishTriggerInformation)
*/
public void setPublishInformation(PublishTriggerInformation information)
{
// store this in an instance member so invoke() has access to it
this.information = information;
}
}
package com.mycompany.cascade.plugin;
import com.cms.publish.PublishTriggerEntityTypes;
import com.hannonhill.cascade.api.asset.common.Identifier;
import com.hannonhill.cascade.model.dom.identifier.EntityType;
import com.hannonhill.cascade.model.dom.identifier.EntityTypes;
public class CustomIdentifier implements Identifier {
private String id;
private EntityType type;
public String getId() {
return id;
}
public EntityType getType() {
return this.type;
}
public void setId(String id) {
this.id = id;
}
public void setType(int type) {
switch (type) {
case PublishTriggerEntityTypes.TYPE_FILE:
this.type = EntityTypes.TYPE_FILE;
break;
case PublishTriggerEntityTypes.TYPE_PAGE:
this.type = EntityTypes.TYPE_PAGE;
break;
}
}
}
<publish-triggers>
<publish-trigger>
<class-name>com.mycompany.cascade.plugin.CallExternalScriptPublishTrigger</class-name>
<parameter name="external-script-url">EXTERNAL_URL_SCRIPT</parameter>
</publish-trigger>
</publish-triggers>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment