diff --git a/.gitignore b/.gitignore index 60d13ec00b..3d4058950e 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ bin /helloworld-jsp README.html CONTRIBUTING.html +.errai diff --git a/helloworld-errai/README.md b/helloworld-errai/README.md new file mode 100644 index 0000000000..0873a0de75 --- /dev/null +++ b/helloworld-errai/README.md @@ -0,0 +1,73 @@ +helloworld-errai: Hello World Using the Errai Framework +======================================================= + +What is it? +----------- + +This example demonstrates the use of *CDI 1.0* and *JAX-RS* in *JBoss AS 7* with a GWT front-end client. +GWT is basically a typesafe, statically checked programming model for producing HTML5+CSS3+JavaScript +front-ends. In this example, we use RESTful services on the backend. The client communicates with the +backend using stubs that are generated based on the JAX-RS resources when the application is compiled. + +You can test the REST endpoint at the URL http://localhost:8080/jboss-as-helloworld-errai/hello/json/David + + +System requirements +------------------- + +All you need to build this project is Java 6.0 (Java SDK 1.6) or better, Maven +3.0 or better. + +The application this project produces is designed to be run on a JBoss AS 7. + +NOTE: +This project retrieves artifacts from the JBoss Community Maven repository, a +superset of the Maven central repository. + +With the prerequisites out of the way, you're ready to build and deploy. + + +Deploying the Application +------------------------- + +First you need to start JBoss AS 7. To do this, run + + $JBOSS_HOME/bin/standalone.sh + +or if you are using windows + + $JBOSS_HOME/bin/standalone.bat + +To deploy the application, you first need to produce the archive to deploy using +the following Maven goal: + + mvn clean package + +You can now deploy the artifact to JBoss AS by executing the following command: + + mvn jboss-as:deploy + +This will deploy `target/jboss-as-helloworld-errai.war`. + +The application will be running at the following URL . + +To undeploy from JBoss AS, run this command: + + mvn jboss-as:undeploy + +You can also start JBoss AS 7 and deploy the project using Eclipse. See the JBoss AS 7 +Getting Started Guide for Developers for more information. + + +Running the Application in GWT Dev Mode +--------------------------------------- + +GWT Dev Mode provides an edit-save-refresh development experience. If you plan to try +modifying this demo, we recommend you start the application in Dev Mode so you don't +have to repackage the whole application every time you change it. + +Deploy the war file and start JBoss AS 7 as described above. + +Then execute the command: + + mvn gwt:run diff --git a/helloworld-errai/pom.xml b/helloworld-errai/pom.xml new file mode 100644 index 0000000000..0dd08dde69 --- /dev/null +++ b/helloworld-errai/pom.xml @@ -0,0 +1,186 @@ + + + 4.0.0 + + org.jboss.as.quickstarts + jboss-as-helloworld-errai + 7.0.2-SNAPSHOT + war + JBoss AS Quickstarts: Errai Hello World + + http://jboss.org/jbossas + + + Apache License, Version 2.0 + repo + http://www.apache.org/licenses/LICENSE-2.0.html + + + + + + + + UTF-8 + + + + + + org.jboss.bom + jboss-javaee-6.0-with-errai + 1.0.0.M4 + pom + import + + + + + + + + + javax.enterprise + cdi-api + provided + + + + + org.jboss.spec.javax.annotation + jboss-annotations-api_1.1_spec + provided + + + + + com.google.gwt + gwt-user + provided + + + + + + + org.jboss.errai + errai-bus + + + + org.jboss.errai + errai-ioc + + + + org.jboss.errai + errai-tools + + + + org.jboss.errai + errai-jaxrs-client + provided + + + + org.jboss.errai + errai-jaxrs-provider + + + + org.jboss.spec.javax.ws.rs + jboss-jaxrs-api_1.1_spec + provided + + + + + + ${project.artifactId} + + + maven-war-plugin + 2.1.1 + + + **/client/local/**/*.class + + + + + + org.jboss.as.plugins + jboss-as-maven-plugin + 7.1.0.CR1 + + + + + maven-compiler-plugin + 2.3.1 + + 1.6 + 1.6 + + + + + + org.codehaus.mojo + gwt-maven-plugin + 2.4.0 + + true + INFO + -Xmx512m + + true + http://localhost:8080/jboss-as-helloworld-errai/HelloWorldApp.html + + + + gwt-compile + + compile + + + + gwt-clean + clean + + clean + + + + + + org.apache.maven.plugins + maven-clean-plugin + 2.4.1 + + + + ${basedir} + + www-test/** + .gwt/** + .errai/** + war/WEB-INF/deploy/** + war/WEB-INF/lib/** + src/main/webapp/WEB-INF/deploy/** + **/gwt-unitCache/** + **/*.JUnit/** + + + + + + + + diff --git a/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/HelloWorldApp.gwt.xml b/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/HelloWorldApp.gwt.xml new file mode 100644 index 0000000000..fc5dd78dbb --- /dev/null +++ b/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/HelloWorldApp.gwt.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + diff --git a/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/client/local/HelloWorldApp.java b/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/client/local/HelloWorldApp.java new file mode 100644 index 0000000000..da1a6978dd --- /dev/null +++ b/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/client/local/HelloWorldApp.java @@ -0,0 +1,51 @@ +package org.jboss.as.quickstarts.erraihelloworld.client.local; + +import javax.annotation.PostConstruct; +import javax.inject.Inject; + +import org.jboss.as.quickstarts.erraihelloworld.client.shared.HelloWorldResource; +import org.jboss.errai.ioc.client.api.Caller; +import org.jboss.errai.ioc.client.api.EntryPoint; + +import com.google.gwt.user.client.ui.RootPanel; + +/** + * This is the entry point to the client portion of the web application. At + * compile time, Errai finds the {@code @EntryPoint} annotation on this class + * and generates bootstrap code that creates an instance of this class when the + * page loads. This client-side bootstrap code will also call the + * {@link #init()} method because it is annotated with the + * {@code @PostConstruct} CDI annotation. + * + * @author Jonathan Fuerth + * @author Christian Sadilek + */ +@EntryPoint +public class HelloWorldApp { + + /** + * Errai's JAX-RS module generates a stub class that makes AJAX calls back to + * the server for each resource method on the {@link HelloWorldResource} + * interface. The paths and HTTP methods for the AJAX calls are determined + * automatically based on the JAX-RS annotations ({@code @Path}, {@code @GET}, + * {@code @POST}, and so on) on the resource. + *

