├── .gitignore ├── LICENSE ├── README.md ├── nb-configuration.xml ├── pom.xml └── src └── main ├── java └── org │ └── glassfish │ └── javaee │ └── javascript │ └── backend │ ├── chat │ ├── ChatMessage.java │ └── ChatServer.java │ └── todo │ ├── DefaultToDoItemRepository.java │ ├── DefaultToDoService.java │ ├── JsonMoxyConfigurationContextResolver.java │ ├── RestConfiguration.java │ ├── ToDoItem.java │ ├── ToDoItemRepository.java │ ├── ToDoResource.java │ └── ToDoService.java ├── resources ├── META-INF │ └── persistence.xml ├── create-script.sql ├── drop-script.sql └── load-script.sql └── webapp ├── WEB-INF ├── beans.xml ├── glassfish-web.xml └── web.xml ├── backend.html ├── chat.jsp ├── css ├── .gitkeep ├── chat │ └── application.css ├── lib │ └── angular-growl.min.css └── todo │ └── application.css ├── images └── remove.png ├── index.html ├── javascript ├── chat │ ├── application.js │ ├── controllers.js │ ├── directives.js │ └── services.js ├── lib │ └── angular-growl.min.js └── todo │ ├── application.js │ ├── controllers.js │ ├── directives.js │ └── services.js └── todo.jsp /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Package Files # 4 | *.jar 5 | *.war 6 | *.ear 7 | .classpath 8 | .project 9 | .settings 10 | target -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL)Version 1.1 2 | 3 | 1. Definitions. 4 | 5 | 1.1. "Contributor" means each individual or entity that creates or contributes to the creation of 6 | 7 | Modifications. 8 | 9 | 1.2. "Contributor Version" means the combination of the Original Software, prior Modifications used by a 10 | 11 | Contributor (if any), and the Modifications made by that particular Contributor. 12 | 13 | 1.3. "Covered Software" means (a) the Original Software, or (b) Modifications, or (c) the combination of 14 | 15 | files containing Original Software with files containing Modifications, in each case including portions 16 | 17 | thereof. 18 | 19 | 1.4. "Executable" means the Covered Software in any form other than Source Code. 20 | 21 | 1.5. "Initial Developer" means the individual or entity that first makes Original Software available under 22 | 23 | this License. 24 | 25 | 1.6. "Larger Work" means a work which combines Covered Software or portions thereof with code not governed 26 | 27 | by the terms of this License. 28 | 29 | 1.7. "License" means this document. 30 | 31 | 1.8. "Licensable" means having the right to grant, to the maximum extent possible, whether at the time of 32 | 33 | the initial grant or subsequently acquired, any and all of the rights conveyed herein. 34 | 35 | 1.9. "Modifications" means the Source Code and Executable form of any of the following: 36 | 37 | A. Any file that results from an addition to, deletion from or modification of the contents of a file 38 | 39 | containing Original Software or previous Modifications; 40 | 41 | B. Any new file that contains any part of the Original Software or previous Modification; or 42 | 43 | C. Any new file that is contributed or otherwise made available under the terms of this License. 44 | 45 | 1.10. "Original Software" means the Source Code and Executable form of computer software code that is 46 | 47 | originally released under this License. 48 | 49 | 1.11. "Patent Claims" means any patent claim(s), now owned or hereafter acquired, including without 50 | 51 | limitation, method, process, and apparatus claims, in any patent Licensable by grantor. 52 | 53 | 1.12. "Source Code" means (a) the common form of computer software code in which modifications are made 54 | 55 | and (b) associated documentation included in or with such code. 56 | 57 | 1.13. "You" (or "Your") means an individual or a legal entity exercising rights under, and complying with 58 | 59 | all of the terms of, this License. For legal entities, "You" includes any entity which controls, is controlled 60 | 61 | by, or is under common control with You. For purposes of this definition, "control" means (a) the power, direct 62 | 63 | or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (b) 64 | 65 | ownership of more than fifty percent (50%) of the outstanding shares or beneficial ownership of such entity. 66 | 67 | 2. License Grants. 68 | 69 | 2.1. The Initial Developer Grant. 70 | 71 | Conditioned upon Your compliance with Section 3.1 below and subject to third party intellectual property 72 | 73 | claims, the Initial Developer hereby grants You a world-wide, royalty-free, non-exclusive license: 74 | 75 | (a) under intellectual property rights (other than patent or trademark) Licensable by Initial Developer, 76 | 77 | to use, reproduce, modify, display, perform, sublicense and distribute the Original Software (or portions 78 | 79 | thereof), with or without Modifications, and/or as part of a Larger Work; and 80 | 81 | (b) under Patent Claims infringed by the making, using or selling of Original Software, to make, have 82 | 83 | made, use, practice, sell, and offer for sale, and/or otherwise dispose of the Original Software (or portions 84 | 85 | thereof). 86 | 87 | (c) The licenses granted in Sections 2.1(a) and (b) are effective on the date Initial Developer first 88 | 89 | distributes or otherwise makes the Original Software available to a third party under the terms of this 90 | 91 | License. 92 | 93 | (d) Notwithstanding Section 2.1(b) above, no patent license is granted: (1) for code that You delete from 94 | 95 | the Original Software, or (2) for infringements caused by: (i) the modification of the Original Software, or 96 | 97 | (ii) the combination of the Original Software with other software or devices. 98 | 99 | 2.2. Contributor Grant. 100 | 101 | Conditioned upon Your compliance with Section 3.1 below and subject to third party intellectual property 102 | 103 | claims, each Contributor hereby grants You a world-wide, royalty-free, non-exclusive license: 104 | 105 | (a) under intellectual property rights (other than patent or trademark) Licensable by Contributor to use, 106 | 107 | reproduce, modify, display, perform, sublicense and distribute the Modifications created by such Contributor 108 | 109 | (or portions thereof), either on an unmodified basis, with other Modifications, as Covered Software and/or as 110 | 111 | part of a Larger Work; and 112 | 113 | (b) under Patent Claims infringed by the making, using, or selling of Modifications made by that 114 | 115 | Contributor either alone and/or in combination with its Contributor Version (or portions of such combination), 116 | 117 | to make, use, sell, offer for sale, have made, and/or otherwise dispose of: (1) Modifications made by that 118 | 119 | Contributor (or portions thereof); and (2) the combination of Modifications made by that Contributor with its 120 | 121 | Contributor Version (or portions of such combination). 122 | 123 | (c) The licenses granted in Sections 2.2(a) and 2.2(b) are effective on the date Contributor first 124 | 125 | distributes or otherwise makes the Modifications available to a third party. 126 | 127 | (d) Notwithstanding Section 2.2(b) above, no patent license is granted: (1) for any code that Contributor 128 | 129 | has deleted from the Contributor Version; (2) for infringements caused by: (i) third party modifications of 130 | 131 | Contributor Version, or (ii) the combination of Modifications made by that Contributor with other software 132 | 133 | (except as part of the Contributor Version) or other devices; or (3) under Patent Claims infringed by Covered 134 | 135 | Software in the absence of Modifications made by that Contributor. 136 | 137 | 3. Distribution Obligations. 138 | 139 | 3.1. Availability of Source Code. 140 | 141 | Any Covered Software that You distribute or otherwise make available in Executable form must also be made 142 | 143 | available in Source Code form and that Source Code form must be distributed only under the terms of this 144 | 145 | License. You must include a copy of this License with every copy of the Source Code form of the Covered 146 | 147 | Software You distribute or otherwise make available. You must inform recipients of any such Covered Software in 148 | 149 | Executable form as to how they can obtain such Covered Software in Source Code form in a reasonable manner on 150 | 151 | or through a medium customarily used for software exchange. 152 | 153 | 3.2. Modifications. 154 | 155 | The Modifications that You create or to which You contribute are governed by the terms of this License. 156 | 157 | You represent that You believe Your Modifications are Your original creation(s) and/or You have sufficient 158 | 159 | rights to grant the rights conveyed by this License. 160 | 161 | 3.3. Required Notices. 162 | 163 | You must include a notice in each of Your Modifications that identifies You as the Contributor of the 164 | 165 | Modification. You may not remove or alter any copyright, patent or trademark notices contained within the 166 | 167 | Covered Software, or any notices of licensing or any descriptive text giving attribution to any Contributor or 168 | 169 | the Initial Developer. 170 | 171 | 3.4. Application of Additional Terms. 172 | 173 | You may not offer or impose any terms on any Covered Software in Source Code form that alters or restricts 174 | 175 | the applicable version of this License or the recipients' rights hereunder. You may choose to offer, and to 176 | 177 | charge a fee for, warranty, support, indemnity or liability obligations to one or more recipients of Covered 178 | 179 | Software. However, you may do so only on Your own behalf, and not on behalf of the Initial Developer or any 180 | 181 | Contributor. You must make it absolutely clear that any such warranty, support, indemnity or liability 182 | 183 | obligation is offered by You alone, and You hereby agree to indemnify the Initial Developer and every 184 | 185 | Contributor for any liability incurred by the Initial Developer or such Contributor as a result of warranty, 186 | 187 | support, indemnity or liability terms You offer. 188 | 189 | 3.5. Distribution of Executable Versions. 190 | 191 | You may distribute the Executable form of the Covered Software under the terms of this License or under 192 | 193 | the terms of a license of Your choice, which may contain terms different from this License, provided that You 194 | 195 | are in compliance with the terms of this License and that the license for the Executable form does not attempt 196 | 197 | to limit or alter the recipient's rights in the Source Code form from the rights set forth in this License. If 198 | 199 | You distribute the Covered Software in Executable form under a different license, You must make it absolutely 200 | 201 | clear that any terms which differ from this License are offered by You alone, not by the Initial Developer or 202 | 203 | Contributor. You hereby agree to indemnify the Initial Developer and every Contributor for any liability 204 | 205 | incurred by the Initial Developer or such Contributor as a result of any such terms You offer. 206 | 207 | 3.6. Larger Works. 208 | 209 | You may create a Larger Work by combining Covered Software with other code not governed by the terms of 210 | 211 | this License and distribute the Larger Work as a single product. In such a case, You must make sure the 212 | 213 | requirements of this License are fulfilled for the Covered Software. 214 | 215 | 4. Versions of the License. 216 | 217 | 4.1. New Versions. 218 | 219 | Oracle is the initial license steward and may publish revised and/or new versions of this License from 220 | 221 | time to time. Each version will be given a distinguishing version number. Except as provided in Section 4.3, no 222 | 223 | one other than the license steward has the right to modify this License. 224 | 225 | 4.2. Effect of New Versions. 226 | 227 | You may always continue to use, distribute or otherwise make the Covered Software available under the 228 | 229 | terms of the version of the License under which You originally received the Covered Software. If the Initial 230 | 231 | Developer includes a notice in the Original Software prohibiting it from being distributed or otherwise made 232 | 233 | available under any subsequent version of the License, You must distribute and make the Covered Software 234 | 235 | available under the terms of the version of the License under which You originally received the Covered 236 | 237 | Software. Otherwise, You may also choose to use, distribute or otherwise make the Covered Software available 238 | 239 | under the terms of any subsequent version of the License published by the license steward. 240 | 241 | 4.3. Modified Versions. 242 | 243 | When You are an Initial Developer and You want to create a new license for Your Original Software, You may 244 | 245 | create and use a modified version of this License if You: (a) rename the license and remove any references to 246 | 247 | the name of the license steward (except to note that the license differs from this License); and (b) otherwise 248 | 249 | make it clear that the license contains terms which differ from this License. 250 | 251 | 5. DISCLAIMER OF WARRANTY. 252 | 253 | COVERED SOFTWARE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, EITHER 254 | 255 | EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED SOFTWARE IS FREE OF DEFECTS, 256 | 257 | MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE 258 | 259 | OF THE COVERED SOFTWARE IS WITH YOU. SHOULD ANY COVERED SOFTWARE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE 260 | 261 | INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. 262 | 263 | THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED SOFTWARE IS 264 | 265 | AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER. 266 | 267 | 6. TERMINATION. 268 | 269 | 6.1. This License and the rights granted hereunder will terminate automatically if You fail to comply with 270 | 271 | terms herein and fail to cure such breach within 30 days of becoming aware of the breach. Provisions which, by 272 | 273 | their nature, must remain in effect beyond the termination of this License shall survive. 274 | 275 | 6.2. If You assert a patent infringement claim (excluding declaratory judgment actions) against Initial 276 | 277 | Developer or a Contributor (the Initial Developer or Contributor against whom You assert such claim is referred 278 | 279 | to as "Participant") alleging that the Participant Software (meaning the Contributor Version where the 280 | 281 | Participant is a Contributor or the Original Software where the Participant is the Initial Developer) directly 282 | 283 | or indirectly infringes any patent, then any and all rights granted directly or indirectly to You by such 284 | 285 | Participant, the Initial Developer (if the Initial Developer is not the Participant) and all Contributors under 286 | 287 | Sections 2.1 and/or 2.2 of this License shall, upon 60 days notice from Participant terminate prospectively and 288 | 289 | automatically at the expiration of such 60 day notice period, unless if within such 60 day period You withdraw 290 | 291 | Your claim with respect to the Participant Software against such Participant either unilaterally or pursuant to 292 | 293 | a written agreement with Participant. 294 | 295 | 6.3. If You assert a patent infringement claim against Participant alleging that the Participant Software 296 | 297 | directly or indirectly infringes any patent where such claim is resolved (such as by license or settlement) 298 | 299 | prior to the initiation of patent infringement litigation, then the reasonable value of the licenses granted by 300 | 301 | such Participant under Sections 2.1 or 2.2 shall be taken into account in determining the amount or value of 302 | 303 | any payment or license. 304 | 305 | 6.4. In the event of termination under Sections 6.1 or 6.2 above, all end user licenses that have been 306 | 307 | validly granted by You or any distributor hereunder prior to termination (excluding licenses granted to You by 308 | 309 | any distributor) shall survive termination. 310 | 311 | 7. LIMITATION OF LIABILITY. 312 | 313 | UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR 314 | 315 | OTHERWISE, SHALL YOU, THE INITIAL DEVELOPER, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF COVERED SOFTWARE, OR 316 | 317 | ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO ANY PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR 318 | 319 | CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL, WORK 320 | 321 | STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF SUCH 322 | 323 | PARTY SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF SUCH DAMAGES. THIS LIMITATION OF LIABILITY SHALL NOT APPLY 324 | 325 | TO LIABILITY FOR DEATH OR PERSONAL INJURY RESULTING FROM SUCH PARTY'S NEGLIGENCE TO THE EXTENT APPLICABLE LAW 326 | 327 | PROHIBITS SUCH LIMITATION. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OR LIMITATION OF INCIDENTAL OR 328 | 329 | CONSEQUENTIAL DAMAGES, SO THIS EXCLUSION AND LIMITATION MAY NOT APPLY TO YOU. 330 | 331 | 8. U.S. GOVERNMENT END USERS. 332 | 333 | The Covered Software is a "commercial item," as that term is defined in 48 C.F.R. 2.101 (Oct. 1995), 334 | 335 | consisting of "commercial computer software" (as that term is defined at 48 C.F.R. ? 252.227-7014(a)(1)) and 336 | 337 | "commercial computer software documentation" as such terms are used in 48 C.F.R. 12.212 (Sept. 1995). 338 | 339 | Consistent with 48 C.F.R. 12.212 and 48 C.F.R. 227.7202-1 through 227.7202-4 (June 1995), all U.S. Government 340 | 341 | End Users acquire Covered Software with only those rights set forth herein. This U.S. Government Rights clause 342 | 343 | is in lieu of, and supersedes, any other FAR, DFAR, or other clause or provision that addresses Government 344 | 345 | rights in computer software under this License. 346 | 347 | 9. MISCELLANEOUS. 348 | 349 | This License represents the complete agreement concerning subject matter hereof. If any provision of this 350 | 351 | License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it 352 | 353 | enforceable. This License shall be governed by the law of the jurisdiction specified in a notice contained 354 | 355 | within the Original Software (except to the extent applicable law, if any, provides otherwise), excluding such 356 | 357 | jurisdiction's conflict-of-law provisions. Any litigation relating to this License shall be subject to the 358 | 359 | jurisdiction of the courts located in the jurisdiction and venue specified in a notice contained within the 360 | 361 | Original Software, with the losing party responsible for costs, including, without limitation, court costs and 362 | 363 | reasonable attorneys' fees and expenses. The application of the United Nations Convention on Contracts for the 364 | 365 | International Sale of Goods is expressly excluded. Any law or regulation which provides that the language of a 366 | 367 | contract shall be construed against the drafter shall not apply to this License. You agree that You alone are 368 | 369 | responsible for compliance with the United States export administration regulations (and the export control 370 | 371 | laws and regulation of any other countries) when You use, distribute or otherwise make available any Covered 372 | 373 | Software. 374 | 375 | 10. RESPONSIBILITY FOR CLAIMS. 376 | 377 | As between Initial Developer and the Contributors, each party is responsible for claims and damages 378 | 379 | arising, directly or indirectly, out of its utilization of rights under this License and You agree to work with 380 | 381 | Initial Developer and Contributors to distribute such responsibility on an equitable basis. Nothing herein is 382 | 383 | intended or shall be deemed to constitute any admission of liability. 384 | 385 | ---------- 386 | NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) 387 | The code released under the CDDL shall be governed by the laws of the State of California (excluding conflict- 388 | 389 | of-law provisions). Any litigation relating to this License shall be subject to the jurisdiction of the Federal 390 | 391 | Courts of the Northern District of California and the state courts of the State of California, with venue lying 392 | 393 | in Santa Clara County, California. 394 | 395 | 396 | 397 | 398 | The GNU General Public License (GPL) Version 2, June 1991 399 | 400 | 401 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 402 | 403 | Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not 404 | 405 | allowed. 406 | 407 | Preamble 408 | 409 | The licenses for most software are designed to take away your freedom to share and change it. By contrast, the 410 | 411 | GNU General Public License is intended to guarantee your freedom to share and change free software--to make 412 | 413 | sure the software is free for all its users. This General Public License applies to most of the Free Software 414 | 415 | Foundation's software and to any other program whose authors commit to using it. (Some other Free Software 416 | 417 | Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your 418 | 419 | programs, too. 420 | 421 | When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are 422 | 423 | designed to make sure that you have the freedom to distribute copies of free software (and charge for this 424 | 425 | service if you wish), that you receive source code or can get it if you want it, that you can change the 426 | 427 | software or use pieces of it in new free programs; and that you know you can do these things. 428 | 429 | To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you 430 | 431 | to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute 432 | 433 | copies of the software, or if you modify it. 434 | 435 | For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the 436 | 437 | recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. 438 | 439 | And you must show them these terms so they know their rights. 440 | 441 | We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives 442 | 443 | you legal permission to copy, distribute and/or modify the software. 444 | 445 | Also, for each author's protection and ours, we want to make certain that everyone understands that there is no 446 | 447 | warranty for this free software. If the software is modified by someone else and passed on, we want its 448 | 449 | recipients to know that what they have is not the original, so that any problems introduced by others will not 450 | 451 | reflect on the original authors' reputations. 452 | 453 | Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that 454 | 455 | redistributors of a free program will individually obtain patent licenses, in effect making the program 456 | 457 | proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or 458 | 459 | not licensed at all. 460 | 461 | The precise terms and conditions for copying, distribution and modification follow. 462 | 463 | 464 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 465 | 466 | 0. This License applies to any program or other work which contains a notice placed by the copyright holder 467 | 468 | saying it may be distributed under the terms of this General Public License. The "Program", below, refers to 469 | 470 | any such program or work, and a "work based on the Program" means either the Program or any derivative work 471 | 472 | under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with 473 | 474 | modifications and/or translated into another language. (Hereinafter, translation is included without limitation 475 | 476 | in the term "modification".) Each licensee is addressed as "you". 477 | 478 | Activities other than copying, distribution and modification are not covered by this License; they are outside 479 | 480 | its scope. The act of running the Program is not restricted, and the output from the Program is covered only if 481 | 482 | its contents constitute a work based on the Program (independent of having been made by running the Program). 483 | 484 | Whether that is true depends on what the Program does. 485 | 486 | 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, 487 | 488 | provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and 489 | 490 | disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any 491 | 492 | warranty; and give any other recipients of the Program a copy of this License along with the Program. 493 | 494 | You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty 495 | 496 | protection in exchange for a fee. 497 | 498 | 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the 499 | 500 | Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that 501 | 502 | you also meet all of these conditions: 503 | 504 | a) You must cause the modified files to carry prominent notices stating that you changed the files and the 505 | 506 | date of any change. 507 | 508 | b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived 509 | 510 | from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the 511 | 512 | terms of this License. 513 | 514 | c) If the modified program normally reads commands interactively when run, you must cause it, when started 515 | 516 | running for such interactive use in the most ordinary way, to print or display an announcement including an 517 | 518 | appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a 519 | 520 | warranty) and that users may redistribute the program under these conditions, and telling the user how to view 521 | 522 | a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an 523 | 524 | announcement, your work based on the Program is not required to print an announcement.) 525 | 526 | These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived 527 | 528 | from the Program, and can be reasonably considered independent and separate works in themselves, then this 529 | 530 | License, and its terms, do not apply to those sections when you distribute them as separate works. But when you 531 | 532 | distribute the same sections as part of a whole which is a work based on the Program, the distribution of the 533 | 534 | whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, 535 | 536 | and thus to each and every part regardless of who wrote it. 537 | 538 | Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by 539 | 540 | you; rather, the intent is to exercise the right to control the distribution of derivative or collective works 541 | 542 | based on the Program. 543 | 544 | In addition, mere aggregation of another work not based on the Program with the Program (or with a work based 545 | 546 | on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope 547 | 548 | of this License. 549 | 550 | 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or 551 | 552 | executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: 553 | 554 | a) Accompany it with the complete corresponding machine-readable source code, which must be distributed 555 | 556 | under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, 557 | 558 | b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge 559 | 560 | no more than your cost of physically performing source distribution, a complete machine-readable copy of the 561 | 562 | corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily 563 | 564 | used for software interchange; or, 565 | 566 | c) Accompany it with the information you received as to the offer to distribute corresponding source code. 567 | 568 | (This alternative is allowed only for noncommercial distribution and only if you received the program in object 569 | 570 | code or executable form with such an offer, in accord with Subsection b above.) 571 | 572 | The source code for a work means the preferred form of the work for making modifications to it. For an 573 | 574 | executable work, complete source code means all the source code for all modules it contains, plus any 575 | 576 | associated interface definition files, plus the scripts used to control compilation and installation of the 577 | 578 | executable. However, as a special exception, the source code distributed need not include anything that is 579 | 580 | normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) 581 | 582 | of the operating system on which the executable runs, unless that component itself accompanies the executable. 583 | 584 | If distribution of executable or object code is made by offering access to copy from a designated place, then 585 | 586 | offering equivalent access to copy the source code from the same place counts as distribution of the source 587 | 588 | code, even though third parties are not compelled to copy the source along with the object code. 589 | 590 | 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this 591 | 592 | License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will 593 | 594 | automatically terminate your rights under this License. However, parties who have received copies, or rights, 595 | 596 | from you under this License will not have their licenses terminated so long as such parties remain in full 597 | 598 | compliance. 599 | 600 | 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you 601 | 602 | permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if 603 | 604 | you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the 605 | 606 | Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, 607 | 608 | distributing or modifying the Program or works based on it. 609 | 610 | 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically 611 | 612 | receives a license from the original licensor to copy, distribute or modify the Program subject to these terms 613 | 614 | and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted 615 | 616 | herein. You are not responsible for enforcing compliance by third parties to this License. 617 | 618 | 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not 619 | 620 | limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that 621 | 622 | contradict the conditions of this License, they do not excuse you from the conditions of this License. If you 623 | 624 | cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent 625 | 626 | obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license 627 | 628 | would not permit royalty-free redistribution of the Program by all those who receive copies directly or 629 | 630 | indirectly through you, then the only way you could satisfy both it and this License would be to refrain 631 | 632 | entirely from distribution of the Program. 633 | 634 | If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance 635 | 636 | of the section is intended to apply and the section as a whole is intended to apply in other circumstances. 637 | 638 | It is not the purpose of this section to induce you to infringe any patents or other property right claims or 639 | 640 | to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the 641 | 642 | free software distribution system, which is implemented by public license practices. Many people have made 643 | 644 | generous contributions to the wide range of software distributed through that system in reliance on consistent 645 | 646 | application of that system; it is up to the author/donor to decide if he or she is willing to distribute 647 | 648 | software through any other system and a licensee cannot impose that choice. 649 | 650 | This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this 651 | 652 | License. 653 | 654 | 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by 655 | 656 | copyrighted interfaces, the original copyright holder who places the Program under this License may add an 657 | 658 | explicit geographical distribution limitation excluding those countries, so that distribution is permitted only 659 | 660 | in or among countries not thus excluded. In such case, this License incorporates the limitation as if written 661 | 662 | in the body of this License. 663 | 664 | 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time 665 | 666 | to time. Such new versions will be similar in spirit to the present version, but may differ in detail to 667 | 668 | address new problems or concerns. 669 | 670 | Each version is given a distinguishing version number. If the Program specifies a version number of this 671 | 672 | License which applies to it and "any later version", you have the option of following the terms and conditions 673 | 674 | either of that version or of any later version published by the Free Software Foundation. If the Program does 675 | 676 | not specify a version number of this License, you may choose any version ever published by the Free Software 677 | 678 | Foundation. 679 | 680 | 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are 681 | 682 | different, write to the author to ask for permission. For software which is copyrighted by the Free Software 683 | 684 | Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be 685 | 686 | guided by the two goals of preserving the free status of all derivatives of our free software and of promoting 687 | 688 | the sharing and reuse of software generally. 689 | 690 | NO WARRANTY 691 | 692 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT 693 | 694 | PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 695 | 696 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT 697 | 698 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 699 | 700 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE 701 | 702 | COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 703 | 704 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY 705 | 706 | OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 707 | 708 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE 709 | 710 | THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 711 | 712 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR 713 | 714 | OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 715 | 716 | END OF TERMS AND CONDITIONS 717 | 718 | 719 | How to Apply These Terms to Your New Programs 720 | 721 | If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to 722 | 723 | achieve this is to make it free software which everyone can redistribute and change under these terms. 724 | 725 | To do so, attach the following notices to the program. It is safest to attach them to the start of each source 726 | 727 | file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" 728 | 729 | line and a pointer to where the full notice is found. 730 | 731 | One line to give the program's name and a brief idea of what it does. 732 | 733 | Copyright (C) 734 | 735 | This program is free software; you can redistribute it and/or modify it under the terms of the GNU General 736 | 737 | Public License as published by the Free Software Foundation; either version 2 of the License, or (at your 738 | 739 | option) any later version. 740 | 741 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the 742 | 743 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 744 | 745 | more details. 746 | 747 | You should have received a copy of the GNU General Public License along with this program; if not, write to 748 | 749 | the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 750 | 751 | Also add information on how to contact you by electronic and paper mail. 752 | 753 | If the program is interactive, make it output a short notice like this when it starts in an interactive mode: 754 | 755 | Gnomovision version 69, Copyright (C) year name of author 756 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are 757 | 758 | welcome to redistribute it under certain conditions; type `show c' for details. 759 | 760 | The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public 761 | 762 | License. Of course, the commands you use may be called something other than `show w' and `show c'; they could 763 | 764 | even be mouse-clicks or menu items--whatever suits your program. 765 | 766 | You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright 767 | 768 | disclaimer" for the program, if necessary. Here is a sample; alter the names: 769 | 770 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at 771 | 772 | compilers) written by James Hacker. 773 | 774 | signature of Ty Coon, 1 April 1989 775 | Ty Coon, President of Vice 776 | 777 | This General Public License does not permit incorporating your program into proprietary programs. If your 778 | 779 | program is a subroutine library, you may consider it more useful to permit linking proprietary applications 780 | 781 | with the library. If this is what you want to do, use the GNU Library General Public License instead of this 782 | 783 | License. 784 | 785 | 786 | "CLASSPATH" EXCEPTION TO THE GPL VERSION 2 787 | 788 | Certain source files distributed by Oracle are subject to the following clarification and special exception to 789 | 790 | the GPL Version 2, but only where Oracle has expressly included in the particular source file's header the 791 | 792 | words "Oracle designates this particular file as subject to the "Classpath" exception as provided by Oracle in 793 | 794 | the License file that accompanied this code." 795 | 796 | Linking this library statically or dynamically with other modules is making a combined work based on this 797 | 798 | library. Thus, the terms and conditions of the GNU General Public License Version 2 cover the whole 799 | 800 | combination. 801 | 802 | As a special exception, the copyright holders of this library give you permission to link this library with 803 | 804 | independent modules to produce an executable, regardless of the license terms of these independent modules, and 805 | 806 | to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for 807 | 808 | each linked independent module, the terms and conditions of the license of that module. An independent module 809 | 810 | is a module which is not derived from or based on this library. If you modify this library, you may extend 811 | 812 | this exception to your version of the library, but you are not obligated to do so. If you do not wish to do 813 | 814 | so, delete this exception statement from your version. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Using JavaScript/HTML5 Rich Clients with Java EE 7 2 | ================================================== 3 | This project demonstrates how you can utilize today's most popular JavaScript frameworks like Angular, 4 | Backbone, Knockout and Ember to utilize the core strengths of Java EE. 5 | 6 | It is a Java EE 7 server-side application with an AngularJS front-end consisting of a chat application (powered by 7 | WebSocket) and a to-do list application (powered by REST). The server-side is implemented using 8 | the Java API for WebSocket, JSON-P, JAX-RS 2, CDI, Bean Validation, EJB 3 and JPA. 9 | 10 | The application is secured. Before running the application, you must setup the right GlassFish security 11 | realm using the instructions below. You can also set the username/passwords 12 | via the database scripts in the source code. The passwords are stored as SHA-256 13 | hashes. The current users are reza, nicole and zehra. The passwords are set to secret1. When using the 14 | application, the browser will warn you about the self-signed SSL certificate that GlassFish uses by default. 15 | Simply ignore the warning, it's harmless. 16 | 17 | The project is in standard Maven format. You should be able to open it using any IDE that supports Maven and 18 | run it using any Java EE 7 container. However, we used NetBeans and GlassFish 4. Note that the project uses 19 | the default Derby database that comes with GlassFish. 20 | 21 | Setup Instructions 22 | ------------------ 23 | Here are the instructions to get up and running using NetBeans and GlassFish: 24 | 25 | * Install JDK 7 26 | * Install GlassFish 4+ 27 | * Install the NetBeans 7.4+ Java EE version (GlassFish 4.1+ will only work with NetBeans 8.0.1+) 28 | * Setup GlassFish in NetBeans (make sure Derby is started with GlassFish) 29 | * Make sure you have GlassFish up and running. 30 | * Execute the following asadmin command to remove any conflicting authentication 31 | realms you might have. A conflict is unlikely, so the command will probably 32 | fail, which is fine. 33 | ``` 34 | asadmin delete-auth-realm javascript-realm 35 | ``` 36 | * For GlassFish versions up to GlassFish 4.1 (but not including it) execute the 37 | following asadmin command to create the authentication realm we need: 38 | ``` 39 | asadmin create-auth-realm --classname com.sun.enterprise.security.ee.auth.realm.jdbc.JDBCRealm --property jaas-context=jdbcRealm:datasource-jndi=jdbc/__default:user-table=javascript_users:user-name-column=username:password-column=password:group-table=javascript_groups:group-name-column=group_name javascript-realm 40 | ``` 41 | * For GlassFish 4.1 and above execute the following asadmin command: 42 | ``` 43 | asadmin create-auth-realm --classname com.sun.enterprise.security.auth.realm.jdbc.JDBCRealm --property jaas-context=jdbcRealm:datasource-jndi=jdbc/__default:user-table=javascript_users:user-name-column=username:password-column=password:group-table=javascript_groups:group-name-column=group_name javascript-realm 44 | ``` 45 | * Open and build the project 46 | * Run the project on GlassFish 4 47 | * Open up a browser and go to [http://localhost:8080/javaee-javascript](http://localhost:8080/javaee-javascript) -------------------------------------------------------------------------------- /nb-configuration.xml: -------------------------------------------------------------------------------- 1 | 2 | 43 | 44 | 50 | 51 | 57 | 1.7-web 58 | ide 59 | gfv3ee6 60 | 61 | 62 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 43 | 46 | 4.0.0 47 | 48 | org.glassfish 49 | javaee-javascript 50 | 1.0-SNAPSHOT 51 | war 52 | 53 | javaee-javascript 54 | 55 | 56 | ${project.build.directory}/endorsed 57 | UTF-8 58 | gfv3ee6 59 | 60 | 61 | 62 | 63 | javax 64 | javaee-api 65 | 7.0 66 | provided 67 | 68 | 69 | org.glassfish.jersey.containers 70 | jersey-container-servlet-core 71 | 2.0 72 | jar 73 | provided 74 | 75 | 76 | org.glassfish.jersey.media 77 | jersey-media-moxy 78 | 2.2 79 | jar 80 | provided 81 | 82 | 83 | 84 | 85 | javaee-javascript 86 | 87 | 88 | org.apache.maven.plugins 89 | maven-compiler-plugin 90 | 3.1 91 | 92 | 1.7 93 | 1.7 94 | 95 | ${endorsed.dir} 96 | 97 | 98 | 99 | 100 | org.apache.maven.plugins 101 | maven-war-plugin 102 | 2.3 103 | 104 | false 105 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /src/main/java/org/glassfish/javaee/javascript/backend/chat/ChatMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. 5 | * 6 | * The contents of this file are subject to the terms of either the GNU 7 | * General Public License Version 2 only ("GPL") or the Common Development 8 | * and Distribution License("CDDL") (collectively, the "License"). You 9 | * may not use this file except in compliance with the License. You can 10 | * obtain a copy of the License at 11 | * http://glassfish.java.net/public/CDDL+GPL_1_1.html 12 | * or packager/legal/LICENSE.txt. See the License for the specific 13 | * language governing permissions and limitations under the License. 14 | * 15 | * When distributing the software, include this License Header Notice in each 16 | * file and include the License file at packager/legal/LICENSE.txt. 17 | * 18 | * GPL Classpath Exception: 19 | * Oracle designates this particular file as subject to the "Classpath" 20 | * exception as provided by Oracle in the GPL Version 2 section of the License 21 | * file that accompanied this code. 22 | * 23 | * Modifications: 24 | * If applicable, add the following below the License Header, with the fields 25 | * enclosed by brackets [] replaced by your own identifying information: 26 | * "Portions Copyright [year] [name of copyright owner]" 27 | * 28 | * Contributor(s): 29 | * If you wish your version of this file to be governed by only the CDDL or 30 | * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 | * elects to include this software in this distribution under the [CDDL or GPL 32 | * Version 2] license." If you don't indicate a single choice of license, a 33 | * recipient has the option to distribute your version of this file under 34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 | * its licensees as provided above. However, if you add GPL Version 2 code 36 | * and therefore, elected the GPL Version 2 license, then the option applies 37 | * only if the new code is made subject to such option by the copyright 38 | * holder. 39 | */ 40 | package org.glassfish.javaee.javascript.backend.chat; 41 | 42 | import java.io.StringReader; 43 | import java.text.SimpleDateFormat; 44 | import java.util.Date; 45 | import javax.json.Json; 46 | import javax.json.JsonObject; 47 | import javax.json.JsonReader; 48 | import javax.validation.constraints.NotNull; 49 | import javax.validation.constraints.Size; 50 | import javax.websocket.Decoder; 51 | import javax.websocket.Encoder; 52 | import javax.websocket.EndpointConfig; 53 | 54 | public class ChatMessage 55 | implements Decoder.Text, Encoder.Text { 56 | 57 | @NotNull 58 | @Size(min = 4, max = 14, 59 | message = "User name must be between 4 and 14 characters.") 60 | private String user; 61 | @NotNull 62 | @Size(min = 2, max = 255, 63 | message = "Message must be between 2 and 255 characters.") 64 | private String message; 65 | 66 | @Override 67 | public void init(EndpointConfig config) { 68 | // Nothing to do. 69 | } 70 | 71 | @Override 72 | public ChatMessage decode(String value) { 73 | try (JsonReader jsonReader = Json.createReader( 74 | new StringReader(value))) { 75 | JsonObject jsonObject = jsonReader.readObject(); 76 | user = jsonObject.getString("user"); 77 | message = jsonObject.getString("message"); 78 | } 79 | 80 | return this; 81 | } 82 | 83 | @Override 84 | public boolean willDecode(String string) { 85 | return true; // Detect if it's a valid format. 86 | } 87 | 88 | @Override 89 | public String encode(ChatMessage chatMessage) { 90 | JsonObject jsonObject = Json.createObjectBuilder() 91 | .add("user", chatMessage.user) 92 | .add("message", chatMessage.message) 93 | .add("timestamp", 94 | new SimpleDateFormat("MM/dd/yyyy h:mm:ss a z") 95 | .format(new Date())) 96 | .build(); 97 | 98 | return jsonObject.toString(); 99 | } 100 | 101 | @Override 102 | public void destroy() { 103 | // Nothing to do. 104 | } 105 | 106 | @Override 107 | public String toString() { 108 | return "ChatMessage{" + "user=" + user + ", message=" + message + '}'; 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/main/java/org/glassfish/javaee/javascript/backend/chat/ChatServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. 5 | * 6 | * The contents of this file are subject to the terms of either the GNU 7 | * General Public License Version 2 only ("GPL") or the Common Development 8 | * and Distribution License("CDDL") (collectively, the "License"). You 9 | * may not use this file except in compliance with the License. You can 10 | * obtain a copy of the License at 11 | * http://glassfish.java.net/public/CDDL+GPL_1_1.html 12 | * or packager/legal/LICENSE.txt. See the License for the specific 13 | * language governing permissions and limitations under the License. 14 | * 15 | * When distributing the software, include this License Header Notice in each 16 | * file and include the License file at packager/legal/LICENSE.txt. 17 | * 18 | * GPL Classpath Exception: 19 | * Oracle designates this particular file as subject to the "Classpath" 20 | * exception as provided by Oracle in the GPL Version 2 section of the License 21 | * file that accompanied this code. 22 | * 23 | * Modifications: 24 | * If applicable, add the following below the License Header, with the fields 25 | * enclosed by brackets [] replaced by your own identifying information: 26 | * "Portions Copyright [year] [name of copyright owner]" 27 | * 28 | * Contributor(s): 29 | * If you wish your version of this file to be governed by only the CDDL or 30 | * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 | * elects to include this software in this distribution under the [CDDL or GPL 32 | * Version 2] license." If you don't indicate a single choice of license, a 33 | * recipient has the option to distribute your version of this file under 34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 | * its licensees as provided above. However, if you add GPL Version 2 code 36 | * and therefore, elected the GPL Version 2 license, then the option applies 37 | * only if the new code is made subject to such option by the copyright 38 | * holder. 39 | */ 40 | package org.glassfish.javaee.javascript.backend.chat; 41 | 42 | import java.io.IOException; 43 | import java.util.HashSet; 44 | import java.util.Set; 45 | import java.util.logging.Level; 46 | import java.util.logging.Logger; 47 | import javax.ejb.Singleton; 48 | import javax.json.Json; 49 | import javax.json.JsonObject; 50 | import javax.validation.ConstraintViolationException; 51 | import javax.validation.Valid; 52 | import javax.websocket.EncodeException; 53 | import javax.websocket.OnClose; 54 | import javax.websocket.OnError; 55 | import javax.websocket.OnMessage; 56 | import javax.websocket.OnOpen; 57 | import javax.websocket.Session; 58 | import javax.websocket.server.ServerEndpoint; 59 | 60 | @ServerEndpoint(value = "/chat", 61 | encoders = {ChatMessage.class}, decoders = {ChatMessage.class}) 62 | @Singleton 63 | public class ChatServer { 64 | 65 | private static final Logger logger = Logger 66 | .getLogger(ChatServer.class.getName()); 67 | 68 | private final Set peers; 69 | 70 | public ChatServer() { 71 | peers = new HashSet<>(); 72 | } 73 | 74 | @OnOpen 75 | public void onOpen(Session peer) { 76 | logger.log(Level.INFO, "Opened session: {0}", peer); 77 | peers.add(peer); 78 | } 79 | 80 | @OnClose 81 | public void onClose(Session peer) { 82 | logger.log(Level.INFO, "Closed session: {0}", peer); 83 | peers.remove(peer); 84 | } 85 | 86 | @OnMessage 87 | public void onMessage(@Valid ChatMessage message, Session session) { 88 | logger.log(Level.INFO, "Received message {0} from peer {1}", 89 | new Object[]{message, session}); 90 | 91 | for (Session peer : peers) { 92 | try { 93 | logger.log(Level.INFO, "Broadcasting message {0} to peer {1}", 94 | new Object[]{message, peer}); 95 | 96 | peer.getBasicRemote().sendObject(message); 97 | } catch (IOException | EncodeException ex) { 98 | logger.log(Level.SEVERE, "Error sending message", ex); 99 | } 100 | } 101 | } 102 | 103 | @OnError 104 | public void onError(Session session, Throwable error) { 105 | try { 106 | if (error.getCause() instanceof ConstraintViolationException) { 107 | // Just report the first validation problem. 108 | JsonObject jsonObject = Json.createObjectBuilder() 109 | .add("error", 110 | ((ConstraintViolationException) error.getCause()) 111 | .getConstraintViolations().iterator().next() 112 | .getMessage()) 113 | .build(); 114 | session.getBasicRemote().sendText(jsonObject.toString()); 115 | } else { 116 | logger.log(Level.SEVERE, null, error); 117 | } 118 | } catch (IOException ex) { 119 | logger.log(Level.SEVERE, null, ex); 120 | } 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/main/java/org/glassfish/javaee/javascript/backend/todo/DefaultToDoItemRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. 5 | * 6 | * The contents of this file are subject to the terms of either the GNU 7 | * General Public License Version 2 only ("GPL") or the Common Development 8 | * and Distribution License("CDDL") (collectively, the "License"). You 9 | * may not use this file except in compliance with the License. You can 10 | * obtain a copy of the License at 11 | * http://glassfish.java.net/public/CDDL+GPL_1_1.html 12 | * or packager/legal/LICENSE.txt. See the License for the specific 13 | * language governing permissions and limitations under the License. 14 | * 15 | * When distributing the software, include this License Header Notice in each 16 | * file and include the License file at packager/legal/LICENSE.txt. 17 | * 18 | * GPL Classpath Exception: 19 | * Oracle designates this particular file as subject to the "Classpath" 20 | * exception as provided by Oracle in the GPL Version 2 section of the License 21 | * file that accompanied this code. 22 | * 23 | * Modifications: 24 | * If applicable, add the following below the License Header, with the fields 25 | * enclosed by brackets [] replaced by your own identifying information: 26 | * "Portions Copyright [year] [name of copyright owner]" 27 | * 28 | * Contributor(s): 29 | * If you wish your version of this file to be governed by only the CDDL or 30 | * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 | * elects to include this software in this distribution under the [CDDL or GPL 32 | * Version 2] license." If you don't indicate a single choice of license, a 33 | * recipient has the option to distribute your version of this file under 34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 | * its licensees as provided above. However, if you add GPL Version 2 code 36 | * and therefore, elected the GPL Version 2 license, then the option applies 37 | * only if the new code is made subject to such option by the copyright 38 | * holder. 39 | */ 40 | package org.glassfish.javaee.javascript.backend.todo; 41 | 42 | import java.util.List; 43 | import javax.persistence.EntityManager; 44 | import javax.persistence.PersistenceContext; 45 | 46 | public class DefaultToDoItemRepository implements ToDoItemRepository { 47 | 48 | @PersistenceContext 49 | private EntityManager entityManager; 50 | 51 | @Override 52 | public ToDoItem create(ToDoItem item) { 53 | entityManager.persist(item); 54 | 55 | return item; 56 | } 57 | 58 | @Override 59 | public ToDoItem find(Long id) { 60 | return entityManager.find(ToDoItem.class, id); 61 | } 62 | 63 | @Override 64 | public List findByUsername(String username) { 65 | return entityManager.createNamedQuery( 66 | "ToDoItem.findByUsername", 67 | ToDoItem.class) 68 | .setParameter("username", username).getResultList(); 69 | } 70 | 71 | @Override 72 | public void update(ToDoItem item) { 73 | entityManager.merge(item); 74 | } 75 | 76 | @Override 77 | public void delete(ToDoItem item) { 78 | entityManager.remove(item); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/org/glassfish/javaee/javascript/backend/todo/DefaultToDoService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. 5 | * 6 | * The contents of this file are subject to the terms of either the GNU 7 | * General Public License Version 2 only ("GPL") or the Common Development 8 | * and Distribution License("CDDL") (collectively, the "License"). You 9 | * may not use this file except in compliance with the License. You can 10 | * obtain a copy of the License at 11 | * http://glassfish.java.net/public/CDDL+GPL_1_1.html 12 | * or packager/legal/LICENSE.txt. See the License for the specific 13 | * language governing permissions and limitations under the License. 14 | * 15 | * When distributing the software, include this License Header Notice in each 16 | * file and include the License file at packager/legal/LICENSE.txt. 17 | * 18 | * GPL Classpath Exception: 19 | * Oracle designates this particular file as subject to the "Classpath" 20 | * exception as provided by Oracle in the GPL Version 2 section of the License 21 | * file that accompanied this code. 22 | * 23 | * Modifications: 24 | * If applicable, add the following below the License Header, with the fields 25 | * enclosed by brackets [] replaced by your own identifying information: 26 | * "Portions Copyright [year] [name of copyright owner]" 27 | * 28 | * Contributor(s): 29 | * If you wish your version of this file to be governed by only the CDDL or 30 | * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 | * elects to include this software in this distribution under the [CDDL or GPL 32 | * Version 2] license." If you don't indicate a single choice of license, a 33 | * recipient has the option to distribute your version of this file under 34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 | * its licensees as provided above. However, if you add GPL Version 2 code 36 | * and therefore, elected the GPL Version 2 license, then the option applies 37 | * only if the new code is made subject to such option by the copyright 38 | * holder. 39 | */ 40 | package org.glassfish.javaee.javascript.backend.todo; 41 | 42 | import java.util.List; 43 | import java.util.logging.Level; 44 | import java.util.logging.Logger; 45 | 46 | import javax.ejb.Stateless; 47 | import javax.inject.Inject; 48 | 49 | @Stateless 50 | public class DefaultToDoService implements ToDoService { 51 | 52 | private static final Logger logger = Logger 53 | .getLogger(DefaultToDoService.class.getName()); 54 | 55 | @Inject 56 | private ToDoItemRepository repository; 57 | 58 | @Override 59 | public ToDoItem addToDoItem(String username, ToDoItem item) { 60 | item.setUsername(username); 61 | 62 | logger.log(Level.INFO, "Adding item: {0}", item); 63 | 64 | return repository.create(item); 65 | } 66 | 67 | @Override 68 | public void updateToDoItem(String username, ToDoItem item) { 69 | item.setUsername(username); 70 | 71 | logger.log(Level.INFO, "Updating item: {0}", item); 72 | 73 | repository.update(item); 74 | } 75 | 76 | @Override 77 | public void removeToDoItem(String username, Long id) { 78 | ToDoItem item = repository.find(id); 79 | 80 | logger.log(Level.INFO, "Removing item: {0}", item); 81 | 82 | repository.delete(item); 83 | } 84 | 85 | @Override 86 | public List findToDoItemsByUsername(String username) { 87 | logger.log(Level.INFO, "Getting all items for: {0}", username); 88 | 89 | return repository.findByUsername(username); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/org/glassfish/javaee/javascript/backend/todo/JsonMoxyConfigurationContextResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. 5 | * 6 | * The contents of this file are subject to the terms of either the GNU 7 | * General Public License Version 2 only ("GPL") or the Common Development 8 | * and Distribution License("CDDL") (collectively, the "License"). You 9 | * may not use this file except in compliance with the License. You can 10 | * obtain a copy of the License at 11 | * http://glassfish.java.net/public/CDDL+GPL_1_1.html 12 | * or packager/legal/LICENSE.txt. See the License for the specific 13 | * language governing permissions and limitations under the License. 14 | * 15 | * When distributing the software, include this License Header Notice in each 16 | * file and include the License file at packager/legal/LICENSE.txt. 17 | * 18 | * GPL Classpath Exception: 19 | * Oracle designates this particular file as subject to the "Classpath" 20 | * exception as provided by Oracle in the GPL Version 2 section of the License 21 | * file that accompanied this code. 22 | * 23 | * Modifications: 24 | * If applicable, add the following below the License Header, with the fields 25 | * enclosed by brackets [] replaced by your own identifying information: 26 | * "Portions Copyright [year] [name of copyright owner]" 27 | * 28 | * Contributor(s): 29 | * If you wish your version of this file to be governed by only the CDDL or 30 | * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 | * elects to include this software in this distribution under the [CDDL or GPL 32 | * Version 2] license." If you don't indicate a single choice of license, a 33 | * recipient has the option to distribute your version of this file under 34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 | * its licensees as provided above. However, if you add GPL Version 2 code 36 | * and therefore, elected the GPL Version 2 license, then the option applies 37 | * only if the new code is made subject to such option by the copyright 38 | * holder. 39 | */ 40 | package org.glassfish.javaee.javascript.backend.todo; 41 | 42 | import java.util.HashMap; 43 | import java.util.Map; 44 | import javax.ws.rs.ext.ContextResolver; 45 | import javax.ws.rs.ext.Provider; 46 | import org.glassfish.jersey.moxy.json.MoxyJsonConfig; 47 | 48 | @Provider 49 | public class JsonMoxyConfigurationContextResolver 50 | implements ContextResolver { 51 | 52 | @Override 53 | public MoxyJsonConfig getContext(Class objectType) { 54 | MoxyJsonConfig configuration = new MoxyJsonConfig(); 55 | 56 | Map namespacePrefixMapper = new HashMap<>(1); 57 | namespacePrefixMapper.put("http://www.w3.org/2001/XMLSchema-instance", "xsi"); 58 | configuration.setNamespacePrefixMapper(namespacePrefixMapper); 59 | configuration.setNamespaceSeparator(':'); 60 | 61 | return configuration; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/glassfish/javaee/javascript/backend/todo/RestConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. 5 | * 6 | * The contents of this file are subject to the terms of either the GNU 7 | * General Public License Version 2 only ("GPL") or the Common Development 8 | * and Distribution License("CDDL") (collectively, the "License"). You 9 | * may not use this file except in compliance with the License. You can 10 | * obtain a copy of the License at 11 | * http://glassfish.java.net/public/CDDL+GPL_1_1.html 12 | * or packager/legal/LICENSE.txt. See the License for the specific 13 | * language governing permissions and limitations under the License. 14 | * 15 | * When distributing the software, include this License Header Notice in each 16 | * file and include the License file at packager/legal/LICENSE.txt. 17 | * 18 | * GPL Classpath Exception: 19 | * Oracle designates this particular file as subject to the "Classpath" 20 | * exception as provided by Oracle in the GPL Version 2 section of the License 21 | * file that accompanied this code. 22 | * 23 | * Modifications: 24 | * If applicable, add the following below the License Header, with the fields 25 | * enclosed by brackets [] replaced by your own identifying information: 26 | * "Portions Copyright [year] [name of copyright owner]" 27 | * 28 | * Contributor(s): 29 | * If you wish your version of this file to be governed by only the CDDL or 30 | * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 | * elects to include this software in this distribution under the [CDDL or GPL 32 | * Version 2] license." If you don't indicate a single choice of license, a 33 | * recipient has the option to distribute your version of this file under 34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 | * its licensees as provided above. However, if you add GPL Version 2 code 36 | * and therefore, elected the GPL Version 2 license, then the option applies 37 | * only if the new code is made subject to such option by the copyright 38 | * holder. 39 | */ 40 | package org.glassfish.javaee.javascript.backend.todo; 41 | 42 | import java.util.HashMap; 43 | import java.util.HashSet; 44 | import java.util.Map; 45 | import java.util.Set; 46 | import javax.ws.rs.ApplicationPath; 47 | import javax.ws.rs.core.Application; 48 | import org.glassfish.jersey.server.ServerProperties; 49 | 50 | @ApplicationPath("resources") 51 | public class RestConfiguration extends Application { 52 | 53 | @Override 54 | public Set> getClasses() { 55 | Set> resources = new HashSet<>(); 56 | addRestResourceClasses(resources); 57 | return resources; 58 | } 59 | 60 | @Override 61 | public Map getProperties() { 62 | Map properties = new HashMap(); 63 | properties.put(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true); 64 | return properties; 65 | } 66 | 67 | private void addRestResourceClasses(Set> resources) { 68 | resources.add(org.glassfish.javaee.javascript.backend.todo.JsonMoxyConfigurationContextResolver.class); 69 | resources.add(org.glassfish.javaee.javascript.backend.todo.ToDoResource.class); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/glassfish/javaee/javascript/backend/todo/ToDoItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. 5 | * 6 | * The contents of this file are subject to the terms of either the GNU 7 | * General Public License Version 2 only ("GPL") or the Common Development 8 | * and Distribution License("CDDL") (collectively, the "License"). You 9 | * may not use this file except in compliance with the License. You can 10 | * obtain a copy of the License at 11 | * http://glassfish.java.net/public/CDDL+GPL_1_1.html 12 | * or packager/legal/LICENSE.txt. See the License for the specific 13 | * language governing permissions and limitations under the License. 14 | * 15 | * When distributing the software, include this License Header Notice in each 16 | * file and include the License file at packager/legal/LICENSE.txt. 17 | * 18 | * GPL Classpath Exception: 19 | * Oracle designates this particular file as subject to the "Classpath" 20 | * exception as provided by Oracle in the GPL Version 2 section of the License 21 | * file that accompanied this code. 22 | * 23 | * Modifications: 24 | * If applicable, add the following below the License Header, with the fields 25 | * enclosed by brackets [] replaced by your own identifying information: 26 | * "Portions Copyright [year] [name of copyright owner]" 27 | * 28 | * Contributor(s): 29 | * If you wish your version of this file to be governed by only the CDDL or 30 | * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 | * elects to include this software in this distribution under the [CDDL or GPL 32 | * Version 2] license." If you don't indicate a single choice of license, a 33 | * recipient has the option to distribute your version of this file under 34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 | * its licensees as provided above. However, if you add GPL Version 2 code 36 | * and therefore, elected the GPL Version 2 license, then the option applies 37 | * only if the new code is made subject to such option by the copyright 38 | * holder. 39 | */ 40 | package org.glassfish.javaee.javascript.backend.todo; 41 | 42 | import java.io.Serializable; 43 | import javax.persistence.Entity; 44 | import javax.persistence.GeneratedValue; 45 | import javax.persistence.Id; 46 | import javax.persistence.NamedQuery; 47 | import javax.persistence.Table; 48 | import javax.validation.constraints.NotNull; 49 | import javax.validation.constraints.Size; 50 | import javax.xml.bind.annotation.XmlRootElement; 51 | import javax.xml.bind.annotation.XmlTransient; 52 | 53 | @Entity 54 | @Table(name = "todo_item") 55 | @NamedQuery( 56 | name = "ToDoItem.findByUsername", 57 | query = "SELECT i FROM ToDoItem i WHERE i.username = :username") 58 | @XmlRootElement 59 | public class ToDoItem implements Serializable { 60 | 61 | private static final long serialVersionUID = 1L; 62 | 63 | @Id 64 | @GeneratedValue 65 | private Long id; 66 | 67 | private String username; 68 | 69 | @NotNull 70 | @Size(min = 5, max = 110, 71 | message = "Item description must be between 5 and 110 characters.") 72 | private String description; 73 | 74 | private boolean completed; 75 | 76 | /** 77 | * Default constructor 78 | */ 79 | protected ToDoItem() { 80 | // Default constructor 81 | } 82 | 83 | /** 84 | * Constructor for creating instances 85 | * 86 | * @param username - username 87 | * @param description - description of the task 88 | * @param completed - completed 89 | */ 90 | public ToDoItem(String username, String description, boolean completed) { 91 | this.username = username; 92 | this.description = description; 93 | this.completed = completed; 94 | } 95 | 96 | public Long getId() { 97 | return id; 98 | } 99 | 100 | public void setId(Long id) { 101 | this.id = id; 102 | } 103 | 104 | @XmlTransient 105 | public String getUsername() { 106 | return username; 107 | } 108 | 109 | public void setUsername(String username) { 110 | this.username = username; 111 | } 112 | 113 | public String getDescription() { 114 | return description; 115 | } 116 | 117 | public void setDescription(String description) { 118 | this.description = description; 119 | } 120 | 121 | public boolean isCompleted() { 122 | return completed; 123 | } 124 | 125 | public void setCompleted(boolean completed) { 126 | this.completed = completed; 127 | } 128 | 129 | @Override 130 | public int hashCode() { 131 | int hash = 0; 132 | hash += (id != null ? id.hashCode() : 0); 133 | return hash; 134 | } 135 | 136 | @Override 137 | public boolean equals(Object object) { 138 | // TODO: Warning - this method won't work in the case the id fields are not set 139 | if (!(object instanceof ToDoItem)) { 140 | return false; 141 | } 142 | ToDoItem other = (ToDoItem) object; 143 | if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { 144 | return false; 145 | } 146 | return true; 147 | } 148 | 149 | @Override 150 | public String toString() { 151 | return "ToDoItem[ id=" + id 152 | + " username=" + username 153 | + " desciption=" + description 154 | + " completed=" + completed + " ]"; 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /src/main/java/org/glassfish/javaee/javascript/backend/todo/ToDoItemRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. 5 | * 6 | * The contents of this file are subject to the terms of either the GNU 7 | * General Public License Version 2 only ("GPL") or the Common Development 8 | * and Distribution License("CDDL") (collectively, the "License"). You 9 | * may not use this file except in compliance with the License. You can 10 | * obtain a copy of the License at 11 | * http://glassfish.java.net/public/CDDL+GPL_1_1.html 12 | * or packager/legal/LICENSE.txt. See the License for the specific 13 | * language governing permissions and limitations under the License. 14 | * 15 | * When distributing the software, include this License Header Notice in each 16 | * file and include the License file at packager/legal/LICENSE.txt. 17 | * 18 | * GPL Classpath Exception: 19 | * Oracle designates this particular file as subject to the "Classpath" 20 | * exception as provided by Oracle in the GPL Version 2 section of the License 21 | * file that accompanied this code. 22 | * 23 | * Modifications: 24 | * If applicable, add the following below the License Header, with the fields 25 | * enclosed by brackets [] replaced by your own identifying information: 26 | * "Portions Copyright [year] [name of copyright owner]" 27 | * 28 | * Contributor(s): 29 | * If you wish your version of this file to be governed by only the CDDL or 30 | * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 | * elects to include this software in this distribution under the [CDDL or GPL 32 | * Version 2] license." If you don't indicate a single choice of license, a 33 | * recipient has the option to distribute your version of this file under 34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 | * its licensees as provided above. However, if you add GPL Version 2 code 36 | * and therefore, elected the GPL Version 2 license, then the option applies 37 | * only if the new code is made subject to such option by the copyright 38 | * holder. 39 | */ 40 | package org.glassfish.javaee.javascript.backend.todo; 41 | 42 | import java.util.List; 43 | 44 | public interface ToDoItemRepository { 45 | 46 | ToDoItem create(ToDoItem item); 47 | 48 | void delete(ToDoItem item); 49 | 50 | ToDoItem find(Long id); 51 | 52 | List findByUsername(String username); 53 | 54 | void update(ToDoItem item); 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/glassfish/javaee/javascript/backend/todo/ToDoResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. 5 | * 6 | * The contents of this file are subject to the terms of either the GNU 7 | * General Public License Version 2 only ("GPL") or the Common Development 8 | * and Distribution License("CDDL") (collectively, the "License"). You 9 | * may not use this file except in compliance with the License. You can 10 | * obtain a copy of the License at 11 | * http://glassfish.java.net/public/CDDL+GPL_1_1.html 12 | * or packager/legal/LICENSE.txt. See the License for the specific 13 | * language governing permissions and limitations under the License. 14 | * 15 | * When distributing the software, include this License Header Notice in each 16 | * file and include the License file at packager/legal/LICENSE.txt. 17 | * 18 | * GPL Classpath Exception: 19 | * Oracle designates this particular file as subject to the "Classpath" 20 | * exception as provided by Oracle in the GPL Version 2 section of the License 21 | * file that accompanied this code. 22 | * 23 | * Modifications: 24 | * If applicable, add the following below the License Header, with the fields 25 | * enclosed by brackets [] replaced by your own identifying information: 26 | * "Portions Copyright [year] [name of copyright owner]" 27 | * 28 | * Contributor(s): 29 | * If you wish your version of this file to be governed by only the CDDL or 30 | * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 | * elects to include this software in this distribution under the [CDDL or GPL 32 | * Version 2] license." If you don't indicate a single choice of license, a 33 | * recipient has the option to distribute your version of this file under 34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 | * its licensees as provided above. However, if you add GPL Version 2 code 36 | * and therefore, elected the GPL Version 2 license, then the option applies 37 | * only if the new code is made subject to such option by the copyright 38 | * holder. 39 | */ 40 | package org.glassfish.javaee.javascript.backend.todo; 41 | 42 | import java.io.Serializable; 43 | import java.util.List; 44 | 45 | import javax.enterprise.context.ApplicationScoped; 46 | import javax.inject.Inject; 47 | import javax.validation.Valid; 48 | import javax.validation.constraints.NotNull; 49 | import javax.validation.constraints.Size; 50 | import javax.ws.rs.Consumes; 51 | import javax.ws.rs.DELETE; 52 | import javax.ws.rs.GET; 53 | import javax.ws.rs.POST; 54 | import javax.ws.rs.PUT; 55 | import javax.ws.rs.Path; 56 | import javax.ws.rs.PathParam; 57 | import javax.ws.rs.Produces; 58 | 59 | @Path("/todo/{username}") 60 | @ApplicationScoped 61 | public class ToDoResource implements Serializable { 62 | 63 | private static final long serialVersionUID = 1L; 64 | 65 | @Inject 66 | private ToDoService service; 67 | 68 | @POST 69 | @Consumes({"application/json"}) 70 | @Produces({"application/json"}) 71 | public ToDoItem create( 72 | @PathParam("username") 73 | @NotNull 74 | @Size(min = 4, max = 14, message = "User name must be between 4 and 14 characters.") String username, 75 | @Valid ToDoItem item) { 76 | return service.addToDoItem(username, item); 77 | } 78 | 79 | @PUT 80 | @Path("{id}") 81 | @Consumes({"application/json"}) 82 | public void edit( 83 | @PathParam("username") 84 | @NotNull 85 | @Size(min = 4, max = 14, message = "User name must be between 4 and 14 characters.") String username, 86 | @PathParam("id") Long id, 87 | @Valid ToDoItem item) { 88 | item.setId(id); 89 | service.updateToDoItem(username, item); 90 | } 91 | 92 | @DELETE 93 | @Path("{id}") 94 | public void remove( 95 | @PathParam("username") 96 | @NotNull 97 | @Size(min = 4, max = 14, message = "User name must be between 4 and 14 characters.") String username, 98 | @PathParam("id") Long id) { 99 | service.removeToDoItem(username, id); 100 | } 101 | 102 | @GET 103 | @Produces({"application/json"}) 104 | public List getAll( 105 | @PathParam("username") 106 | @NotNull 107 | @Size(min = 4, max = 14, message = "User name must be between 4 and 14 characters.") String username) { 108 | return service.findToDoItemsByUsername(username); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/main/java/org/glassfish/javaee/javascript/backend/todo/ToDoService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. 5 | * 6 | * The contents of this file are subject to the terms of either the GNU 7 | * General Public License Version 2 only ("GPL") or the Common Development 8 | * and Distribution License("CDDL") (collectively, the "License"). You 9 | * may not use this file except in compliance with the License. You can 10 | * obtain a copy of the License at 11 | * http://glassfish.java.net/public/CDDL+GPL_1_1.html 12 | * or packager/legal/LICENSE.txt. See the License for the specific 13 | * language governing permissions and limitations under the License. 14 | * 15 | * When distributing the software, include this License Header Notice in each 16 | * file and include the License file at packager/legal/LICENSE.txt. 17 | * 18 | * GPL Classpath Exception: 19 | * Oracle designates this particular file as subject to the "Classpath" 20 | * exception as provided by Oracle in the GPL Version 2 section of the License 21 | * file that accompanied this code. 22 | * 23 | * Modifications: 24 | * If applicable, add the following below the License Header, with the fields 25 | * enclosed by brackets [] replaced by your own identifying information: 26 | * "Portions Copyright [year] [name of copyright owner]" 27 | * 28 | * Contributor(s): 29 | * If you wish your version of this file to be governed by only the CDDL or 30 | * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 | * elects to include this software in this distribution under the [CDDL or GPL 32 | * Version 2] license." If you don't indicate a single choice of license, a 33 | * recipient has the option to distribute your version of this file under 34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 | * its licensees as provided above. However, if you add GPL Version 2 code 36 | * and therefore, elected the GPL Version 2 license, then the option applies 37 | * only if the new code is made subject to such option by the copyright 38 | * holder. 39 | */ 40 | package org.glassfish.javaee.javascript.backend.todo; 41 | 42 | import java.util.List; 43 | 44 | public interface ToDoService { 45 | 46 | ToDoItem addToDoItem(String username, ToDoItem item); 47 | 48 | List findToDoItemsByUsername(String username); 49 | 50 | void removeToDoItem(String username, Long id); 51 | 52 | void updateToDoItem(String username, ToDoItem item); 53 | } 54 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/resources/create-script.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE javascript_users (username VARCHAR(20) NOT NULL, password VARCHAR(255) NOT NULL, PRIMARY KEY (username)) 2 | CREATE TABLE javascript_groups (username VARCHAR(20) NOT NULL, group_name VARCHAR(20) NOT NULL, PRIMARY KEY (username, group_name)) 3 | ALTER TABLE javascript_groups ADD CONSTRAINT fk_username FOREIGN KEY(username) REFERENCES javascript_users (username) -------------------------------------------------------------------------------- /src/main/resources/drop-script.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE javascript_groups 2 | DROP TABLE javascript_users -------------------------------------------------------------------------------- /src/main/resources/load-script.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO javascript_users (username, password) VALUES ('reza', '5b11618c2e44027877d0cd0921ed166b9f176f50587fc91e7534dd2946db77d6') 2 | INSERT INTO javascript_users (username, password) VALUES ('nicole', '5b11618c2e44027877d0cd0921ed166b9f176f50587fc91e7534dd2946db77d6') 3 | INSERT INTO javascript_users (username, password) VALUES ('zehra', '5b11618c2e44027877d0cd0921ed166b9f176f50587fc91e7534dd2946db77d6') 4 | INSERT INTO javascript_groups (username, group_name) VALUES ('reza', 'javascript') 5 | INSERT INTO javascript_groups (username, group_name) VALUES ('nicole', 'javascript') 6 | INSERT INTO javascript_groups (username, group_name) VALUES ('zehra', 'javascript') -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/glassfish-web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | javaee-javascript 5 | 6 | javascript 7 | javascript 8 | 9 | 10 | 11 | 12 | Keep a copy of the generated servlet class' java code. 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | Secure JavaScript Clients 9 | 10 | Secure Interfaces 11 | /todo.jsp 12 | /chat.jsp 13 | 17 | GET 18 | 23 | 24 | 25 | javascript 26 | 27 | 28 | CONFIDENTIAL 29 | 30 | 31 | 32 | Secure Java EE Backend 33 | 34 | Secure Services 35 | /resources/todo/* 36 | /chat 37 | GET 38 | POST 39 | PUT 40 | DELETE 41 | 42 | 43 | CONFIDENTIAL 44 | 45 | 46 | 47 | 48 | BASIC 49 | javascript-realm 50 | 51 | 52 | 53 | 30 54 | 55 | 56 | -------------------------------------------------------------------------------- /src/main/webapp/backend.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Java EE as JavaScript Backend 4 | 5 | 6 | 7 |

