public RequestItem(String service, String operation, ISecurityToken token, BeanConverter converter) { this.service = service; this.operation = operation; this.token = token; this.converter = converter; }
public RestfulRequestItem(HttpRequest request, ISecurityToken token, BeanConverter converter) : base(GetServiceFromPath(request.RawUrl), GetMethod(request), token, converter) { url = request.RawUrl.Replace(SOCIAL_RESTFUL_PATH, "/"); parameters = CreateParameterMap(request); try { using (Stream istream = request.InputStream) { MemoryStream memoryStream = new MemoryStream(0x10000); byte[] buffer = new byte[0x1000]; int bytes; while ((bytes = istream.Read(buffer, 0, buffer.Length)) > 0) { memoryStream.Write(buffer, 0, bytes); } postData = request.ContentEncoding.GetString(memoryStream.ToArray()); } } catch (IOException e) { throw new Exception("Could not get the post data from the request", e); } }
public ApiController() { jsonConverter = new BeanJsonConverter(); xmlConverter = new BeanXmlConverter(); atomConverter = new BeanAtomConverter(); dispatcher = new StandardHandlerDispatcher( new PersonHandler(), new ActivityHandler(), new AppDataHandler(), new MessageHandler()); }
public void Copy(T source, V destination, BeanConverter <T, V> beanConverter) { if (beanConverter == null) { throw new ArgumentException("converter must not be null"); } Copy(source, destination); beanConverter.Convert(source, destination); }
public RpcRequestItem(JsonObject rpc, ISecurityToken token, BeanConverter converter) : base(getService(rpc["method"].ToString()), getOperation(rpc["method"].ToString()), token, converter) { if (rpc.Contains("params")) { data = rpc["params"] as JsonObject; } else { data = new JsonObject(); } }
/// <summary> /// Actual dispatch handling for servlet requests /// </summary> /// <param name="servletRequest"></param> /// <param name="servletResponse"></param> private void executeRequest(HttpRequest servletRequest, HttpResponse servletResponse) { setCharacterEncodings(servletRequest, servletResponse); ISecurityToken token = getSecurityToken(System.Web.HttpContext.Current, servletRequest.RawUrl); if (token == null) { sendSecurityError(servletResponse); return; } BeanConverter converter = getConverterForRequest(servletRequest); handleSingleRequest(servletRequest, servletResponse, token, converter); }
public RestfulRequestItem(String path, String method, String postData, ISecurityToken token, BeanConverter converter) : base(GetServiceFromPath(path), method, token, converter) { this.postData = postData; url = path; PutUrlParamsIntoParameters(); }
public RestfulRequestItem(String service, String method, ISecurityToken token, BeanConverter converter) : base(service, method, token, converter) { }
private BeanConverter getConverterForRequest(HttpRequest servletRequest) { String formatString = null; BeanConverter converter = null; String contentType = null; try { formatString = servletRequest.Params[FORMAT_PARAM]; } catch (Exception t) { // this happens while testing } try { contentType = servletRequest.ContentType; } catch (Exception t) { //this happens while testing //if (logger.isLoggable(Level.FINE)) { // logger.fine("Unexpected error : content type is null " + t.toString()); // } } if (!string.IsNullOrEmpty(contentType)) { if (ContentTypes.ALLOWED_JSON_CONTENT_TYPES.Contains(contentType)) { converter = jsonConverter; } else if (ContentTypes.ALLOWED_ATOM_CONTENT_TYPES.Contains(contentType)) { converter = atomConverter; } else if (ContentTypes.ALLOWED_XML_CONTENT_TYPES.Contains(contentType)) { converter = xmlConverter; } else if (formatString == null) { // takes care of cases where content!= null but is "" converter = jsonConverter; } } else if (!string.IsNullOrEmpty(formatString)) { if (formatString.Equals(ATOM_FORMAT)) { converter = atomConverter; } else if (formatString.Equals(XML_FORMAT)) { converter = xmlConverter; } else { converter = jsonConverter; } } else { converter = jsonConverter; } return(converter); }
private void handleSingleRequest(HttpRequest servletRequest, HttpResponse servletResponse, ISecurityToken token, BeanConverter converter) { RestfulRequestItem requestItem = new RestfulRequestItem(servletRequest, token, converter); var asyncResult = HandleRequestItem(requestItem); // handle just the one case where we can't find a handler if (asyncResult == null) { sendError(servletResponse, new ResponseItem((int)HttpStatusCode.InternalServerError, "The service " + requestItem.getService() + " is not implemented")); return; } ResponseItem responseItem = getResponseItem(asyncResult); servletResponse.ContentType = converter.GetContentType(); if (responseItem.getErrorCode() >= 200 && responseItem.getErrorCode() < 400) { Object resp = responseItem.getResponse(); // put single object responses into restfulcollection if (!(resp is DataCollection) && !(resp is IRestfulCollection)) { switch (requestItem.getService()) { case IHandlerDispatcher.ACTIVITY_ROUTE: if (resp is Activity) { resp = new RestfulCollection <Activity>((Activity)resp); } break; case IHandlerDispatcher.PEOPLE_ROUTE: if (resp is Person) { resp = new RestfulCollection <Person>((Person)resp); } break; case IHandlerDispatcher.APPDATA_ROUTE: if (requestItem.getOperation() != "POST") { resp = new DataCollection(new Dictionary <string, Dictionary <string, string> > { { "entry", (Dictionary <string, string>)resp } }); } break; default: resp = new Dictionary <string, object> { { "entry", resp } }; break; } } servletResponse.Output.Write(converter.ConvertToString(resp, requestItem)); } else { sendError(servletResponse, responseItem); } }
public void Copy(User source, UserInfo destination, BeanConverter <User, UserInfo> beanConverter) { destination.Name = source.Name; beanConverter.Convert(source, destination); }
private void handleSingleRequest(HttpRequest servletRequest, HttpResponse servletResponse, ISecurityToken token, BeanConverter converter) { RestfulRequestItem requestItem = new RestfulRequestItem(servletRequest, token, converter); var asyncResult = HandleRequestItem(requestItem); // handle just the one case where we can't find a handler if (asyncResult == null) { sendError(servletResponse, new ResponseItem((int)HttpStatusCode.InternalServerError, "The service " + requestItem.getService() + " is not implemented")); return; } ResponseItem responseItem = getResponseItem(asyncResult); servletResponse.ContentType = converter.GetContentType(); if (responseItem.getErrorCode() >= 200 && responseItem.getErrorCode() < 400) { Object resp = responseItem.getResponse(); // put single object responses into restfulcollection if (!(resp is DataCollection) && !(resp is IRestfulCollection)) { switch (requestItem.getService()) { case IHandlerDispatcher.ACTIVITY_ROUTE: if (resp is Activity) { resp = new RestfulCollection<Activity>((Activity)resp); } break; case IHandlerDispatcher.PEOPLE_ROUTE: if (resp is Person) { resp = new RestfulCollection<Person>((Person)resp); } break; case IHandlerDispatcher.APPDATA_ROUTE: if (requestItem.getOperation() != "POST") resp = new DataCollection(new Dictionary<string, Dictionary<string, string>> { { "entry", (Dictionary<string, string>)resp } }); break; default: resp = new Dictionary<string, object> { { "entry", resp } }; break; } } servletResponse.Output.Write(converter.ConvertToString(resp, requestItem)); } else { sendError(servletResponse, responseItem); } }