11-20-2008 12:21 PM
01-23-2009 03:58 AM
01-23-2009 10:34 AM
01-27-2009 02:48 AM
05-20-2009 05:19 AM
07-09-2009 05:14 AM
07-27-2009 05:12 PM
07-28-2009 01:20 AM
12-12-2009 11:04 AM
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
public class CreateFolder {
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
String host = "192.168.1.1";
int port = 8080;
String username = "admin";
String password = "admin";
String parentFolder = "Company Home";
String folderName = "sales3";
String description = "sales space3";
String url = "http://" + host + ":" + port + "/alfresco/service/api/path/workspace/SpacesStore/" + parentFolder + "/children";
String contentType = "application/atom+xml;type=entry";
String xml =
"<?xml version='1.0' encoding='utf-8'?>\n" +
"<entry xmlns='http://www.w3.org/2005/Atom' xmlns:cmis='http://www.cmis.org/2008/05'>\n" +
"<title>" + folderName + "</title>\n" +
"<summary>" + description + "</summary>\n" +
"<cmis:object>\n"+
"<cmis:properties>\n" +
"<cmis:propertyString cmis:name='ObjectTypeId'>\n" +
"<cmis:value>folder</cmis:value>\n" +
"</cmis:propertyString>\n" +
"</cmis:properties>\n" +
"</cmis:object>\n" +
"</entry>\n";
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(host, port),
new UsernamePasswordCredentials(username, password));
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Content-type", contentType);
StringEntity requestEntity = new StringEntity(xml, "UTF-8");
httppost.setEntity(requestEntity);
System.out.println("executing request" + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.println("—————————————-");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content type: " + entity.getContentType());
long contentLength = entity.getContentLength();
System.out.println("Response content length: "
+ entity.getContentLength());
if (contentLength > 0) {
byte [] b = new byte[(int) contentLength];
entity.getContent().read(b);
System.out.println("Response content: " + new String(b));
}
entity.writeTo(System.out);
}
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
long end = System.currentTimeMillis();
System.out.println("Time spend: " + (end-start) + "ms");
}
}
12-12-2009 11:08 AM
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Collection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import sun.misc.BASE64Encoder;
import eu.medsea.mimeutil.MimeType;
import eu.medsea.mimeutil.MimeUtil;
import eu.medsea.mimeutil.MimeUtil2;
/**
* A simple example that uses HttpClient to execute an HTTP request against a
* target site that requires user authentication.
*/
public class UploadDocument {
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
String host = "192.168.21.111";
int port = 8080;
String username = "admin";
String password = "admin";
String parentFolder = "Company Home";
String filePath = "test.pdf";
String description = "PDF upload test";
String author = "admin";
String url = "http://" + host + ":" + port + "/alfresco/service/api/path/workspace/SpacesStore/" + parentFolder + "/children";
String contentType = "application/atom+xml;type=entry";
BASE64Encoder encoder = new BASE64Encoder();
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);
// Create the byte array to hold the data
byte[] bytes = new byte[(int) file.length()];
dis.readFully(bytes);
Collection<MimeType> mimeTypes = MimeUtil.getMimeTypes(file);
MimeType mimeType = MimeUtil.getMostSpecificMimeType(mimeTypes);
if(mimeType == null) mimeType = MimeUtil2.UNKNOWN_MIME_TYPE;
String xml =
"<?xml version='1.0' encoding='utf-8'?>\n" +
"<entry xmlns='http://www.w3.org/2005/Atom' xmlns:cmis='http://www.cmis.org/2008/05'>\n" +
"<title>" + file.getName() + "</title>\n" +
"<summary>" + description + "</summary>\n" +
"<author>" + author + "</author>\n" +
"<content type='" + mimeType.toString() + "'>" + encoder.encode(bytes) + "</content>\n" +
"<cmis:object>\n"+
"<cmis:properties>\n" +
"<cmis:propertyString cmis:name='ObjectTypeId'>\n" +
"<cmis:value>document</cmis:value>\n" +
"</cmis:propertyString>\n" +
"</cmis:properties>\n" +
"</cmis:object>\n" +
"</entry>\n";
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(host, port),
new UsernamePasswordCredentials(username, password));
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Content-type", contentType);
StringEntity requestEntity = new StringEntity(xml, "UTF-8");
httppost.setEntity(requestEntity);
System.out.println("executing request" + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.println("—————————————-");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content type: " + entity.getContentType());
long contentLength = entity.getContentLength();
System.out.println("Response content length: "
+ entity.getContentLength());
if (contentLength > 0) {
byte [] b = new byte[(int) contentLength];
entity.getContent().read(b);
System.out.println("Response content: " + new String(b));
}
entity.writeTo(System.out);
}
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
long end = System.currentTimeMillis();
System.out.println("Time spend: " + (end-start) + "ms");
}
}
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.