Java EE as JavaScript Backend

8 |

The JavaScript backend consists of a chat WebSocket API 9 | and a to do list REST API implemented using the Java 10 | API for WebSocket, JSON-P, JAX-RS 2 and Java EE 7.

11 |

Note that while the endpoints in this application are not fully secured 12 | but uses TLS/SSL, in a real application they should be secured, likely using 13 | some sort of federated security scheme.

14 |

The WebSocket chat endpoint is at: 15 | 16 | wss://localhost:8181/javaee-javascript/chat. The 17 | endpoint communicates via JSON. Once connected, a client 18 | can send a chat message using this format: 19 |

20 |         {"user": "the name of the user",
21 |          "message": "the chat message"}
22 |         
23 | Each chat message sent to the server will be echoed back to all 24 | currently connected clients including the client that sent the 25 | message. The server will also include the timestamp on the server 26 | when the message was received. The chat messages echoed back 27 | from the server will follow this format: 28 |
29 |         {"user": "the name of the user that sent the message",
30 |          "message": "the echoed chat message",
31 |          "timestamp": "the server timestamp - MM/dd/yyyy h:mm:ss a z"}
32 |         
33 |

34 |

The REST endpoint is available at 35 | https://localhost:8181/javaee-javascript/resources/todo. The 36 | REST endpoint also communicates with the client using JSON. 37 | To do items are stored under various usernames. To retrieve 38 | the current to do items for a given user, the client must issue 39 | a GET request to this URL: 40 | https://localhost:8181/javaee-javascript/resources/todo/{username}. 41 | The server will respond with the list of current to do items for the 42 | user following this format: 43 |