+ * You can create additional JAX-RS proxies by following the same pattern + * ({@code @Inject Caller}) with your own JAX-RS resource + * classes. + */ + @Inject + private Caller helloWorldCaller; + + /** + * This method creates an instance of the UiBinder UI {@link HelloWorldClient} + * and attaches it to the RootPanel, the top-level DOM node that is an + * ancestor to all GWT widgets on the page (the {@code } element of the + * HTML document). + */ + @PostConstruct + public void init() { + RootPanel.get().add(new HelloWorldClient(helloWorldCaller).getElement()); + } + +} diff --git a/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/client/local/HelloWorldClient.java b/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/client/local/HelloWorldClient.java new file mode 100644 index 0000000000..69632a7b93 --- /dev/null +++ b/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/client/local/HelloWorldClient.java @@ -0,0 +1,96 @@ +package org.jboss.as.quickstarts.erraihelloworld.client.local; + +import org.jboss.as.quickstarts.erraihelloworld.client.shared.HelloWorldResource; +import org.jboss.errai.bus.client.api.ErrorCallback; +import org.jboss.errai.bus.client.api.Message; +import org.jboss.errai.bus.client.api.RemoteCallback; +import org.jboss.errai.ioc.client.api.Caller; + +import com.google.gwt.core.client.GWT; +import com.google.gwt.dom.client.InputElement; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.uibinder.client.UiBinder; +import com.google.gwt.uibinder.client.UiField; +import com.google.gwt.uibinder.client.UiHandler; +import com.google.gwt.user.client.DOM; +import com.google.gwt.user.client.ui.Button; +import com.google.gwt.user.client.ui.Label; +import com.google.gwt.user.client.ui.Panel; + +/** + * Companion class to the UI declared in {@linkplain HelloWorldClient.ui.xml}. + * Handles events and updates the DOM in response. + * + * @author Jonathan Fuerth + * @author Christian Sadilek + */ +public class HelloWorldClient { + + // The following two lines are boilerplate required by the GWT UiBinder system + + interface MyUiBinder extends UiBinder {} + private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class); + + + // The following fields annotated with @UiField are injected by UiBinder + // when the constructor calls uiBinder.createAndBindUi(this). + + @UiField Panel root; + @UiField Label result; + @UiField InputElement name; + @UiField Button sayHelloButton; + + /** + * Typesafe stub for calling methods on the JAX-RS resource class {@link HelloWorldResource}. + */ + private final Caller helloWorldCaller; + + HelloWorldClient(Caller helloWorldCaller) { + this.helloWorldCaller = helloWorldCaller; + + // populates all the @UiField members with the corresponding DOM nodes + // in HelloWorldClient.ui.xml + uiBinder.createAndBindUi(this); + + // Programmatically setting the button name attribute of the sayHelloButton. + // We do this so that we can refer to the button by name from the handwritten + // inline JavaScript in the form onSubmit handler in HelloWorldClient.ui.xml. + // This is not a typical approach in GWT, but it illustrates one way to achieve + // easy interoperability between GWT widgets and traditional HTML + JavaScript. + DOM.setElementAttribute(sayHelloButton.getElement(), "name", "sayHelloButton"); + } + + /** + * Handles a click of the button by sending an AJAX request to the + * HelloWorldResourceImpl and then updating the {@code result} label in response. + * + * @param e Details of the click event. Ignored by this handler. + */ + @UiHandler("sayHelloButton") + public void onButtonClick(ClickEvent e) { + helloWorldCaller.call(new RemoteCallback() { + + @Override + public void callback(String name) { + result.setText(name); + } + }, errorCallback).getHelloWorldJSON(name.getValue()); + } + + /** + * Returns the root of the DOM subtree under control of this UiBinder widget. + */ + public Panel getElement() { + return root; + } + + private ErrorCallback errorCallback = new ErrorCallback() { + + @Override + public boolean error(Message message, Throwable throwable) { + result.setText("Sorry, can't say hello now. " + throwable.getMessage()); + return false; + } + }; + +} diff --git a/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/client/local/HelloWorldClient.ui.xml b/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/client/local/HelloWorldClient.ui.xml new file mode 100644 index 0000000000..b58c447042 --- /dev/null +++ b/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/client/local/HelloWorldClient.ui.xml @@ -0,0 +1,15 @@ + + + +

GWT Hello World

+
+
+ + + +
+
+ + + diff --git a/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/client/shared/HelloWorldResource.java b/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/client/shared/HelloWorldResource.java new file mode 100644 index 0000000000..81d2ebf225 --- /dev/null +++ b/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/client/shared/HelloWorldResource.java @@ -0,0 +1,28 @@ +package org.jboss.as.quickstarts.erraihelloworld.client.shared; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; + + +/** + * A simple REST service which is able to say hello to someone using + * HelloService. Please take a look at the web.xml where JAX-RS is enabled and + * notice the @PathParam which expects the URL to contain {@code /json/David} or + * {@code /xml/Mary}. + */ +@Path("/") +public interface HelloWorldResource { + + @GET + @Path("/json/{name}") + @Produces("application/json") + public String getHelloWorldJSON(@PathParam("name") String name); + + @GET + @Path("/xml/{name}") + @Produces("application/xml") + public String getHelloWorldXML(@PathParam("name") String name); + +} diff --git a/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/server/HelloService.java b/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/server/HelloService.java new file mode 100644 index 0000000000..3ed9fb4ac0 --- /dev/null +++ b/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/server/HelloService.java @@ -0,0 +1,14 @@ +package org.jboss.as.quickstarts.erraihelloworld.server; + +/** + * A simple CDI service that says hello to someone. + * + * @author Pete Muir + */ +public class HelloService { + + String createHelloMessage(String name) { + return "Hello " + name + "!"; + } + +} diff --git a/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/server/HelloWorldResourceImpl.java b/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/server/HelloWorldResourceImpl.java new file mode 100644 index 0000000000..cede937712 --- /dev/null +++ b/helloworld-errai/src/main/java/org/jboss/as/quickstarts/erraihelloworld/server/HelloWorldResourceImpl.java @@ -0,0 +1,35 @@ +package org.jboss.as.quickstarts.erraihelloworld.server; + +import javax.inject.Inject; +import javax.ws.rs.PathParam; + +import org.jboss.as.quickstarts.erraihelloworld.client.shared.HelloWorldResource; + + +/** + * A simple REST service which is able to say hello to someone using + * HelloService. Please take a look at the web.xml where JAX-RS is enabled and + * notice the @PathParam which expects the URL to contain {@code /json/David} or + * {@code /xml/Mary}. + * + * @author bsutter@redhat.com + */ +public class HelloWorldResourceImpl implements HelloWorldResource { + + @Inject + HelloService helloService; + + @Override + public String getHelloWorldJSON(@PathParam("name") String name) { + System.out.println("name: " + name); + return helloService.createHelloMessage(name); + } + + @Override + public String getHelloWorldXML(@PathParam("name") String name) { + System.out.println("name: " + name); + return "" + helloService.createHelloMessage(name) + + ""; + } + +} diff --git a/helloworld-errai/src/main/resources/ErraiApp.properties b/helloworld-errai/src/main/resources/ErraiApp.properties new file mode 100644 index 0000000000..0ca66b3218 --- /dev/null +++ b/helloworld-errai/src/main/resources/ErraiApp.properties @@ -0,0 +1,17 @@ +# +# Copyright 2011 JBoss, a divison Red Hat, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# required marker interface diff --git a/helloworld-errai/src/main/resources/ErraiService.properties b/helloworld-errai/src/main/resources/ErraiService.properties new file mode 100644 index 0000000000..08bd072f11 --- /dev/null +++ b/helloworld-errai/src/main/resources/ErraiService.properties @@ -0,0 +1,21 @@ +# +# Copyright 2011 JBoss, a divison Red Hat, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +errai.require_authentication_for_all=false +errai.application_context=/App/ +errai.login_motd=UNAUTHORIZED ACCESS IS PROHIBITED! + diff --git a/helloworld-errai/src/main/resources/log4j.properties b/helloworld-errai/src/main/resources/log4j.properties new file mode 100644 index 0000000000..59c4526874 --- /dev/null +++ b/helloworld-errai/src/main/resources/log4j.properties @@ -0,0 +1,23 @@ +# +# Copyright 2010 JBoss, a divison Red Hat, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +log4j.rootLogger=INFO, A1 +log4j.appender.A1=org.apache.log4j.ConsoleAppender +log4j.appender.A1.layout=org.apache.log4j.PatternLayout +log4j.appender.mainlogger.layout.ConversionPattern=%d [%t] %-5p %c{1} - %m%n + +log4j.logger.org.reflections=INFO +log4j.logger.org.jboss.resteasy=INFO diff --git a/helloworld-errai/src/main/webapp/HelloWorldApp.html b/helloworld-errai/src/main/webapp/HelloWorldApp.html new file mode 100644 index 0000000000..a3921d3c64 --- /dev/null +++ b/helloworld-errai/src/main/webapp/HelloWorldApp.html @@ -0,0 +1,11 @@ + + + + GWT Hello World + + + + + + + diff --git a/helloworld-errai/src/main/webapp/WEB-INF/beans.xml b/helloworld-errai/src/main/webapp/WEB-INF/beans.xml new file mode 100644 index 0000000000..4bb1bb81c0 --- /dev/null +++ b/helloworld-errai/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/helloworld-errai/src/main/webapp/WEB-INF/web.xml b/helloworld-errai/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..3fe5ca126c --- /dev/null +++ b/helloworld-errai/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,49 @@ + + + + + + + + javax.ws.rs.core.Application + /hello/* + + + + HelloWorldApp.html + + + + ErraiServlet + org.jboss.errai.bus.server.servlet.DefaultBlockingServlet + 1 + + + + ErraiServlet + *.erraiBus + + + + HelloWorldApp.html + +