Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Restful Datasource with url parameter replacement

    Hi all!

    I'm new to the SmartClient and to SmartGWT, and I don't know how obvious my information is, but for I needed to do a lot of research to get a good solution, and take a piece off the puzzle here and there to accomplish my objective, and I want to contribute.
    In my project, we are using a Restful architecture, and I extended the RestDataSource setting all the properties to use GET, PUT, POST and DELETE verbs, and to do parameter replacement in case like http://server/resources/order/{id}/orderitem{id}.

    The code follows:

    Code:
    public class RestfulDataSource extends RestDataSource {
    
        private OperationBinding[] operationBindings;
    
        public RestfulDataSource(String id, String fetchUrl, String addUrl, String updateUrl, String removeUrl) {
            super();
            this.setID(id);
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Accept", "text/xml");
    
            setDataFormat(DSDataFormat.XML);
            setJsonRecordXPath("data");
    
    
            OperationBinding fetch = new OperationBinding();
            fetch.setOperationType(DSOperationType.FETCH);
            fetch.setDataProtocol(DSProtocol.GETPARAMS);
            fetch.setDataURL(fetchUrl);
            DSRequest fetchRequest = new DSRequest();
            fetchRequest.setHttpHeaders(headers);
            fetch.setRequestProperties(fetchRequest);
    
            OperationBinding add = new OperationBinding();
            add.setOperationType(DSOperationType.ADD);
            add.setDataProtocol(DSProtocol.POSTMESSAGE);
            add.setDataURL(addUrl);
            DSRequest addRequest = new DSRequest();
            addRequest.setHttpHeaders(headers);
            add.setRequestProperties(fetchRequest);
    
            OperationBinding update = new OperationBinding();
            update.setOperationType(DSOperationType.UPDATE);
            update.setDataProtocol(DSProtocol.GETPARAMS);
            DSRequest updateRequest = new DSRequest();
            updateRequest.setHttpMethod("PUT");
            updateRequest.setHttpHeaders(headers);
            update.setRequestProperties(updateRequest);
            update.setDataURL(updateUrl);
    
            OperationBinding remove = new OperationBinding();
            remove.setOperationType(DSOperationType.REMOVE);
            remove.setDataProtocol(DSProtocol.GETPARAMS);
            DSRequest removeRequest = new DSRequest();
            removeRequest.setHttpMethod("DELETE");
            removeRequest.setHttpHeaders(headers);
            remove.setRequestProperties(removeRequest);
            remove.setDataURL(removeUrl);
    
            operationBindings = new OperationBinding[]{fetch, add, update, remove};
    
            this.setOperationBindings(operationBindings);
        }
    
        
        @Override
        protected Object transformRequest(DSRequest dsRequest) {
    
            for (OperationBinding operationBinding : operationBindings) {
                if (dsRequest.getOperationType().equals(operationBinding.getOperationType())) {
                    String url = operationBinding.getDataURL();
                    int start = url.indexOf('{');
                    int end = url.indexOf('}');
                    while (start != -1 && end != -1) {
                        String parametro = url.substring(start + 1, end);
                        url = url.replace("{" + parametro + "}",
                                JSOHelper.getAttribute(dsRequest.getData(), parametro));
                        start = url.indexOf('{');
                        end = url.indexOf('}');
                    }
                    dsRequest.setActionURL(url);
                    return super.transformRequest(dsRequest);
                }
            }
    
            return super.transformRequest(dsRequest);
        }
    }
    Please, if you know how to do this better, send the code and show how miserable I am.

    Thanks!

    #2
    I have to do the same thing and I have recently found that gwt includes or is gonna include RegExp support.

    Have a look at
    http://code.google.com/p/google-web-toolkit/issues/detail?id=1727
    http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/#src/com/google/gwt/regexp/

    Comment


      #3
      I have been watching the following internet draft with an intention to add support to DataSource in the future: URI Template. The intent will be to expand the template based on the record contents automatically.

      Comment

      Working...
      X