44 |         [{"id": "to do item ID",
45 |          "description": "item 1 description",
46 |          "completed": "true|false"},
47 |          {"id": "to do item ID",
48 |          "description": "item 2 description",
49 |          "completed": "true|false"}
50 |          ...]
51 |     
52 | To add an item for a user, the client must issue a POST request to 53 | this URL: 54 | https://localhost:8181/javaee-javascript/resources/todo/{username}. 55 | The POST body must follow this format: 56 |
57 |         {"description": "item description"}
58 |     
59 | An ID will be automatically assigned and the item will be assumed to be 60 | incomplete. The POST response will return the ID assigned to the item as plain text (not JSON). 61 | To modify a to do item, the client must issue a PUT 62 | request to this URL: 63 | https://localhost:8181/javaee-javascript/resources/todo/{username}/{id}. 64 | The PUT request body must follow this format: 65 |
66 |         {"id": "to do item ID",
67 |          "description": "item description",
68 |          "completed": "true|false"}
69 |     
70 | To delete a to do item, the client must issue a DELETE request to 71 | this URL: 72 | https://localhost:8181/javaee-javascript/resources/todo/{username}/{id}. 73 |

74 | 75 | -------------------------------------------------------------------------------- /src/main/webapp/chat.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Chat 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 |
25 | 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 |
{{message.timestamp}}{{message.user}}{{message.message}}
34 |
35 | 36 | {{messages.length}} in chat 37 | 38 |
39 | 41 |
42 |
43 | -------------------------------------------------------------------------------- /src/main/webapp/css/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcandidosilva/javaee-javascript/ac0a8a2d5a6467847b89341f69e705aa438f5bff/src/main/webapp/css/.gitkeep -------------------------------------------------------------------------------- /src/main/webapp/css/chat/application.css: -------------------------------------------------------------------------------- 1 | #chat-panel { 2 | margin: 20px; 3 | border: 1px solid #cfcfcf; 4 | width: 720px; 5 | padding: 20px; 6 | } 7 | 8 | .chat-label { 9 | color: #444; 10 | font-size: 20px; 11 | font-weight: bold; 12 | margin-bottom: 15px; 13 | } 14 | 15 | .chat-box { 16 | border: 1px solid #cfcfcf; 17 | height: 400px; 18 | overflow: auto; 19 | padding: 2px; 20 | width: 100%; 21 | } 22 | 23 | .message-list { 24 | width: 100%; 25 | } 26 | 27 | .mesage-panel { 28 | font-family: Helvetica, sans-serif; 29 | font-size: 18px; 30 | border-bottom: 1px dotted #cfcfcf; 31 | } 32 | 33 | .timestamp-column { 34 | font-size: 14px; 35 | color: darkgray; 36 | width: 27%; 37 | padding: 4px; 38 | } 39 | 40 | .user-column { 41 | font-weight: bold; 42 | font-size: 20px; 43 | width: 12%; 44 | padding: 4px; 45 | } 46 | 47 | .message-column { 48 | width: 51%; 49 | padding: 4px; 50 | } 51 | 52 | .textbox { 53 | display: block; 54 | font-family: Helvetica, sans-serif; 55 | font-size: 20px; 56 | line-height: normal; 57 | margin: 5px auto 0; 58 | padding: 6px; 59 | width: 100%; 60 | } 61 | 62 | .message-count-label { 63 | display: block; 64 | text-align: left; 65 | color: #666; 66 | font-family: Helvetica, sans-serif; 67 | font-size: 16px; 68 | margin: 5px auto 0; 69 | padding: 6px; 70 | } -------------------------------------------------------------------------------- /src/main/webapp/css/lib/angular-growl.min.css: -------------------------------------------------------------------------------- 1 | /** 2 | * angular-growl - v0.4.0 - 2013-11-19 3 | * https://github.com/marcorinck/angular-growl 4 | * Copyright (c) 2013 Marco Rinck; Licensed MIT 5 | */ 6 | 7 | .growl{position:fixed;top:10px;right:10px;float:right;width:250px}.growl-item.ng-enter,.growl-item.ng-leave{-webkit-transition:.5s linear all;-moz-transition:.5s linear all;-o-transition:.5s linear all;transition:.5s linear all}.growl-item.ng-enter,.growl-item.ng-leave.ng-leave-active{opacity:0}.growl-item.ng-leave,.growl-item.ng-enter.ng-enter-active{opacity:1} -------------------------------------------------------------------------------- /src/main/webapp/css/todo/application.css: -------------------------------------------------------------------------------- 1 | li { 2 | border-bottom: 1px dotted #cfcfcf; 3 | font-size: 20px; 4 | padding: 6px; 5 | position: relative; 6 | } 7 | 8 | .center { 9 | margin-top: 20px; 10 | text-align: center; 11 | width: 100%; 12 | } 13 | 14 | #todo-panel { 15 | border: 1px solid #cfcfcf; 16 | margin: auto; 17 | text-align: center; 18 | width: 475px; 19 | padding: 20px; 20 | } 21 | 22 | .todo-label { 23 | color: #444; 24 | font-size: 20px; 25 | font-weight: bold; 26 | text-align: center; 27 | margin-bottom: 15px; 28 | } 29 | 30 | #todo-list { 31 | margin: 1em auto; 32 | text-align: left; 33 | width: 428px; 34 | list-style: none; 35 | padding: 0; 36 | } 37 | 38 | .completed { 39 | color: #666; 40 | text-decoration: line-through; 41 | } 42 | 43 | .hidden { display: none; } 44 | 45 | .textbox { 46 | display: block; 47 | font-family: Helvetica, sans-serif; 48 | font-size: 20px; 49 | line-height: normal; 50 | margin: 5px auto 0; 51 | padding: 6px; 52 | width: 420px; 53 | } 54 | 55 | .todo-item-remove-button { 56 | position: absolute; 57 | right: 0; 58 | top: 12px; 59 | } 60 | 61 | .todo-item-remove-icon { 62 | /* 63 | Delete icon courtesy of The Noun Project: 64 | http://thenounproject.com/noun/delete/ 65 | */ 66 | background: url(../../images/remove.png) no-repeat; 67 | display: block; 68 | height: 16px; 69 | opacity: 0.5; 70 | width: 23px; 71 | visibility: hidden; 72 | } 73 | 74 | li:hover .todo-item-remove-icon { visibility: visible; } 75 | li .todo-item-remove-icon:hover { opacity: 1.0 } 76 | 77 | .item-count-label { 78 | display: block; 79 | text-align: left; 80 | color: #666; 81 | font-family: Helvetica, sans-serif; 82 | font-size: 16px; 83 | margin: 5px auto 0; 84 | padding: 6px; 85 | } -------------------------------------------------------------------------------- /src/main/webapp/images/remove.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcandidosilva/javaee-javascript/ac0a8a2d5a6467847b89341f69e705aa438f5bff/src/main/webapp/images/remove.png -------------------------------------------------------------------------------- /src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | JavaScript/HTML5 Rich Clients with Java EE 7 4 | 5 | 6 | 7 |

