Use alfresco webscript from third party
public class HelperUtil {
private static final Log LOGGER = LogFactoryUtil.getLog(HelperUtil.class);
//Private Default Constructor
private HelperUtil(){
}
public static String getWebScriptURL(String webscriptname) throws UnsupportedEncodingException{
String url = null;
String server = PropsUtil.get("alfresco.webscript.url");
String port = PropsUtil.get("alfresco.webscript.port");
String protocol =PropsUtil.get("alfresco.webscript.protocol");
String service = PropsUtil.get("alfresco.webscript.service");
String fS = StringPool.FORWARD_SLASH;
String cl = StringPool.COLON;
if(Validator.isNotNull(port)) {
url = protocol+cl+fS+fS+server+cl+port+fS+"alfresco"+fS+service+fS+webscriptname;
}else {
url = protocol+cl+fS+fS+server+fS+"alfresco"+fS+service+fS+webscriptname;
}
return url;
}
public static String executeWebscript(String requestBody, String webscriptname, boolean isAdmin, boolean isPost) throws SystemException, HttpException, IOException, UnknownHostException{
LOGGER.debug("executing webscript.... " + webscriptname);
LOGGER.debug("requestBody.... " + requestBody);
String url = getWebScriptURL(webscriptname);
url = url + "?requestBody="+URLEncoder.encode(requestBody,"UTF-8");
LOGGER.debug("url.... " + url);
if(isPost){
return executePostMethod(url, isAdmin);
}
else{
return executeGetMethod(url, isAdmin);
}
}
public static String executeGetMethod(String url, boolean isAdmin) throws HttpException, IOException, SystemException{
String response = null;
String adminUser = PropsUtil.get("alfresco.webscript.admin");
String userHeaderKey = PropsUtil.get("alfresco.webscript.ssouserheader");
LOGGER.debug("url..." + url);
LOGGER.debug("adminUser ...." + adminUser);
LOGGER.debug("userHeaderKey ...." + userHeaderKey);
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
if(isAdmin){
method.addRequestHeader(userHeaderKey, adminUser);
}
else{
method.addRequestHeader(userHeaderKey, HelperUtil.getLogin());
}
client.executeMethod(method);
response = method.getResponseBodyAsString();
LOGGER.debug("response.... " + response);
return response;
}
public static String executePostMethod(String url, boolean isAdmin) throws HttpException, IOException, SystemException{
String response = null;
String adminUser = PropsUtil.get("alfresco.webscript.admin");
String userHeaderKey = PropsUtil.get("alfresco.webscript.ssouserheader");
LOGGER.debug("url..." + url);
LOGGER.debug("adminUser ...." + adminUser);
LOGGER.debug("userHeaderKey ...." + userHeaderKey);
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
if(isAdmin){
method.addRequestHeader(userHeaderKey, adminUser);
}
else{
method.addRequestHeader(userHeaderKey, HelperUtil.getLogin());
}
client.executeMethod(method);
response = method.getResponseBodyAsString();
LOGGER.debug("response.... " + response);
return response;
}
}
e.g properties
### Key for webscript
alfresco.webscript.url=domain.com
alfresco.webscript.port=
alfresco.webscript.protocol=https
alfresco.webscript.ssouserheader=X-Alfresco-Remote-User
alfresco.webscript.service=wcservice
alfresco.webscript.admin=CDTest02
private static final Log LOGGER = LogFactoryUtil.getLog(HelperUtil.class);
//Private Default Constructor
private HelperUtil(){
}
public static String getWebScriptURL(String webscriptname) throws UnsupportedEncodingException{
String url = null;
String server = PropsUtil.get("alfresco.webscript.url");
String port = PropsUtil.get("alfresco.webscript.port");
String protocol =PropsUtil.get("alfresco.webscript.protocol");
String service = PropsUtil.get("alfresco.webscript.service");
String fS = StringPool.FORWARD_SLASH;
String cl = StringPool.COLON;
if(Validator.isNotNull(port)) {
url = protocol+cl+fS+fS+server+cl+port+fS+"alfresco"+fS+service+fS+webscriptname;
}else {
url = protocol+cl+fS+fS+server+fS+"alfresco"+fS+service+fS+webscriptname;
}
return url;
}
public static String executeWebscript(String requestBody, String webscriptname, boolean isAdmin, boolean isPost) throws SystemException, HttpException, IOException, UnknownHostException{
LOGGER.debug("executing webscript.... " + webscriptname);
LOGGER.debug("requestBody.... " + requestBody);
String url = getWebScriptURL(webscriptname);
url = url + "?requestBody="+URLEncoder.encode(requestBody,"UTF-8");
LOGGER.debug("url.... " + url);
if(isPost){
return executePostMethod(url, isAdmin);
}
else{
return executeGetMethod(url, isAdmin);
}
}
public static String executeGetMethod(String url, boolean isAdmin) throws HttpException, IOException, SystemException{
String response = null;
String adminUser = PropsUtil.get("alfresco.webscript.admin");
String userHeaderKey = PropsUtil.get("alfresco.webscript.ssouserheader");
LOGGER.debug("url..." + url);
LOGGER.debug("adminUser ...." + adminUser);
LOGGER.debug("userHeaderKey ...." + userHeaderKey);
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
if(isAdmin){
method.addRequestHeader(userHeaderKey, adminUser);
}
else{
method.addRequestHeader(userHeaderKey, HelperUtil.getLogin());
}
client.executeMethod(method);
response = method.getResponseBodyAsString();
LOGGER.debug("response.... " + response);
return response;
}
public static String executePostMethod(String url, boolean isAdmin) throws HttpException, IOException, SystemException{
String response = null;
String adminUser = PropsUtil.get("alfresco.webscript.admin");
String userHeaderKey = PropsUtil.get("alfresco.webscript.ssouserheader");
LOGGER.debug("url..." + url);
LOGGER.debug("adminUser ...." + adminUser);
LOGGER.debug("userHeaderKey ...." + userHeaderKey);
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
if(isAdmin){
method.addRequestHeader(userHeaderKey, adminUser);
}
else{
method.addRequestHeader(userHeaderKey, HelperUtil.getLogin());
}
client.executeMethod(method);
response = method.getResponseBodyAsString();
LOGGER.debug("response.... " + response);
return response;
}
}
e.g properties
### Key for webscript
alfresco.webscript.url=domain.com
alfresco.webscript.port=
alfresco.webscript.protocol=https
alfresco.webscript.ssouserheader=X-Alfresco-Remote-User
alfresco.webscript.service=wcservice
alfresco.webscript.admin=CDTest02
Comments
Post a Comment