04-19-2011 09:39 AM
public class MyApplet extends Applet //implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
public void start(){
getUrl();
String response = authorization("admin","123");
String ticket = getTicket( response );
uploadDocs( ticket );
}
private String getTicket(String authResp){
System.out.println(authResp);
int indxLast=authResp.lastIndexOf('"');
String strTic = "TICKET";
int indxFirst=authResp.lastIndexOf('_')+1;
String strTicket = authResp.substring(indxFirst,indxLast);
System.out.println(strTicket);
return strTicket;
}
public String uploadDocs( String ticket ){
String urlString= "http://localhost:8080/alfresco/service/api/upload";
String fileId = "413f2b83-6fa3-454e-bc01-82473225e2e5";
URL url;
HttpURLConnection connection = null;
try
{
String urlParameters = "{ \"ticket\" : \""+ticket+"\", \"protocol\" : \"workspace\", \"upload-id\" : \"" + fileId +"\", \"man_assigned_id\" : \"myFistCont\" }";
url = new URL(urlString);
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.setRequestProperty("ticket", ticket);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
// Send request
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.writeBytes("My content");
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();
System.out.println(_jsonResponse);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
finally
{
if (connection != null)
{
connection.disconnect();
}
}
return "";
}
public String authorization(String _userName, String _password)
{
URL url;
HttpURLConnection connection = null;
String _jsonResponse;
try
{
String urlParameters = "{ \"username\" : \"" + _userName +"\", \"password\" : \"" + _password +"\" }";
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);
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();
_jsonResponse = response.toString();
System.out.println(_jsonResponse);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
finally
{
if (connection != null)
{
connection.disconnect();
}
}
return _jsonResponse;
}
private String getTicket(String authResp){
System.out.println(authResp);
int indxLast=authResp.lastIndexOf('"');
String strTic = "TICKET";
int indxFirst=authResp.lastIndexOf('_')+1;
String strTicket = authResp.substring(indxFirst,indxLast);
System.out.println(strTicket);
return strTicket;
}
}
05-13-2011 04:51 AM
05-13-2011 04:52 AM
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.