private Stream serializeUserToXMLStream(User user, HttpStatusCode code) { // create a user serializer XmlSerializer serializer = new XmlSerializer(typeof(User)); // create a user serializer stream MemoryStream stream = new MemoryStream(); // encode the xml string using utf-8 XmlTextWriter xmlTextWriter = new XmlTextWriter(stream, new System.Text.UTF8Encoding(false)); // serialize the user to the stream //serializer.Serialize(stream, user); serializer.Serialize(xmlTextWriter, user); // retrieve a utf-8 formated user byte[] bytes = stream.GetBuffer(); // set the content type to xml WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml"; // set the responses http status WebOperationContext.Current.OutgoingResponse.StatusCode = code; // return the utf-8 user stream return new MemoryStream(bytes); }
public UserService() { // create a test user User user = new User(); user.userName = "******"; user.externalId = "mcrooke"; user.displayName = "Mr Matt Crooke"; // set the user names user.name = new Name(); user.name.givenName = "Matt"; user.name.familyName = "Crooke"; user.name.formatted = "Mr Matt Crooke"; // set the user emails PluralAttribute email = new PluralAttribute(); email.primary = true; email.value = "*****@*****.**"; PluralAttribute[] emails = new PluralAttribute[] { email }; user.emails = emails; // retrieve the users cache manager if (usersCacheManager == null) usersCacheManager = CacheFactory.GetCacheManager(USERS_CACHE); // populate the id with the local id user.id = "uid=" + user.externalId + ",dc=hackerypokery,dc=com"; // store the user in the cache usersCacheManager.Add(user.id, user); }
public HttpWebResponse createUser(User user, string requestType, string responseType) { try { // create a request to create the user Uri uri = new Uri(url + "/User"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri) as HttpWebRequest; // set the request & response media types request.ContentType = requestType; request.Accept = responseType; // set the request method request.Method = "POST"; // get the request stream to write the user Stream stream = request.GetRequestStream(); // create a user serializer XmlSerializer serializer = new XmlSerializer(typeof(User)); // serialize the user object to xml serializer.Serialize(stream, user); // send the create user request to the rest resource HttpWebResponse response = (HttpWebResponse)request.GetResponse(); return response; } catch (WebException wException) { // get the response from the exception HttpWebResponse response = (HttpWebResponse)wException.Response; // return the error response return response; } }
public HttpWebResponse createUser(User user) { return createUser(user, APPLICATION_XML_TYPE, APPLICATION_XML_TYPE); }
public Stream updateUserAsJSON(string uid, User user) { // retrieve the users cache manager if (usersCacheManager == null) usersCacheManager = CacheFactory.GetCacheManager(USERS_CACHE); // check a usersCacheManager is configured if (usersCacheManager == null) { // usersCacheManager not configured //logger.error("cache manager not configured"); // build an error message Error error = new Error(); error.code = System.Net.HttpStatusCode.InternalServerError.ToString(); error.description = "cache manager not configured"; // return a server error return serializeErrorToJSONStream(error, System.Net.HttpStatusCode.InternalServerError); } // check if the user is cached if (usersCacheManager.Contains(uid)) { // remove the retrieved user usersCacheManager.Remove(uid); // update the with the new user usersCacheManager.Add(user.id, user); // return the user with the ok status return serializeUserToJSONStream(user, System.Net.HttpStatusCode.OK); } else { // user not found, build an error message Error error = new Error(); error.code = System.Net.HttpStatusCode.NotFound.ToString(); error.description = "user not found"; // user not found, return an error message return serializeErrorToJSONStream(error, System.Net.HttpStatusCode.NotFound); } }
public Stream updateUser(string uid, User user) { return updateUserAsXML(uid, user); }
public Stream createUserAsJSON(User user) { // retrieve the users cache manager if (usersCacheManager == null) usersCacheManager = CacheFactory.GetCacheManager(USERS_CACHE); // check a usersCacheManager is configured if (usersCacheManager == null) { // usersCacheManager not configured //logger.error("cache manager not configured"); // build an error message Error error = new Error(); error.code = System.Net.HttpStatusCode.InternalServerError.ToString(); error.description = "cache manager not configured"; // return a server error return serializeErrorToJSONStream(error, System.Net.HttpStatusCode.InternalServerError); } // populate the id with the local id user.id = "uid=" + user.externalId + ",dc=hackerypokery,dc=com"; // store the user in the cache usersCacheManager.Add(user.id, user); // return the user with the created status return serializeUserToJSONStream(user, System.Net.HttpStatusCode.Created); }
public Stream createUser(User user) { return createUserAsXML(user); }
private Stream serializeUserToJSONStream(User user, HttpStatusCode code) { // create a user serializer DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(User)); // create a user serializer stream MemoryStream stream = new MemoryStream(); // serialize the user to the stream serializer.WriteObject(stream, user); // retrieve a utf-8 formated user byte[] bytes = stream.GetBuffer(); // set the content type to xml WebOperationContext.Current.OutgoingResponse.ContentType = "application/json"; // set the responses http status WebOperationContext.Current.OutgoingResponse.StatusCode = code; // return the utf-8 user stream return new MemoryStream(bytes); }
public static String createUserTest(Client client) { // create a test user User user = new User(); user.userName = "******"; user.externalId = "mcrooke"; user.displayName = "Mr Matt Crooke"; // set the user names user.name = new Name(); user.name.givenName = "Matt"; user.name.familyName = "Crooke"; user.name.formatted = "Mr Matt Crooke"; // set the user emails PluralAttribute email = new PluralAttribute(); email.primary = true; email.value = "*****@*****.**"; PluralAttribute[] emails = new PluralAttribute[] { email }; user.emails = emails; // send the request to create the user HttpWebResponse response = client.createUser(user); // verify the response code indicates success //int status = response.getStatusCode(); //assert(status == 201); // retrieve the location of the user //String location = response.getHeaders().getFirst("Location"); // build a nide reader from the response XmlNodeReader xmlNodeReader = client.buildReaderFromResponse(response); try { // get the user object from the response user = client.getUserFromResponse(xmlNodeReader); Console.WriteLine("created user - " + user.externalId + " as " + user.id); return user.id; } catch (InvalidOperationException ioException) { // InvalidOperationException is throw when a user cannot be deserialized // get the error object from the response Error error = client.getErrorFromResponse(xmlNodeReader); Console.WriteLine("user cannot be created, error returned - " + error.code + " " + error.description); return null; } /* // get the response stream to read the user Stream stream = response.GetResponseStream(); // create a user deserializer XmlSerializer deserializer = new XmlSerializer(typeof(User)); // retrieve the returned user entry user = (User)deserializer.Deserialize(stream); // retrieve the id of the user String id = user.id; Console.WriteLine("created user - " + user.externalId + " as " + user.id); return id; */ }