Last active
December 9, 2016 17:30
-
-
Save filosganga/3902429 to your computer and use it in GitHub Desktop.
GWT RPC Spring Scaffolding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.filippodeluca.gwt.examples.contacts; | |
import com.google.gwt.user.client.rpc.IsSerializable; | |
public class Contact implements IsSerializable { | |
private String name; | |
private String email; | |
public Contact(String name, String email) { | |
this.name = name; | |
this.email = email; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getEmail() { | |
return email; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.filippodeluca.gwt.examples.contacts; | |
import com.google.gwt.user.client.rpc.RemoteService; | |
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; | |
import java.util.ArrayList; | |
@RemoteServiceRelativePath("ContactService") | |
public interface ContactService extends RemoteService { | |
ArrayList<Contact> findByName(String name); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.filippodeluca.gwt.examples.contacts; | |
import com.google.gwt.user.client.rpc.AsyncCallback; | |
import java.util.ArrayList; | |
public interface ContactServiceAsync extends RemoteService { | |
void findByName(String name, AsyncCallback<ArrayList<Contact>> callback); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.filippodeluca.gwt.examples.contacts; | |
import org.filippodeluca.gwt.rpc.GWTService; | |
import org.springframework.stereotype.Service; | |
import org.springframework.transaction.annotation.Transactional; | |
import java.util.ArrayList; | |
/** | |
* @author Filippo De Luca | |
*/ | |
@Service("contactService") | |
public class ContactServiceImpl extends GWTService implements ContactService { | |
@Override | |
@Transactional(readOnly = true) | |
public ArrayList<Contact> findByName(String name) { | |
// Implementation calling the DB or whatever do you want | |
return new ArrayList<Contact>(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.filippodeluca.gwt.examples.contacts; | |
import org.filippodeluca.gwt.rpc.GWTServiceServlet; | |
import java.util.ArrayList; | |
/** | |
* @author Filippo De Luca | |
*/ | |
public class ContactServiceServlet extends GWTServiceServlet implements ContactService { | |
private ContactService delegate; | |
@Override | |
public void init(ServletConfig config) throws ServletException { | |
super.init(config); | |
delegate = getRequiredWebApplicationContext(config.getServletContext()).getBean(ContactService.class); | |
} | |
@Override | |
public ArrayList<Contact> findByName(String name) { | |
// Call the delegate implementation | |
return delegate.findByName(name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.filippodeluca.gwt.rpc; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
/** | |
* Base class to develop a GWT service. | |
* Exposes the Request and Response to the subclasses. | |
* | |
* @author Filippo De Luca | |
*/ | |
public abstract class GWTService { | |
/** | |
* Return the current HTTP request. | |
* @return | |
*/ | |
protected HttpServletRequest currentRequest() { | |
return GWTThreadLocals.instance().getRequest(); | |
} | |
protected HttpServletResponse currentResponse() { | |
return GWTThreadLocals.instance().getResponse(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.filippodeluca.gwt.rpc; | |
/** | |
* It set the GWT thread locals to be available from the services. | |
* | |
* @author Filippo De Luca | |
*/ | |
public abstract class GWTServiceServlet extends RemoteServiceServlet { | |
@Override | |
protected void onBeforeRequestDeserialized(String serializedRequest) { | |
super.onBeforeRequestDeserialized(serializedRequest); | |
setGwtThreadLocals(); | |
} | |
protected void setGwtThreadLocals() { | |
GWTThreadLocals.instance().setRequest(getThreadLocalRequest()); | |
GWTThreadLocals.instance().setResponse(getThreadLocalResponse()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.filippodeluca.gwt.rpc; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
/** | |
* Singleton class to pass Request and response from servlet to service. | |
* | |
* @author Filippo De Luca | |
*/ | |
public final class GWTThreadLocals { | |
private static final GWTThreadLocals INSTANCE = new GWTThreadLocals(); | |
protected ThreadLocal<HttpServletRequest> perThreadRequest; | |
protected ThreadLocal<HttpServletResponse> perThreadResponse; | |
private GWTThreadLocals() { | |
perThreadRequest = new ThreadLocal<HttpServletRequest>(); | |
perThreadResponse = new ThreadLocal<HttpServletResponse>(); | |
} | |
public static GWTThreadLocals instance() { | |
return INSTANCE; | |
} | |
public void setRequest(HttpServletRequest request) { | |
perThreadRequest.set(request); | |
} | |
public HttpServletRequest getRequest() { | |
return perThreadRequest.get(); | |
} | |
public void setResponse(HttpServletResponse response) { | |
perThreadResponse.set(response); | |
} | |
public HttpServletResponse getResponse() { | |
return perThreadResponse.get(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ContactServiceAsync.java:
void findByName(String name, AsyncCallback"<"ArrayList callback); ??
"AsyncCallback<" (>?)