implementing.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. .. _ows_services_implementing:
  2. Implementing a simple OWS service
  3. =================================
  4. This section explains How to Create a Simple GeoServer OWS service for GeoServer using the following scenario. The service should supply a capabilities document which advertises a single operation called "sayHello". The result of a sayHello operation is the simple string "Hello World".
  5. .. contents::
  6. Setup
  7. -----
  8. The first step in creating our plug-in is setting up a maven project for it. The project will be called "hello".
  9. #. Create a new directory called :file:`hello` anywhere on your file system.
  10. #. Add a maven pom called :file:`pom.xml` to the :file:`hello` directory:
  11. .. literalinclude:: hello/pom.xml
  12. :language: xml
  13. #. Create a java source directory, :file:`src/main/java` under the :file:`hello` directory::
  14. hello/
  15. + pom.xml
  16. + src/
  17. + main/
  18. + java/
  19. Creating the Plug-in
  20. --------------------
  21. A plug-in is a collection of extensions realized as spring beans. In this example the extension point of interest is a HelloWorld POJO (Plain Old Java Object).
  22. #. Create a class called **HelloWorld**:
  23. .. literalinclude:: hello/src/main/java/HelloWorld.java
  24. :language: java
  25. The service is relatively simple. It provides a method sayHello(..) which takes a HttpServletRequest, and a HttpServletResponse. The parameter list for this function is automatically discovered by the org.geoserver.ows.Dispatcher.
  26. #. Create an :file:`applicationContext.xml` declaring the above class as a bean.
  27. .. literalinclude:: hello/src/main/java/applicationContext.xml
  28. :language: xml
  29. At this point the hello project should look like the following:
  30. .. code-block:: sh
  31. hello/
  32. + pom.xml
  33. + src/
  34. + main/
  35. + java/
  36. + HelloWorld.java
  37. + applicationContext.xml
  38. Trying it Out
  39. -------------
  40. #. Install the :file:`hello` module:
  41. .. code-block:: sh
  42. [hello]% mvn install
  43. .. code-block:: sh
  44. [INFO] Scanning for projects...
  45. [INFO]
  46. [INFO] ------------------------------------------------------------------------
  47. [INFO] Building Hello World Service Module 1.0
  48. [INFO] ------------------------------------------------------------------------
  49. [INFO]
  50. [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello ---
  51. [INFO] Deleting /home/bradh/devel/geoserver/doc/en/developer/source/programming-guide/ows-services/hello/target
  52. [INFO]
  53. [INFO] --- cobertura-maven-plugin:2.6:clean (default) @ hello ---
  54. [INFO]
  55. [INFO] --- git-commit-id-plugin:2.0.4:revision (default) @ hello ---
  56. [INFO] [GitCommitIdMojo] .git directory could not be found, skipping execution
  57. [INFO]
  58. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello ---
  59. [INFO] Using 'UTF-8' encoding to copy filtered resources.
  60. [INFO] Copying 1 resource
  61. [INFO] skip non existing resourceDirectory /home/bradh/devel/geoserver/doc/en/developer/source/programming-guide/ows-services/hello/src/main/resources
  62. [INFO]
  63. [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hello ---
  64. [INFO] Compiling 1 source file to /home/bradh/devel/geoserver/doc/en/developer/source/programming-guide/ows-services/hello/target/classes
  65. [INFO]
  66. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello ---
  67. [INFO] Using 'UTF-8' encoding to copy filtered resources.
  68. [INFO] skip non existing resourceDirectory /home/bradh/devel/geoserver/doc/en/developer/source/programming-guide/ows-services/hello/src/test/java
  69. [INFO] skip non existing resourceDirectory /home/bradh/devel/geoserver/doc/en/developer/source/programming-guide/ows-services/hello/src/test/resources
  70. [INFO]
  71. [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ hello ---
  72. [INFO] No sources to compile
  73. [INFO]
  74. [INFO] --- maven-surefire-plugin:2.12.3:test (default-test) @ hello ---
  75. [INFO] No tests to run.
  76. [INFO]
  77. [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello ---
  78. [INFO] Building jar: /home/bradh/devel/geoserver/doc/en/developer/source/programming-guide/ows-services/hello/target/hello-1.0.jar
  79. [INFO]
  80. [INFO] --- maven-jar-plugin:2.4:test-jar (default) @ hello ---
  81. [WARNING] JAR will be empty - no content was marked for inclusion!
  82. [INFO] Building jar: /home/bradh/devel/geoserver/doc/en/developer/source/programming-guide/ows-services/hello/target/hello-1.0-tests.jar
  83. [INFO]
  84. [INFO] >>> maven-source-plugin:2.2.1:jar (attach-sources) > generate-sources @ hello >>>
  85. [INFO]
  86. [INFO] --- git-commit-id-plugin:2.0.4:revision (default) @ hello ---
  87. [INFO] [GitCommitIdMojo] .git directory could not be found, skipping execution
  88. [INFO]
  89. [INFO] <<< maven-source-plugin:2.2.1:jar (attach-sources) < generate-sources @ hello <<<
  90. [INFO]
  91. [INFO] --- maven-source-plugin:2.2.1:jar (attach-sources) @ hello ---
  92. [INFO] Building jar: /home/bradh/devel/geoserver/doc/en/developer/source/programming-guide/ows-services/hello/target/hello-1.0-sources.jar
  93. [INFO]
  94. [INFO] >>> maven-source-plugin:2.2.1:test-jar (attach-sources) > generate-sources @ hello >>>
  95. [INFO]
  96. [INFO] --- git-commit-id-plugin:2.0.4:revision (default) @ hello ---
  97. [INFO] [GitCommitIdMojo] .git directory could not be found, skipping execution
  98. [INFO]
  99. [INFO] <<< maven-source-plugin:2.2.1:test-jar (attach-sources) < generate-sources @ hello <<<
  100. [INFO]
  101. [INFO] --- maven-source-plugin:2.2.1:test-jar (attach-sources) @ hello ---
  102. [INFO] No sources in project. Archive not created.
  103. [INFO]
  104. [INFO] --- maven-install-plugin:2.4:install (default-install) @ hello ---
  105. [INFO] Installing /home/bradh/devel/geoserver/doc/en/developer/source/programming-guide/ows-services/hello/target/hello-1.0.jar to /home/bradh/.m2/repository/org/geoserver/hello/1.0/hello-1.0.jar
  106. [INFO] Installing /home/bradh/devel/geoserver/doc/en/developer/source/programming-guide/ows-services/hello/pom.xml to /home/bradh/.m2/repository/org/geoserver/hello/1.0/hello-1.0.pom
  107. [INFO] Installing /home/bradh/devel/geoserver/doc/en/developer/source/programming-guide/ows-services/hello/target/hello-1.0-tests.jar to /home/bradh/.m2/repository/org/geoserver/hello/1.0/hello-1.0-tests.jar
  108. [INFO] Installing /home/bradh/devel/geoserver/doc/en/developer/source/programming-guide/ows-services/hello/target/hello-1.0-sources.jar to /home/bradh/.m2/repository/org/geoserver/hello/1.0/hello-1.0-sources.jar
  109. [INFO] ------------------------------------------------------------------------
  110. [INFO] BUILD SUCCESS
  111. [INFO] ------------------------------------------------------------------------
  112. [INFO] Total time: 2.473 s
  113. [INFO] Finished at: 2015-10-20T10:14:16+11:00
  114. [INFO] Final Memory: 23M/589M
  115. [INFO] ------------------------------------------------------------------------
  116. #. Next we need to make sure :file:`hello-1.0.jar` is included when we run GeoServer:
  117. * If you are running a GeoServer in Tomcat, copy :file:`target/hello-1.0.jar` into :file:`webapps/geoserver/WEB-INF/lib` (just like we manually install extensions or community modules).
  118. Restart GeoServer to pick up the change.
  119. * If running with :command:`Eclipse` using maven eclipse plugin, the easiest approach is edit the :file:`web-app/pom.xml` with the following dependency:
  120. .. code-block:: xml
  121. <dependency>
  122. <groupId>org.geoserver</groupId>
  123. <artifactId>hello</artifactId>
  124. <version>1.0-SNAPSHOT</version>
  125. </dependency>
  126. After editing :file:`webapps/pom.xml` we need run `mvn eclipse:eclipse`, and then from :command:`Eclipse` right click on web-app project and :guilabel:`Refresh` for the IDE to notice the change.
  127. * If and IDE like IntellJ, Eclipse M2 Maven plugin or NetBeans refresh the project so it picks up the changes to :file:`pom.xml`
  128. #. Restart GeoServer and visit::
  129. http://<host>/geoserver/ows?request=sayHello&service=hello&version=1.0.0
  130. request
  131. the method we defined in our service
  132. service
  133. the name we passed to the Service descriptor in the applicationContext.xml
  134. version
  135. the version we passed to the Service descriptor in the applicationContext.xml
  136. .. figure:: firefox_helloworld.png
  137. :align: center
  138. .. note::
  139. A common pitfall is to bundle an extension without the
  140. :file:`applicationContext.xml` file. If you receive the error message
  141. "No service: ( hello )" this is potentially the case. To ensure the file is
  142. present inspect the contents of the hello jar present in the :file:`target`
  143. directory of the hello module.
  144. Bundling with Web Module
  145. ------------------------
  146. An alternative to plugging into an existing installation is to build a complete
  147. GeoServer war that includes the custom hello plugin. To achieve this a new
  148. dependency is declared from the core **web/app** module on the new plugin
  149. project. This requires building GeoServer from sources.
  150. #. Build GeoServer from sources as described :ref:`here <maven_guide>`.
  151. #. Install the :file:`hello` module as above.
  152. #. Edit :file:`web/app/pom.xml` and add the following dependency:
  153. .. code-block:: xml
  154. <dependency>
  155. <groupId>org.geoserver</groupId>
  156. <artifactId>hello</artifactId>
  157. <version>1.0</version>
  158. </dependency>
  159. #. Install the :file:`web/app` module
  160. .. code-block:: sh
  161. [web/app] mvn install
  162. A GeoServer war including the hello extension should now be present in the
  163. :file:`target` directory.
  164. .. note::
  165. To verify the plugin was bundled properly unpack :file:`geoserver.war` and
  166. inspect the contents of the :file:`WEB-INF/lib` directory and ensure the
  167. hello jar is present.
  168. Running from Source
  169. -------------------
  170. During development the most convenient way to work with the extension is to run
  171. it directly from sources.
  172. #. Setup GeoServer in eclipse as described :ref:`here <eclipse_guide>`.
  173. #. Move the hello module into the GeoServer source tree under the ``community``
  174. root module.
  175. #. Edit the :file:`community/pom.xml` and add a new profile::
  176. <profile>
  177. <id>hello</id>
  178. <modules>
  179. <module>hello</module>
  180. </modules>
  181. </profile>
  182. #. If not already done, edit :file:`web/app/pom.xml` and add the following
  183. dependency:
  184. .. code-block:: xml
  185. <dependency>
  186. <groupId>org.geoserver</groupId>
  187. <artifactId>hello</artifactId>
  188. <version>1.0</version>
  189. </dependency>
  190. #. From the root of the GeoServer source tree run the following maven command:
  191. .. code-block:: sh
  192. [src] mvn -P hello eclipse:eclipse
  193. #. In eclipse import the new hello module and refresh all modules.
  194. #. In the ``web-app`` module run the ``Start.java`` main class to start
  195. GeoServer.
  196. .. note::
  197. Ensure that the ``web-app`` module in eclipse depends on the newly imported
  198. ``hello`` module. This can be done by inspecting the ``web-app`` module
  199. properties and ensuring the ``hello`` module is a project dependency.