09-07-2012 11:40 AM
RemoteClient remote = new RemoteClient();
remote.setEndpoint("http");
remote.setRequestMethod(HttpMethod.POST);
remote.setRequestContentType("application/json");
remote.setTicket(ticket);
Map<String, String> properties = new HashMap<String, String>();
properties.put("shortName", siteShortName);
properties.put("sitePreset", sitePreset);
properties.put("title", siteTitle);
properties.put("description", siteDesc);
properties.put("visibility", "PUBLIC");
properties.put("ticket", ticket);
JSONObject json = new JSONObject(properties);
Response response = remote.call("http://localhost:8080/share/service/modules/create-site", json.toString());
if(clientJSON.ticket)
{
repoResponse = scriptRemoteConnector.post("/api/sites?alf_ticket="+clientJSON.ticket, clientRequest, "application/json");
}
else
{
repoResponse = scriptRemoteConnector.post("/api/sites", clientRequest, "application/json");
}
09-07-2012 05:57 PM
09-10-2012 04:17 AM
09-11-2012 01:14 PM
10-10-2012 01:59 AM
10-12-2012 04:00 PM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using httpsclient;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using System.Configuration;
namespace AlfrescoLoader
{
public partial class Form1 : Form
{
string BaseURI;
string JSESSIONID = "";
string Ticket = "";
public Form1()
{
InitializeComponent();
txtAlfrescoURL.Text = "http://" + ConfigurationSettings.AppSettings["DefaultURL"];
txtUsername.Text = ConfigurationSettings.AppSettings["DefaultUsername"];
txtPassword.Text = ConfigurationSettings.AppSettings["DefaultPassword"];
ddSiteVisibility.SelectedIndex = 0;
}
private Boolean AddSite(string Ticket, string SiteTitle, string SiteName, string SiteDescription, string SiteVisibility)
{
try
{
// Set the json that will define the site.
string MySite = @"{""visibility"":""" + SiteVisibility + @""",""sitePreset"":""site-dashboard"",""title"":""" +
SiteTitle + @""",""description"":""" + SiteDescription + @""",""shortName"":""" +
SiteName + @""",""ticket"":""" + Ticket + @"""}";
// set the url that we will call.
string uri = BaseURI + "/share/service/modules/create-site";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.ContentType = "application/json";
req.Accept = "application/json";
req.Method = "POST";
req.Headers["Cookie"] = "JSESSIONID=" + JSESSIONID; //set the cookie to contain the JSESSIONID we retrieved earlier.
using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(MySite);
}
}
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Stream stream = response.GetResponseStream();
string json = "";
using (StreamReader reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
json += reader.ReadLine();
}
}
stream.Close();
}
catch
{
return false;
}
return true;
}
private string GetLoginTicket(string Username, string Password)
{
string uri = BaseURI + "/alfresco/service/api/login";
string Params = @"{""username"":""" + Username + @""",""password"":""" + Password + @"""}";
CookieContainer cc = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.ContentType = "application/json";
req.CookieContainer = cc;
req.Method = "POST";
using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(Params);
}
}
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
if (res == null)
return "Login Failed!";
Stream stream = res.GetResponseStream();
string json = "";
using (StreamReader reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
json += reader.ReadLine();
}
}
res.Close();
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> datajson = (Dictionary<string, object>)serializer.DeserializeObject(json);
Dictionary<string, object> Ticketjson = (Dictionary<string, object>)datajson["data"];
string Ticket = Ticketjson["ticket"].ToString();
return Ticket;
}
private string DoLogin(string Username, string Password)
{
string uri = BaseURI + "/share/page/dologin";
string logintext = "username=" + Username + "&password=" + Password;
CookieContainer cc = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.ContentType = "application/x-www-form-urlencoded"; // Posting form data
req.CookieContainer = cc;
req.Method = "POST";
using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(logintext);
}
}
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
// Look through the returned cookies and find the one named "JSESSIONID"
foreach (Cookie cook in req.CookieContainer.GetCookies(req.RequestUri))
{
if (cook.Name.ToString() == "JSESSIONID")
return cook.Value.ToString();
}
return null;
}
private void btnLogin_Click(object sender, EventArgs e)
{
BaseURI = txtAlfrescoURL.Text; //This gets the base URL from a textbox on the form.
JSESSIONID = DoLogin(txtUsername.Text, txtPassword.Text); // This will retrieve the JSESSIONID
Ticket = GetLoginTicket(txtUsername.Text, txtPassword.Text); // This will get my login ticket.
// show a message on the form to indicate if I logged in successfully or not.
lblLoginStatus.Text = Ticket;
if (lblLoginStatus.Text.Substring(0, 6) != "TICKET")
{
lblLoginStatus.ForeColor = Color.Red;
}
else
{
lblLoginStatus.ForeColor = Color.Green;
lblLoginStatus.Text = "Login Successful! " + lblLoginStatus.Text;
}
}
private void btnCreateSite_Click(object sender, EventArgs e)
{
BaseURI = txtAlfrescoURL.Text;
if (Ticket.Substring(0, 6) != "TICKET")
{
lblCreateSiteResults.Text = "Not logged in!";
lblCreateSiteResults.ForeColor = Color.Red;
}
if (txtSiteTitle.Text.Length == 0 || txtSiteName.Text.Length == 0 || txtSiteDescription.Text.Length == 0)
{
lblCreateSiteResults.Text = "Must fill in Site Title, Name and Description!";
lblCreateSiteResults.ForeColor = Color.Red;
}
Boolean ReturnStatus = AddSite(Ticket, txtSiteTitle.Text, txtSiteName.Text, txtSiteDescription.Text, ddSiteVisibility.SelectedItem.ToString());
if (ReturnStatus)
{
lblCreateSiteResults.Text = "Site " + txtSiteName.Text + " created successfully!";
lblCreateSiteResults.ForeColor = Color.Green;
}
else
{
lblCreateSiteResults.Text = "Failed to Create Site";
lblCreateSiteResults.ForeColor = Color.Red;
}
}
}
}
03-06-2013 02:10 AM
07-08-2013 12:14 PM
07-16-2013 06:34 AM
08-12-2015 04:26 PM
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.