option1:How to create a share site using the REST API ?
option2:The code is described as the following.I don't build it . XD
String alf_ticket = getAlfTicket("admin", "admin");
JSONObject site = new JSONObject();
site.put("sitePreset", "site-dashboard");
site.put("shortName", shortName);
site.put("title", "myTitle"+shortName.substring(0,4));
site.put("description", "myDescription");
site.put("visibility", ScriptSiteService.PUBLIC_SITE);
site.put("ticket", alf_ticket);
//client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(new AuthScope("localhost", 8080), new UsernamePasswordCredentials("admin", "admin"));
//post = new PostMethod("http://localhost:8080/share/service/modules/create-site?alf_ticket="+alf_ticket);
//post = new PostMethod("http://localhost:8080/alfresco/s/api/sites?alf_ticket="+alf_ticket);
post = new PostMethod("http://localhost:8080/share/service/modules/create-site?ticket="+alf_ticket);//Working one
//post = new PostMethod("http://localhost:8080/share/service/modules/create-site?alf_ticket="+alf_ticket);
//post = new GetMethod("http://localhost:8080/share/page/modules/create-site");
post.setDoAuthentication(true);
post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
post.setRequestHeader("Accept-Charset", "ISO-8859-1,UTF-8");
post.setRequestHeader("Accept-Language", "en");
post.setRequestHeader("Content_Type", "application/json");
//post.addParameter("alf_ticket", alf_ticket);
System.out.println("Authorization Header Before >>>"+post.getRequestHeader("Authorization"));
String userid = "admin";
String password = "admin";
String stringUserIdPassword = userid + ":" + password;
byte[] byteUserIdPassword = stringUserIdPassword.getBytes( "ASCII" );
byte[] base64UserIdPassword = new Base64().encode( byteUserIdPassword );
post.setRequestHeader("Authorization", "BASIC "+base64UserIdPassword.toString());
System.out.println("Authorization Header After >>>"+post.getRequestHeader("Authorization"));
post.setRequestEntity(new StringRequestEntity(site.toString(),"application/json", "UTF-8"));
RequestEntity reqEnt = post.getRequestEntity();
reqEnt.writeRequest(System.out);
System.out.println();
System.out.println("Request Entity>>> "+reqEnt.getContentType());
Header[] head = post.getRequestHeaders();
for(Header hea : head)
{
System.out.println("Headers >>>"+hea.toString());
}
statusCode = client.executeMethod(post);
System.out.println("Status Text>>>"+HttpStatus.getStatusText(statusCode));
System.out.println("ResponseBody json"+post.getResponseBodyAsString());
post.releaseConnection();
public String getAlfTicket(String _userName, String _password)
{
String _ticket = "";
URL url;
HttpURLConnection connection = null;
try
{
String urlParameters = "{ \"username\" : \"" + _userName +"\", \"password\" : \"" + _password +"\" }";
// Create connection
url = new URL("http://localhost:8080/alfresco/service/api/login");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
// Send request
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
// Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null)
{
response.append(line);
response.append('\r');
}
rd.close();
String _jsonResponse = response.toString();
org.json.simple.JSONObject _jsonResponseObject = (org.json.simple.JSONObject) new JSONParser().parse(_jsonResponse);
org.json.simple.JSONObject jsonDataObject = (org.json.simple.JSONObject)new JSONParser().parse(_jsonResponseObject.get("data").toString());
_ticket = jsonDataObject.get("ticket").toString();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
finally
{
if (connection != null)
{
connection.disconnect();
}
}
return _ticket;
}
Reference:
Http post method Example
沒有留言:
張貼留言
留個話吧:)