//get current user
String currentUser = AuthenticationUtil.getFullyAuthenticatedUser();
String fakeUser = "IAMBIGD";
LOGGER.debug("currentUser:" + currentUser);
if (currentUser == null || !currentUser.equals(fakeUser)) {
// AuthenticationUtil.setCurrentUser(username);
//set impersonate
AuthenticationUtil.setFullyAuthenticatedUser(fakeUser);
}
星期三, 7月 30, 2014
[Alfresco] impersonate 登入用戶
alfresco提供以下方法讓你可以模仿別的用戶登入的授權。
星期二, 4月 08, 2014
[Alfresco] 如何使用log4j除錯
本文介紹如何使用alfresco內的log4j來除錯 :D
做完設定後,請記得重開tomcat server
log4j 路徑:
/usr/local/TOMCAT/webapps/alfresco/WEB-INF/classes/log4j.properties
logger.log("Hello Debug");
修正log4j properties
log4j.logger.org.alfresco.repo.jscript=debug log4j.logger.org.alfresco.repo.jscript.ScriptLogger=debug
private static Log LOGGER = LogFactory.getLog(UploadObject.class);
LOGGER.debug("Hello Debug");
修正log4j properties
log4j.logger.tw.org.iii.cosa.javabacked.webscript=debug log4j.logger.tw.org.iii.cosa.javabacked.utility=debug
log4j.logger.org.example=debug
做完設定後,請記得重開tomcat server
log4j 路徑:
/usr/local/TOMCAT/webapps/alfresco/WEB-INF/classes/log4j.properties
Javascript webscript logging
在js controller加入以下語法logger.log("Hello Debug");
修正log4j properties
log4j.logger.org.alfresco.repo.jscript=debug log4j.logger.org.alfresco.repo.jscript.ScriptLogger=debug
Java backed webscript logging
在java controller加入以下語法private static Log LOGGER = LogFactory.getLog(UploadObject.class);
LOGGER.debug("Hello Debug");
修正log4j properties
log4j.logger.tw.org.iii.cosa.javabacked.webscript=debug log4j.logger.tw.org.iii.cosa.javabacked.utility=debug
log4j.logger.org.example=debug
星期日, 4月 14, 2013
[Alfresco] 如何取得webscript的參數
這篇記錄如何從webscript取得QueryString、Path parameter of URL與Post body
QueryString
<url>/people/search/email?filter={email}</url>
Path parameter
<url>/person/{userName}</url>
Post Body
For example, imagine a web script URL template of
Reference:
http://wiki.alfresco.com/wiki/Web_Scripts_Examples#URL_Argument_Handling
QueryString
<url>/people/search/email?filter={email}</url>
function main()
{
var filter = args["filter"];
}
Path parameter
<url>/person/{userName}</url>
function main()
{
// Get the user name of the person to get
var userName = url.extension;
}
Post Body
function main(){
//invalid input
if(!json.has("users")){
status.setCode(status.STATUS_BAD_REQUEST, "The request body format is error.");
return;
}
var usersIndex ;
var users = json.get("users");//json array
for (usersIndex = 0; usersIndex < users.length(); usersIndex++){
//Get user val
var currentUser = users.get(usersIndex);
}
其他補充
url
A host object providing access to the URL (or parts of the URL) that triggered the web script.
- context
- Alfresco context path, for example /alfresco
- serviceContext
- Alfresco service context path, for example /alfresco/service
- service
- Web script path, for example /alfresco/service/blog/search
- full
- Web script URL, for example /alfresco/service/blog/search?q=tutorial
- templateArgs
- a map of substituted token values (within the URI path) indexed by token name
- args
- Web script URL arguments, for example q=tutorial
- match
- The part of the web script URL that matched the web script URL template
- extension
- The part of the web script URL that extends beyond the match path (if there is no extension, an empty string is returned)
For example, imagine a web script URL template of
/user/{userid}
and a web script request URL of
/alfresco/service/user/fred?profile=full&format=html
The url root object will respond as follows:
- url.context => /alfresco
- url.serviceContext => /alfresco/service
- url.service => /alfresco/service/user/fred
- url.full => /alfresco/service/user/fred?profile=full&format=html
- url.args => profile=full&format=html
- url.templateArgs['userid'] => fred
- url.match => /user/
- url.extension => fred
http://wiki.alfresco.com/wiki/Web_Scripts_Examples#URL_Argument_Handling
星期日, 2月 24, 2013
[Alfresco] 在java backed取得post request body
先前已經用過javascript webscript取的post request body。
這次要改用javabacked的方式取得。
範例如下:
這次要改用javabacked的方式取得。
範例如下:
public void execute(WebScriptRequest req, WebScriptResponse response) {
InputStream inputBody = req.getContent().getInputStream();
String requestBody = RequestBODY.get(inputBody,Charset.forName("utf-8"));
//requestBody:{"parentUUID":"23d97062-5310-4b80-864a-a2232238fd11","uuids":[]}
System.out.println("requestBody:" + requestBody);
}
星期五, 2月 22, 2013
[Alfresco] 下載Zip
原本的alfresco不支援下載多個檔案與資料夾,可以透過自已實作java backed的方式實現。
以下是找到的範例,不過用的alfresco似乎跟我的不一樣:)
http://code.google.com/p/alfresco-application-samples/
以下是找到的範例,不過用的alfresco似乎跟我的不一樣:)
http://code.google.com/p/alfresco-application-samples/
星期三, 2月 06, 2013
[Alfresco] 檢查當前節點下的路徑是否存在
今天想要提升一下目前上傳元件的功能,需要支援拖拉資料夾的上傳。
昨天已經成功上傳所有目錄檔案,只差建資料夾的整合。
需要一個api來自動建立不存在的資料夾!!,之前已經有實作直接建資料夾的webscript。
目前需要一個如何判斷一個資料夾是否存在的方法。
以下是簡單的範例
星期一, 7月 02, 2012
[Alfresco] 實作alfreso javabacked 上傳心得 How to upload file using java backed web script
本文記錄使用Java API實作上傳的測試心得
上網找到的取得HttpServletRequest 都不適用於alfresco 3.4.5版本
// NOTE: This web script must be executed in a HTTP Servlet environment
// if (!(req instanceof WebScriptServletRequest)) {
// throw new WebScriptException(
// "Content retrieval must be executed in HTTP Servlet environment");
// }
// HttpServletRequest httpReq = ((WebScriptServletRequest)req).getHttpServletRequest();
發生錯誤如下: ClassCastException
用以下這段程式就能正常取得上傳檔案了,先前的中文問題出在client指定錯誤編碼了,繞了一大圈竟然是手誤呀!!
上網找到的取得HttpServletRequest 都不適用於alfresco 3.4.5版本
// NOTE: This web script must be executed in a HTTP Servlet environment
// if (!(req instanceof WebScriptServletRequest)) {
// throw new WebScriptException(
// "Content retrieval must be executed in HTTP Servlet environment");
// }
// HttpServletRequest httpReq = ((WebScriptServletRequest)req).getHttpServletRequest();
發生錯誤如下: ClassCastException
protected Map<string, object> executeImpl(
WebScriptRequest req,
Status status,
Cache cache) {
WrappingWebScriptRequest wrappingWebScriptRequest = (WrappingWebScriptRequest) req;
WebScriptRequest webScriptRequest = wrappingWebScriptRequest.getNext();
WebScriptServletRequest servletRequest = (WebScriptServletRequest) webScriptRequest;
FormField uploadFile = servletRequest.getFileField("file");
//file field
uploadFileName = uploadFile.getFilename();
uploadMIMEType = uploadFile.getMimetype();
uploadContent = uploadFile.getInputStream();
System.out.println("[form data] filename:" + uploadFileName);
System.out.println("[form data] mimetype:" + uploadMIMEType);
//do something
}
用以下這段程式就能正常取得上傳檔案了,先前的中文問題出在client指定錯誤編碼了,繞了一大圈竟然是手誤呀!!
//how to get WebScriptServletRequest WrappingWebScriptRequest wrappingWebScriptRequest = (WrappingWebScriptRequest) req; WebScriptRequest webScriptRequest = wrappingWebScriptRequest.getNext(); WebScriptServletRequest servletRequest = (WebScriptServletRequest) webScriptRequest; //get data form form FormData formData = (FormData)servletRequest.parseContent(); FormData.FormField[] formFields = formData.getFields(); int fieldsLen = formFields.length; for(int i=0;i如果要做更複雜的行為不在這篇的討論範例 :)
星期二, 6月 19, 2012
[Alfresco] How to create Java backed web script in alfresco (3)
這個範例在Google都找的到,沒做多大的修改。
重點是記錄Spring Bean XML的差異性。
Spring Bean XML
重點在於property標籤設定,如果你參考了foundation的api請記得把他加進來!!,
所以這個範例要使用了Repository,請設定rel=repositoryHelper。
Java backed controller
透過xml的設定, 就可以透過 public void setRepository(Repository repository) {}直接呼叫alfresco的核心api了。
重點是記錄Spring Bean XML的差異性。
Spring Bean XML
重點在於property標籤設定,如果你參考了foundation的api請記得把他加進來!!,
所以這個範例要使用了Repository,請設定rel=repositoryHelper。
<bean
id="webscript.org.example.simplejavadir.get"
class="org.example.SimpleJavaDir"
parent="webscript">
<property name="repository" ref="repositoryHelper"/>
</bean>
Java backed controller
透過xml的設定, 就可以透過 public void setRepository(Repository repository) {}直接呼叫alfresco的核心api了。
package org.example;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.repo.model.Repository;
import org.alfresco.service.cmr.repository.NodeRef;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
public class SimpleJavaDir extends DeclarativeWebScript {
private Repository repository;
private static Log s_logger = LogFactory.getLog(SimpleJavaDir.class);
public void setRepository(Repository repository) {
this.repository = repository;
}
@Override
protected Map<string, object> executeImpl(WebScriptRequest req,
Status status, Cache cache) {
s_logger.debug("SimpleJavaDir entry");
// extract folder listing arguments from URI
String verboseArg = req.getParameter("verbose");
Boolean verbose = Boolean.parseBoolean(verboseArg);
Map<string, string> templateArgs = req.getServiceMatch().getTemplateVars();
String folderPath = templateArgs.get("folderpath");
String nodePath = "workspace/SpacesStore/" + folderPath;
// @param1 one of "node", "path", or "avmpath"
NodeRef folder = repository.findNodeRef("path", nodePath.split("/"));
// validate that folder has been found
if (folder == null) {
throw new WebScriptException(Status.STATUS_NOT_FOUND, "Folder "
+ folderPath + " not found");
//you will see the followning messages in your server
// ERROR [extensions.webscripts.AbstractRuntime] Exception from executeScript - redirecting to status template error: 05190001 Folder Company Homes not found
}
// construct model for response template to render
Map<string, object> model = new HashMap<string, object>();
model.put("verbose", verbose);
model.put("folder", folder);
model.put("nodePath", nodePath);
return model;
}
}
星期一, 5月 28, 2012
[Alfresco] Alfresco Namespace
Note: This list will expand / change between now and the next release.
| Namespace | Common Prefix | Description |
|---|---|---|
| http://www.alfresco.org | alf | General Alfresco Namespace |
| http://www.alfresco.org/model/dictionary/1.0 | d | Data Dictionary model |
| http://www.alfresco.org/model/system/1.0 | sys | Repository system model |
| http://www.alfresco.org/model/content/1.0 | cm | Content Domain model |
| http://www.alfresco.org/model/application/1.0 | app | Application model |
| http://www.alfresco.org/model/bpm/1.0 | bpm | Business Process Model |
| http://www.alfresco.org/model/site/1.0 | st | Site Model |
| http://www.alfresco.org/model/forum/1.0 | fm | Forum Model |
| http://www.alfresco.org/model/user/1.0 | usr | User model (in repository.jar) |
| http://www.alfresco.org/view/repository/1.0 | view | Alfresco Import / Export View |
| http://www.alfresco.org/model/action/1.0 | act | Action service model |
| http://www.alfresco.org/model/rule/1.0 | rule | Rule service model |
| http://www.alfresco.org/ws/service/authentication/1.0 | auth | Authentication Web Service |
| http://www.alfresco.org/ws/service/repository/1.0 | rep | Repository Web Service |
| http://www.alfresco.org/ws/service/content/1.0 | content | Content Web Service |
| http://www.alfresco.org/ws/service/authoring/1.0 | author | Authoring Web Service |
| http://www.alfresco.org/ws/service/classification/1.0 | cls | Classification Web Service |
| http://www.alfresco.org/ws/cml/1.0 | cml | Content Manipulation Language |
| http://www.alfresco.org/ws/model/content/1.0 | cm | Web Service Content Domain Model |
| http://www.alfresco.org/ws/headers/1.0 | hdr | SOAP Headers |
| http://www.alfresco.org/model/workflow/1.0 | wf | Workflow Model(link is to Alfresco simple workflow model, not generally extended) |
星期一, 2月 20, 2012
[Alfresco] How to create Java backed web script in alfresco (1)
1.accessing alfresco application services not available via JavaScript API
2.when the performance is absolutely critical
3.to get tighter control of the generated response.
4.when we need to access the Alfresco API
5.if we prefer stronger programming language like JAVA
透過上面的架構圖,我們可以知道需要準備四個元件
1.XML descriptor document (Describing the webscript controller)
2.Freemarker (Writing the response template-FTL)
3.Java class (Writing the webscript)
4.Spring bean XML(Register the Java class in the Alfresco)
接著準備來寫一個Java Backed Web Script吧~
[Alfresco] How to create Java backed web script in alfresco (2)
Reference:
Alfresco Help Java backed web script
Alfresco WiKi: Java backed web script samples
Alfresco Blog: Java-Backed Web Scripts
Web script article
星期一, 2月 06, 2012
[Alfresco] Boolean type is in FTL
在ftl檔案遇到boolean欄位一開始所遇到的錯誤
{
"shareDpxStatus": <#if person.properties.shareDpxStatus??>${person.properties.shareDpxStatus}<#else>null
}
原來ftl檔不支援直接將boolean型態印出來,要先轉換成字串型態
解法如下:
{
"shareDpxStatus": <#if person.properties.shareDpxStatus??>${person.properties.shareDpxStatus?string("true","false")}<#else>null
}
{
"shareDpxStatus": <#if person.properties.shareDpxStatus??>${person.properties.shareDpxStatus}<#else>null
}
原來ftl檔不支援直接將boolean型態印出來,要先轉換成字串型態
解法如下:
{
"shareDpxStatus": <#if person.properties.shareDpxStatus??>${person.properties.shareDpxStatus?string("true","false")}<#else>null
}
星期一, 1月 09, 2012
[Alfresco] Dot symbol in account
原本以為是alfresco不支援,結果是自已搞錯了XD
會發生以下錯誤!!
GET http://
UserName: ken.tsai
後端顯示找不到這個uid的錯誤,因為uid被截斷了
ERROR [extensions.webscripts.AbstractRuntime] Exception from executeScript - redirecting to status template error: 00100007 Web Script format 'tsai' is not registered
參考一下原本的範例程式,發現在webscript xml的描述檔把原本的
改為
就成功解決了。
後續的釐清程序之後再補
Reference:
Alfresco Web Scripts Wiki
星期二, 3月 22, 2011
[Alfresco] Create your first web script
if you want to create customize web script in Alfresco.
You will need to understand:
You will need to understand:
- XML for expressing the Web Script description
- <basename>.<httpmethod>.desc.xml
- Optionally, JavaScript for writing Web Script behaviour
- <basename>.<httpmethod>.js
- Freemarker for rendering a Web Script response
- <basename>.<httpmethod>.<format>.ftl
訂閱:
意見 (Atom)