JavaScript/HTML5 Rich Clients with Java EE 7

8 |

This application demonstrates how Java EE 7 can be an extremely 9 | effective backend for JavaScript/HTML5 rich clients. The application 10 | shows the use of APIs like WebSocket, JSON-P, JAX-RS, CDI, 11 | Bean Validation, EJB 3 and JPA. AngularJS is used as the JavaScript 12 | front-end framework. However, the choice of AngularJS is somewhat 13 | arbitrary and not central to the application. The concepts and backend 14 | could also be utilized by Backbone.js, Ember.js, KnockoutJS, JQuery or 15 | perhaps even plain JavaScript.

16 |

17 | There are two main parts of the application: 18 |

19 | 23 |

Note that while this page is not secured, both the interfaces are 24 | secured using HTTP BASIC authentication and SSL. You 25 | can set the usernames and SHA-256 encrypted passwords in the SQL load 26 | scripts of the application source.

27 |

The backend REST and WebSocket APIs are documented 28 | here.

29 | 30 | -------------------------------------------------------------------------------- /src/main/webapp/javascript/chat/application.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var chatApplication = angular.module('chatApplication', ['chatControllers', 4 | 'chatServices', 'chatDirectives', 'angular-growl']); 5 | 6 | chatApplication.config(['growlProvider', function(growlProvider) { 7 | growlProvider.globalTimeToLive(2000); 8 | }]); -------------------------------------------------------------------------------- /src/main/webapp/javascript/chat/controllers.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var chatControllers = angular.module('chatControllers', []); 4 | 5 | chatControllers.controller('ChatController', ['$scope', 'growl', 'Chat', 6 | function($scope, growl, Chat) { 7 | $scope.user = principal; 8 | $scope.messages = []; 9 | $scope.newMessage = ''; 10 | 11 | Chat.setListener( 12 | function(message) { 13 | $scope.messages.push(message); 14 | $scope.newMessage = ''; 15 | $scope.$apply(); 16 | }, 17 | function(error) { 18 | growl.addErrorMessage(error); 19 | $scope.$apply(); 20 | }); 21 | 22 | $scope.send = function() { 23 | Chat.send(principal, $scope.newMessage); 24 | }; 25 | }]); -------------------------------------------------------------------------------- /src/main/webapp/javascript/chat/directives.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var chatDirectives = angular.module('chatDirectives', []); 3 | 4 | /** 5 | * Scrolls the element to the bottom when the expression passed in via the 6 | * attribute becomes true 7 | */ 8 | chatDirectives.directive('autoScroll', function($timeout) { 9 | return function(scope, element, attributes) { 10 | scope.$watch(attributes.autoScroll, function() { 11 | $timeout(function() { 12 | jQuery(element).animate( 13 | {scrollTop: jQuery(element).prop("scrollHeight")}, 'slow'); 14 | }, 0, false); 15 | }); 16 | }; 17 | }); -------------------------------------------------------------------------------- /src/main/webapp/javascript/chat/services.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var chatServices = angular.module('chatServices', []); 4 | 5 | chatServices.factory('Chat', [function() { 6 | return new function() { 7 | var websocket = new WebSocket('wss://localhost:8181/javaee-javascript/chat'); 8 | this.send = function(sender, value) { 9 | websocket.send(JSON.stringify({user: sender, message: value})); 10 | }; 11 | this.setListener = function(messageListener, errorListener) { 12 | websocket.onmessage = function(message) { 13 | var parsedMessage = JSON.parse(message.data); 14 | 15 | if (!parsedMessage.error) { 16 | messageListener(parsedMessage); 17 | } else { 18 | errorListener(parsedMessage.error); 19 | } 20 | }; 21 | }; 22 | }; 23 | }]); -------------------------------------------------------------------------------- /src/main/webapp/javascript/lib/angular-growl.min.js: -------------------------------------------------------------------------------- 1 | /** 2 | * angular-growl - v0.4.0 - 2013-11-19 3 | * https://github.com/marcorinck/angular-growl 4 | * Copyright (c) 2013 Marco Rinck; Licensed MIT 5 | */ 6 | angular.module("angular-growl",[]),angular.module("angular-growl").directive("growl",["$rootScope",function(a){"use strict";return{restrict:"A",template:'
',replace:!1,scope:!0,controller:["$scope","$timeout","growl",function(b,c,d){function e(a){b.messages.push(a),a.ttl&&-1!==a.ttl&&c(function(){b.deleteMessage(a)},a.ttl)}var f=d.onlyUnique();b.messages=[],a.$on("growlMessage",function(a,c){var d;f?(angular.forEach(b.messages,function(a){c.text===a.text&&c.severity===a.severity&&(d=!0)}),d||e(c)):e(c)}),b.deleteMessage=function(a){var c=b.messages.indexOf(a);c>-1&&b.messages.splice(c,1)},b.computeClasses=function(a){return{"alert-success":"success"===a.severity,"alert-error":"error"===a.severity,"alert-danger":"error"===a.severity,"alert-info":"info"===a.severity,"alert-warning":"warn"===a.severity}}}]}}]),angular.module("angular-growl").provider("growl",function(){"use strict";var a=null,b=!1,c="messages",d="text",e="severity",f=!0;this.globalTimeToLive=function(b){a=b},this.globalEnableHtml=function(a){b=a},this.messagesKey=function(a){c=a},this.messageTextKey=function(a){d=a},this.messageSeverityKey=function(a){e=a},this.onlyUniqueMessages=function(a){f=a},this.serverMessagesInterceptor=["$q","growl",function(a,b){function d(a){a.data[c]&&a.data[c].length>0&&b.addServerMessages(a.data[c])}function e(a){return d(a),a}function f(b){return d(b),a.reject(b)}return function(a){return a.then(e,f)}}],this.$get=["$rootScope","$filter",function(c,g){function h(a){p&&(a.text=p(a.text)),c.$broadcast("growlMessage",a)}function i(c,d,e){var f,g=d||{};f={text:c,severity:e,ttl:g.ttl||a,enableHtml:g.enableHtml||b},h(f)}function j(a,b){i(a,b,"warn")}function k(a,b){i(a,b,"error")}function l(a,b){i(a,b,"info")}function m(a,b){i(a,b,"success")}function n(a){var b,c,f,g;for(g=a.length,b=0;g>b;b++)if(c=a[b],c[d]&&c[e]){switch(c[e]){case"warn":f="warn";break;case"success":f="success";break;case"info":f="info";break;case"error":f="error"}i(c[d],void 0,f)}}function o(){return f}var p;try{p=g("translate")}catch(q){}return{addWarnMessage:j,addErrorMessage:k,addInfoMessage:l,addSuccessMessage:m,addServerMessages:n,onlyUnique:o}}]}); -------------------------------------------------------------------------------- /src/main/webapp/javascript/todo/application.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var todoApplication = angular.module('todoApplication', 4 | ['todoControllers', 'todoServices', 'todoDirectives', 'angular-growl']); 5 | 6 | todoApplication.config(['growlProvider', function(growlProvider) { 7 | growlProvider.globalTimeToLive(2000); 8 | }]); -------------------------------------------------------------------------------- /src/main/webapp/javascript/todo/controllers.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var todoControllers = angular.module('todoControllers', []); 4 | 5 | todoControllers.controller('ToDoController', ['$scope', 'growl', 'ToDo', 6 | function($scope, growl, ToDo) { 7 | $scope.user = principal; 8 | $scope.newToDoDescription = ''; 9 | $scope.itemToEdit = null; 10 | 11 | $scope.items = ToDo.query(); 12 | 13 | $scope.addItem = function() { 14 | var item = new ToDo({description: $scope.newToDoDescription}); 15 | item.$save( 16 | function(data) { 17 | // Success. 18 | $scope.items.push(data); 19 | $scope.newToDoDescription = ''; 20 | }, 21 | function(error) { 22 | // Error. 23 | growl.addErrorMessage(error.data.pop().message); 24 | }); 25 | }; 26 | 27 | $scope.editItem = function(item) { 28 | $scope.itemToEdit = item; 29 | $scope.itemBackup = angular.extend({}, item); 30 | }; 31 | 32 | $scope.commitEditItem = function(item) { 33 | item.$update( 34 | function() { 35 | // Success. 36 | $scope.itemToEdit = null; 37 | }, 38 | function(error) { 39 | // Error. 40 | growl.addErrorMessage(error.data.pop().message); 41 | }); 42 | }; 43 | 44 | $scope.revertEditing = function(item) { 45 | $scope.items[$scope.items.indexOf(item)] = $scope.itemBackup; 46 | $scope.itemToEdit = null; // Call commit instead? 47 | }; 48 | 49 | $scope.removeItem = function(item) { 50 | item.$remove(function() { 51 | $scope.items.splice($scope.items.indexOf(item), 1); 52 | }); 53 | }; 54 | }]); -------------------------------------------------------------------------------- /src/main/webapp/javascript/todo/directives.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var todoDirectives = angular.module('todoDirectives', []); 4 | 5 | /** 6 | * Applies the expression passed in via the attribute when the escape key is pressed 7 | */ 8 | todoDirectives.directive('onEscape', function() { 9 | var ESCAPE_KEY = 27; 10 | return function(scope, element, attributes) { 11 | element.bind('keydown', function(event) { 12 | if (event.keyCode === ESCAPE_KEY) { 13 | scope.$apply(attributes.onEscape); 14 | } 15 | }); 16 | }; 17 | }); 18 | 19 | /** 20 | * Gets focus to the element when the expression passed in via the attribute becomes true 21 | */ 22 | todoDirectives.directive('focusWhen', function($timeout) { 23 | return function(scope, element, attributes) { 24 | scope.$watch(attributes.focusWhen, function(watchedValue) { 25 | if (watchedValue) { 26 | $timeout(function() { 27 | element[0].focus(); 28 | }, 0, false); 29 | } 30 | }); 31 | }; 32 | }); -------------------------------------------------------------------------------- /src/main/webapp/javascript/todo/services.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var todoServices = angular.module('todoServices', ['ngResource']); 4 | 5 | todoServices.factory('ToDo', ['$resource', 6 | function($resource) { 7 | return $resource( 8 | 'https://localhost:8181/javaee-javascript/resources/todo/:userId/:itemId', 9 | {userId: principal, itemId: '@id'}, {'update': {method: 'PUT'}}); 10 | }]); -------------------------------------------------------------------------------- /src/main/webapp/todo.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | To Do Items 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 |
25 |
26 | 27 |
28 | 31 |
32 |
33 |
    34 |
  • 35 |
    36 | 37 | {{item.description}} 38 | 41 | 42 | 43 |
    44 |
    45 |
    46 | 52 |
    53 |
    54 |
  • 55 |
56 | 57 | {{items.length}} on your list 58 | 59 |
60 |
61 |
62 | --------------------------------------------------------------------------------