├── .gitignore ├── .travis.yml ├── Dockerfile ├── LICENSE ├── README.md ├── README_MONGO.md ├── README_kubernetes.md ├── docs └── GettingStarted.gif ├── kubernetes └── deployment.yaml ├── manifest.yml ├── pom.xml ├── server.xml └── src ├── main ├── java │ ├── application │ │ └── Application.java │ └── wasdev │ │ └── sample │ │ ├── Visitor.java │ │ ├── rest │ │ └── VisitorAPI.java │ │ └── store │ │ ├── CloudantVisitorStore.java │ │ ├── MongoDbVisitorStore.java │ │ ├── VCAPHelper.java │ │ ├── VisitorStore.java │ │ └── VisitorStoreFactory.java ├── resources │ ├── cloudant.properties │ └── mongo.properties ├── webapp │ ├── WEB-INF │ │ └── ibm-web-ext.xml │ ├── antixss.js │ ├── index.html │ ├── js │ │ └── lib │ │ │ └── jquery.i18n │ │ │ ├── jquery.i18n.emitter.bidi.js │ │ │ ├── jquery.i18n.emitter.js │ │ │ ├── jquery.i18n.fallbacks.js │ │ │ ├── jquery.i18n.js │ │ │ ├── jquery.i18n.language.js │ │ │ ├── jquery.i18n.messagestore.js │ │ │ └── jquery.i18n.parser.js │ └── styles.css └── wlp │ └── server.xml └── test └── java └── test └── TestApplication.java /.gitignore: -------------------------------------------------------------------------------- 1 | target/* 2 | |target/GetStartedJava.war 3 | .settings 4 | .apt_generated 5 | .classpath 6 | .factorypath 7 | .project 8 | /target/ 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | sudo: false 3 | script: mvn clean verify -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM websphere-liberty:microProfile 2 | COPY server.xml /config/ 3 | ADD target/GetStartedJava.war /opt/ibm/wlp/usr/servers/defaultServer/dropins/ 4 | ENV LICENSE accept 5 | EXPOSE 9080 6 | 7 | ## Running the container locally 8 | # mvn clean install 9 | # docker build -t getstartedjava:latest . 10 | # docker run -d --name myjavacontainer getstartedjava 11 | # docker run -p 9080:9080 --name myjavacontainer getstartedjava 12 | # Visit http://localhost:9080/GetStartedJava/ 13 | 14 | ## Push container to IBM Cloud 15 | # docker tag getstartedjava:latest registry.ng.bluemix.net//getstartedjava:latest 16 | # docker push registry.ng.bluemix.net//getstartedjava:latest 17 | # ibmcloud ic images # Verify new image 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Liberty getting started application 2 | The Getting Started tutorial for Liberty uses this sample application to provide you with a sample workflow for working with any Liberty app on IBM Cloud or in IBM Cloud Private; you set up a development environment, deploy an app locally and on the cloud, and then integrate an IBM Cloud database service in your app. 3 | 4 | The Liberty app may use either the [Cloudant Java Client](https://github.com/cloudant/java-cloudant) or the [MongoDB Java Client](https://mongodb.github.io/mongo-java-driver) to add information to a database and then return information from a database to the UI. 5 | 6 |

7 | 8 | Gif of the sample app contains a title that says, Welcome, a prompt asking the user to enter their name, and a list of the database contents which are the names Joe, Jane, and Bob. The user enters the name, Mary and the screen refreshes to display, Hello, Mary, I've added you to the database. The database contents listed are now Mary, Joe, Jane, and Bob. 9 | 10 |

11 | 12 | ## Before you begin 13 | 14 | You'll need a [IBM Cloud account](https://console.ng.bluemix.net/registration/), [Git](https://git-scm.com/downloads), [Cloud Foundry CLI](https://github.com/cloudfoundry/cli#downloads), and [Maven](https://maven.apache.org/download.cgi) installed. If you use [IBM Cloud Private](https://www.ibm.com/cloud-computing/products/ibm-cloud-private/), you need access to the [IBM Cloud Private Cloud Foundry](https://www.ibm.com/support/knowledgecenter/en/SSBS6K_2.1.0/cloud_foundry/overview.html) environment. 15 | 16 | ## Instructions 17 | 18 | **IBM Cloud Cloud Foundry**: [Getting started tutorial for Liberty](https://cloud.ibm.com/docs/cloud-foundry-public?topic=cloud-foundry-public-getting-started-liberty). 19 | 20 | **IBM Cloud Kubernetes Service**: [README_kubernetes.md](README_kubernetes.md) 21 | 22 | **IBM Cloud Private**: The starter application for IBM Cloud Private guides you through a similar process. However, instead of hosting both your service and application in the same cloud environment, you use a user-provided service. This guide shows you how to deploy your application to IBM Cloud Private and bind it to a Cloudant Database in IBM Cloud. For the complete procedure, see [Working with user-provided services and the Liberty starter app](https://www.ibm.com/support/knowledgecenter/SSBS6K_2.1.0/cloud_foundry/buildpacks/buildpacks_using_Libertyapp.html). 23 | 24 | ## 25 | -------------------------------------------------------------------------------- /README_MONGO.md: -------------------------------------------------------------------------------- 1 | ## Connecting to a MongoDB Database 2 | 3 | You'll need access to a MongoDB instance. The below steps outline how to do this: 4 | 5 | 1. Sign in to your IBM Cloud Console, and select the 'Create Resource' button in the top right of the page. 6 | 2. Type 'MongoDB' in the search field and select 'Compose for MongoDB' under the 'Data & Analytics' section. 7 | 3. Check that the name/region/organization/space/pricing fields are accurate. 8 | 4. Select the 'Create' button in the bottom left of the page. 9 | 5. Return to the IBM Cloud dashboard and select the newly created service. 10 | 6. In the sidebar on the left side of the page, select 'Service Credentials' 11 | 7. Select the 'New Credential' button, and then the 'Add' button in the page that appears. 12 | 13 | To prepare the application for use in a local environment, use the below steps. Otherwise, skip the steps to move on to deploying the application to IBM Cloud: 14 | 15 | 1. The new credential will be listed in the table on the same page. Select the 'View credentials' option in the 'Actions' column. 16 | 2. Copy the contents of the 'uri' field to the 'mongo_url' field in 'src/main/resources/mongo.properties'. 17 | 3. Copy the contents of the 'ca_certificate_base64' field to the 'mongo_ssl' field in the same file as the previous step. 18 | 19 | Before deploying the application, make sure that it builds successfully by running `mvn install liberty:run-server` from the root directory of the project (where this README document is located). If no errors are shown, run `cf push` to deploy the application. Once the deployment process completes, run `cf bind-service GetStartedJava [SERVICE_NAME]`, where [SERVICE_NAME] is the name of your MongoDB service. Finally, run `cf restage GetStartedJava`. Once this finishes, you can access the application using the URL provided in the output from the 'cf push' command from earlier. 20 | 21 | ## Additional Notes on Changes 22 | 23 | The application may work with either Cloudant or MongoDB. However, if both services are available, **the application will default to MongoDB**. 24 | 25 | When deployed on IBM Cloud, this application does **not** require bound MongoDB services to have any permutation of 'mongodb' in the name. User-provided services (as created with the cf utility) are also acceptable. Finally, a TrustStore does **not** have to be created prior to building/running the application. This is generated during runtime using the MongoDB service's credentials (Though the application will accept or add to a preconfigured TrustStore if one is provided). Generated TrustStores are stored in the working directory of the application, and can be deleted when the application finishes execution. 26 | -------------------------------------------------------------------------------- /README_kubernetes.md: -------------------------------------------------------------------------------- 1 | # Deploy to IBM Cloud Kubernetes Service 2 | 3 | Follow these instructions to deploy this application to a Kubernetes cluster and connect it with a Cloudant database. 4 | 5 | ## Download 6 | 7 | ```bash 8 | git clone https://github.com/IBM-Cloud/get-started-java 9 | cd get-started-java 10 | mvn clean install 11 | ``` 12 | 13 | ## Build Docker Image 14 | 15 | 1. Find your container registry **namespace** by running `ibmcloud cr namespaces`. If you don't have any, create one using `ibmcloud cr namespace-add ` 16 | 17 | 2. Identify your **Container Registry** by running `ibmcloud cr info` (Ex: registry.ng.bluemix.net) 18 | 19 | 3. Build and tag (`-t`)the docker image by running the command below replacing REGISTRY and NAMESPACE with he appropriate values. 20 | 21 | ```sh 22 | docker build . -t //myapp:v1.0.0 23 | ``` 24 | Example: `docker build . -t registry.ng.bluemix.net/mynamespace/myapp:v1.0.0` 25 | 26 | 4. Push the docker image to your Container Registry on IBM Cloud 27 | 28 | ```sh 29 | docker push //myapp:v1.0.0 30 | ``` 31 | 32 | ## Deploy 33 | 34 | #### Create a Kubernetes cluster 35 | 36 | 1. [Creating a Kubernetes cluster in IBM Cloud](https://console.bluemix.net/docs/containers/container_index.html#clusters). 37 | 2. Follow the instructions in the Access tab to set up your `kubectl` cli. 38 | 39 | #### Create a Cloudant Database 40 | 41 | 1. Go to the [Catalog](https://console.bluemix.net/catalog/) and create a new [Cloudant](https://console.bluemix.net/catalog/services/cloudant-nosql-db) database instance. 42 | 43 | 2. Choose `Legacy and IAM` for **Authentication** 44 | 45 | 3. Create new credentials under **Service Credentials** and copy value of the **url** field. 46 | 47 | 4. Create a Kubernetes secret with your Cloudant credentials. 48 | 49 | ```bash 50 | kubectl create secret generic cloudant --from-literal=url= 51 | ``` 52 | Example: 53 | ```bash 54 | kubectl create secret generic cloudant --from-literal=url=https://username:passw0rdf@username-bluemix.cloudant.com 55 | ``` 56 | 57 | #### Create the deployment 58 | 59 | 1. Replace `` and `` with the appropriate values in `kubernetes/deployment.yaml` 60 | 2. Create a deployment: 61 | ```shell 62 | kubectl create -f kubernetes/deployment.yaml 63 | ``` 64 | - **Paid Cluster**: Expose the service using an External IP and Loadbalancer 65 | ``` 66 | kubectl expose deployment get-started-java --type LoadBalancer --port 9080 --target-port 9080 67 | ``` 68 | 69 | - **Free Cluster**: Use the Worker IP and NodePort 70 | ```bash 71 | kubectl expose deployment get-started-java --type NodePort --port 9080 --target-port 9080 72 | ``` 73 | 74 | ### Access the application 75 | 76 | Verify **STATUS** of pod is `RUNNING` 77 | 78 | ```shell 79 | kubectl get pods -l app=get-started-java 80 | ``` 81 | 82 | **Standard (Paid) Cluster:** 83 | 84 | 1. Identify your LoadBalancer Ingress IP using `kubectl get service get-started-java` 85 | 2. Access your application at t `http://:9080/GetStartedJava` 86 | 87 | **Free Cluster:** 88 | 89 | 1. Identify your Worker Public IP using `ibmcloud cs workers YOUR_CLUSTER_NAME` 90 | 2. Identify the Node Port using `kubectl describe service get-started-java` 91 | 3. Access your application at `http://:/GetStartedJava` 92 | 93 | 94 | ## Clean Up 95 | ```bash 96 | kubectl delete deployment,service -l app=get-started-java 97 | kubectl delete secret cloudant 98 | ``` 99 | -------------------------------------------------------------------------------- /docs/GettingStarted.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM-Cloud/get-started-java/f6878de156ccee4bcec10252ddb35bda0a6db91c/docs/GettingStarted.gif -------------------------------------------------------------------------------- /kubernetes/deployment.yaml: -------------------------------------------------------------------------------- 1 | # Update values before use 2 | apiVersion: apps/v1 3 | kind: Deployment 4 | metadata: 5 | name: get-started-java 6 | labels: 7 | app: get-started-java 8 | spec: 9 | replicas: 1 10 | selector: 11 | matchLabels: 12 | app: get-started-java 13 | template: 14 | metadata: 15 | labels: 16 | app: get-started-java 17 | spec: 18 | containers: 19 | - name: get-started-java 20 | image: //myapp:v1.0.0 21 | ports: 22 | - containerPort: 9080 23 | imagePullPolicy: Always 24 | env: 25 | - name: CLOUDANT_URL 26 | valueFrom: 27 | secretKeyRef: 28 | name: cloudant 29 | key: url 30 | optional: true 31 | 32 | 33 | -------------------------------------------------------------------------------- /manifest.yml: -------------------------------------------------------------------------------- 1 | --- 2 | applications: 3 | - name: GetStartedJava 4 | random-route: true 5 | path: target/GetStartedJava.war 6 | memory: 256M 7 | instances: 1 8 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | net.wasdev.maven.parent 7 | java7-parent 8 | 1.4 9 | 10 | 11 | 12 | net.wasdev.wlp.sample 13 | GetStartedJava 14 | 1.0-SNAPSHOT 15 | war 16 | 17 | IBM Cloud Getting Started 18 | https://bluemix.net 19 | 20 | 21 | 22 | The Apache Software License, Version 2.0 23 | https://raw.github.com/WASdev/sample.servlet/master/LICENSE 24 | repo 25 | 26 | 27 | 28 | 29 | 30 | 31 | org.apache.geronimo.specs 32 | geronimo-servlet_3.0_spec 33 | 1.0 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.apache.cxf 41 | cxf-rt-rs-client 42 | 3.1.1 43 | test 44 | 45 | 46 | 47 | org.apache.geronimo.specs 48 | geronimo-servlet_3.0_spec 49 | provided 50 | 51 | 52 | 53 | org.mongodb 54 | mongodb-driver 55 | 3.4.2 56 | 57 | 58 | 59 | com.cloudant 60 | cloudant-client 61 | 2.7.0 62 | 63 | 64 | 65 | javax.ws.rs 66 | javax.ws.rs-api 67 | 2.0 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | org.apache.maven.plugins 77 | maven-war-plugin 78 | 79 | false 80 | GetStartedJava 81 | 82 | 83 | 84 | net.wasdev.wlp.maven.plugins 85 | liberty-maven-plugin 86 | 1.2.1 87 | 88 | 89 | 90 | 91 | 92 | net.wasdev.wlp.maven.plugins 93 | liberty-maven-plugin 94 | 95 | src/main/wlp/server.xml 96 | 97 | ../../../../../${project.build.finalName} 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /server.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | beanValidation-1.1 6 | cdi-1.2 7 | ejbLite-3.2 8 | el-3.0 9 | jaxrs-2.0 10 | jdbc-4.1 11 | jndi-1.0 12 | jpa-2.1 13 | jsf-2.2 14 | jsonp-1.0 15 | jsp-2.3 16 | managedBeans-1.0 17 | servlet-3.1 18 | websocket-1.1 19 | 20 | 21 | 22 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/main/java/application/Application.java: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright (c) 2018 IBM Corp. * 3 | * * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); * 5 | * you may not use this file except in compliance with the License. * 6 | * You may obtain a copy of the License at * 7 | * * 8 | * http://www.apache.org/licenses/LICENSE-2.0 * 9 | * * 10 | * Unless required by applicable law or agreed to in writing, software * 11 | * distributed under the License is distributed on an "AS IS" BASIS, * 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 13 | * See the License for the specific language governing permissions and * 14 | * limitations under the License. * 15 | ******************************************************************************/ 16 | package application; 17 | 18 | public class Application { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/wasdev/sample/Visitor.java: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * Copyright IBM Corp. 2018 * 3 | * * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); * 5 | * you may not use this file except in compliance with the License. * 6 | * You may obtain a copy of the License at * 7 | * * 8 | * http://www.apache.org/licenses/LICENSE-2.0 * 9 | * * 10 | * Unless required by applicable law or agreed to in writing, software * 11 | * distributed under the License is distributed on an "AS IS" BASIS, * 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 13 | * See the License for the specific language governing permissions and * 14 | * limitations under the License. * 15 | *****************************************************************************/ 16 | package wasdev.sample; 17 | 18 | /** 19 | * Represents a Visitor document stored in Cloudant. 20 | */ 21 | 22 | public class Visitor { 23 | private String _id; 24 | private String _rev; 25 | private String name = null; 26 | 27 | public Visitor() { 28 | this.name = ""; 29 | } 30 | 31 | /** 32 | * Gets the ID. 33 | * 34 | * @return The ID. 35 | */ 36 | public String get_id() { 37 | return _id; 38 | } 39 | 40 | /** 41 | * Sets the ID 42 | * 43 | * @param _id 44 | * The ID to set. 45 | */ 46 | public void set_id(String _id) { 47 | this._id = _id; 48 | } 49 | 50 | /** 51 | * Gets the revision of the document. 52 | * 53 | * @return The revision of the document. 54 | */ 55 | public String get_rev() { 56 | return _rev; 57 | } 58 | 59 | /** 60 | * Sets the revision. 61 | * 62 | * @param _rev 63 | * The revision to set. 64 | */ 65 | public void set_rev(String _rev) { 66 | this._rev = _rev; 67 | } 68 | 69 | /** 70 | * Gets the visitorName of the document. 71 | * 72 | * @return The name of the document. 73 | */ 74 | public String getName() { 75 | return name; 76 | } 77 | 78 | /** 79 | * Sets the name 80 | * 81 | * @param name 82 | * The visitorName to set. 83 | */ 84 | public void setName(String visitorName) { 85 | this.name = visitorName; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/wasdev/sample/rest/VisitorAPI.java: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright (c) 2018 IBM Corp. * 3 | * * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); * 5 | * you may not use this file except in compliance with the License. * 6 | * You may obtain a copy of the License at * 7 | * * 8 | * http://www.apache.org/licenses/LICENSE-2.0 * 9 | * * 10 | * Unless required by applicable law or agreed to in writing, software * 11 | * distributed under the License is distributed on an "AS IS" BASIS, * 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 13 | * See the License for the specific language governing permissions and * 14 | * limitations under the License. * 15 | ******************************************************************************/ 16 | package wasdev.sample.rest; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | import javax.ws.rs.ApplicationPath; 22 | import javax.ws.rs.Consumes; 23 | import javax.ws.rs.GET; 24 | import javax.ws.rs.POST; 25 | import javax.ws.rs.Path; 26 | import javax.ws.rs.Produces; 27 | import javax.ws.rs.core.Application; 28 | 29 | import com.google.gson.Gson; 30 | 31 | import wasdev.sample.Visitor; 32 | import wasdev.sample.store.VisitorStore; 33 | import wasdev.sample.store.VisitorStoreFactory; 34 | 35 | @ApplicationPath("api") 36 | @Path("/visitors") 37 | public class VisitorAPI extends Application { 38 | 39 | //Our database store 40 | VisitorStore store = VisitorStoreFactory.getInstance(); 41 | 42 | /** 43 | * Gets all Visitors. 44 | * REST API example: 45 | * 46 | * GET http://localhost:9080/GetStartedJava/api/visitors 47 | * 48 | * 49 | * Response: 50 | * 51 | * [ "Bob", "Jane" ] 52 | * 53 | * @return A collection of all the Visitors 54 | */ 55 | @GET 56 | @Path("/") 57 | @Produces({"application/json"}) 58 | public String getVisitors() { 59 | 60 | if (store == null) { 61 | return "[]"; 62 | } 63 | 64 | List names = new ArrayList(); 65 | for (Visitor doc : store.getAll()) { 66 | String name = doc.getName(); 67 | if (name != null){ 68 | names.add(name); 69 | } 70 | } 71 | return new Gson().toJson(names); 72 | } 73 | 74 | /** 75 | * Creates a new Visitor. 76 | * 77 | * REST API example: 78 | * 79 | * POST http://localhost:9080/GetStartedJava/api/visitors 80 | * 81 | * POST Body: 82 | * 83 | * { 84 | * "name":"Bob" 85 | * } 86 | * 87 | * Response: 88 | * 89 | * { 90 | * "id":"123", 91 | * "name":"Bob" 92 | * } 93 | * 94 | * @param visitor The new Visitor to create. 95 | * @return The Visitor after it has been stored. This will include a unique ID for the Visitor. 96 | */ 97 | @POST 98 | @Produces("application/text") 99 | @Consumes("application/json") 100 | public String newToDo(Visitor visitor) { 101 | if(store == null) { 102 | return String.format("Hello %s!", visitor.getName()); 103 | } 104 | store.persist(visitor); 105 | return String.format("Hello %s! I've added you to the database.", visitor.getName()); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/wasdev/sample/store/CloudantVisitorStore.java: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright (c) 2018 IBM Corp. * 3 | * * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); * 5 | * you may not use this file except in compliance with the License. * 6 | * You may obtain a copy of the License at * 7 | * * 8 | * http://www.apache.org/licenses/LICENSE-2.0 * 9 | * * 10 | * Unless required by applicable law or agreed to in writing, software * 11 | * distributed under the License is distributed on an "AS IS" BASIS, * 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 13 | * See the License for the specific language governing permissions and * 14 | * limitations under the License. * 15 | ******************************************************************************/ 16 | package wasdev.sample.store; 17 | 18 | import java.io.IOException; 19 | import java.net.URL; 20 | import java.util.Collection; 21 | import java.util.List; 22 | 23 | import com.cloudant.client.api.ClientBuilder; 24 | import com.cloudant.client.api.CloudantClient; 25 | import com.cloudant.client.api.Database; 26 | import com.google.gson.JsonObject; 27 | 28 | import wasdev.sample.Visitor; 29 | 30 | public class CloudantVisitorStore implements VisitorStore{ 31 | 32 | private Database db = null; 33 | private static final String databaseName = "mydb"; 34 | 35 | public CloudantVisitorStore(){ 36 | CloudantClient cloudant = createClient(); 37 | if(cloudant!=null){ 38 | db = cloudant.database(databaseName, true); 39 | } 40 | } 41 | 42 | public Database getDB(){ 43 | return db; 44 | } 45 | 46 | private static CloudantClient createClient() { 47 | 48 | String url; 49 | 50 | if (System.getenv("VCAP_SERVICES") != null) { 51 | // When running in IBM Cloud, the VCAP_SERVICES env var will have the credentials for all bound/connected services 52 | // Parse the VCAP JSON structure looking for cloudant. 53 | JsonObject cloudantCredentials = VCAPHelper.getCloudCredentials("cloudant"); 54 | if(cloudantCredentials == null){ 55 | System.out.println("No cloudant database service bound to this application"); 56 | return null; 57 | } 58 | url = cloudantCredentials.get("url").getAsString(); 59 | } else if (System.getenv("CLOUDANT_URL") != null) { 60 | url = System.getenv("CLOUDANT_URL"); 61 | } else { 62 | System.out.println("Running locally. Looking for credentials in cloudant.properties"); 63 | url = VCAPHelper.getLocalProperties("cloudant.properties").getProperty("cloudant_url"); 64 | if(url == null || url.length()==0){ 65 | System.out.println("To use a database, set the Cloudant url in src/main/resources/cloudant.properties"); 66 | return null; 67 | } 68 | } 69 | 70 | try { 71 | System.out.println("Connecting to Cloudant"); 72 | CloudantClient client = ClientBuilder.url(new URL(url)).build(); 73 | return client; 74 | } catch (Exception e) { 75 | System.out.println("Unable to connect to database"); 76 | //e.printStackTrace(); 77 | return null; 78 | } 79 | } 80 | 81 | @Override 82 | public Collection getAll(){ 83 | List docs; 84 | try { 85 | docs = db.getAllDocsRequestBuilder().includeDocs(true).build().getResponse().getDocsAs(Visitor.class); 86 | } catch (IOException e) { 87 | return null; 88 | } 89 | return docs; 90 | } 91 | 92 | @Override 93 | public Visitor get(String id) { 94 | return db.find(Visitor.class, id); 95 | } 96 | 97 | @Override 98 | public Visitor persist(Visitor td) { 99 | String id = db.save(td).getId(); 100 | return db.find(Visitor.class, id); 101 | } 102 | 103 | @Override 104 | public Visitor update(String id, Visitor newVisitor) { 105 | Visitor visitor = db.find(Visitor.class, id); 106 | visitor.setName(newVisitor.getName()); 107 | db.update(visitor); 108 | return db.find(Visitor.class, id); 109 | 110 | } 111 | 112 | @Override 113 | public void delete(String id) { 114 | Visitor visitor = db.find(Visitor.class, id); 115 | db.remove(id, visitor.get_rev()); 116 | 117 | } 118 | 119 | @Override 120 | public int count() throws Exception { 121 | return getAll().size(); 122 | } 123 | 124 | } 125 | -------------------------------------------------------------------------------- /src/main/java/wasdev/sample/store/MongoDbVisitorStore.java: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright (c) 2018 IBM Corp. * 3 | * * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); * 5 | * you may not use this file except in compliance with the License. * 6 | * You may obtain a copy of the License at * 7 | * * 8 | * http://www.apache.org/licenses/LICENSE-2.0 * 9 | * * 10 | * Unless required by applicable law or agreed to in writing, software * 11 | * distributed under the License is distributed on an "AS IS" BASIS, * 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 13 | * See the License for the specific language governing permissions and * 14 | * limitations under the License. * 15 | ******************************************************************************/ 16 | package wasdev.sample.store; 17 | 18 | import java.io.ByteArrayInputStream; 19 | import java.io.File; 20 | import java.io.FileInputStream; 21 | import java.io.FileNotFoundException; 22 | import java.io.FileOutputStream; 23 | import java.io.IOException; 24 | import java.lang.String; 25 | import java.security.cert.Certificate; 26 | import java.security.cert.CertificateException; 27 | import java.security.cert.CertificateFactory; 28 | import java.security.KeyStore; 29 | import java.security.KeyStoreException; 30 | import java.security.NoSuchAlgorithmException; 31 | import java.util.Base64; 32 | import java.util.Collection; 33 | import java.util.HashSet; 34 | import java.util.Set; 35 | 36 | import org.bson.Document; 37 | import com.google.gson.JsonObject; 38 | import com.mongodb.client.model.Filters; 39 | import com.mongodb.client.MongoCollection; 40 | import com.mongodb.client.MongoDatabase; 41 | import com.mongodb.client.MongoIterable; 42 | import com.mongodb.client.result.DeleteResult; 43 | import com.mongodb.MongoClient; 44 | import com.mongodb.MongoClientURI; 45 | 46 | import wasdev.sample.Visitor; 47 | 48 | public class MongoDbVisitorStore implements VisitorStore { 49 | 50 | private MongoDatabase db = null; 51 | private MongoCollection collection; 52 | private static final String databaseName = "mydb", 53 | collectionName = "mycollection"; 54 | 55 | public MongoDbVisitorStore() { 56 | MongoClient mongo = createClient(); 57 | if (mongo != null) { 58 | db = mongo.getDatabase(databaseName); 59 | if (db == null) { 60 | System.out.println("Could not find database with name \"" + 61 | databaseName + 62 | "\". A new database will be created."); 63 | } 64 | } 65 | collection = db.getCollection(collectionName); 66 | } 67 | 68 | public MongoDatabase getDB() { 69 | return db; 70 | } 71 | 72 | /** 73 | * Retrieve the file name for the KeyStore, or default to a file name of 74 | * "mongoKeyStore" in the current user's directory. 75 | * @return A String that either contains the file name specified in 76 | * javax.net.ssl.trustStore, or a "mongoKeyStore" prefixed by the 77 | * current user's directory. 78 | */ 79 | private static String getKeyStoreName() { 80 | String keyStoreName = System.getProperty("java.net.ssl.trustStore"); 81 | return keyStoreName == null ? System.getProperty("user.dir") + 82 | File.separator + "mongoKeyStore" : 83 | keyStoreName; 84 | } 85 | 86 | /** 87 | * Retrieve the password for the KeyStore, or default to "keypass". 88 | * 89 | * @return A String that either contains the password specified in 90 | * javax.net.ssl.trustStorePassword, or "keypass". 91 | */ 92 | private static String getKeyStorePass() { 93 | String keyStorePass = System.getProperty("javax.net.ssl.trustStorePassword"); 94 | return keyStorePass == null ? "keypass" : keyStorePass; 95 | } 96 | 97 | /** 98 | * Given a file name and password, either locate and load an existing 99 | * KeyStore or create a new one. 100 | * 101 | * @param keyStoreName String specifying the name of the KeyStore file. 102 | * @param keyStorePass String specifying the password for the KeyStore. 103 | * @return A KeyStore object located at the specified location, or null 104 | * if an error occurs. 105 | */ 106 | private static KeyStore initializeKeyStore(String keyStoreName, 107 | String keyStorePass) { 108 | KeyStore keyStore = null; 109 | try { 110 | keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); 111 | } catch (KeyStoreException e) { 112 | System.out.println("Could not get KeyStore instance."); 113 | //e.printStackTrace(); 114 | return null; 115 | } 116 | 117 | File file = new File(keyStoreName); 118 | 119 | FileInputStream inputStream = null; 120 | char [] keyStorePassArray = "null".toCharArray(); 121 | 122 | if (file.exists()) { 123 | System.out.println("Found existing TrustStore: \"" + keyStoreName + 124 | "\""); 125 | try { 126 | inputStream = new FileInputStream(file); 127 | } catch (FileNotFoundException e) { 128 | System.out.println("Could not locate TrustStore file."); 129 | //e.printStackTrace(); 130 | return null; 131 | } 132 | keyStorePassArray = keyStorePass.toCharArray(); 133 | } else { 134 | System.out.println("Creating new TrustStore: \"" + keyStoreName + 135 | "\""); 136 | } 137 | 138 | try { 139 | keyStore.load(inputStream, keyStorePassArray); 140 | } catch (IOException | NoSuchAlgorithmException | 141 | CertificateException e) { 142 | System.out.println("Could not load TrustStore."); 143 | //e.printStackTrace(); 144 | return null; 145 | } 146 | 147 | return keyStore; 148 | } 149 | 150 | /** 151 | * Create a Certificate object given a Base64-encoded String. This method 152 | * expects "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----" 153 | * endings to be removed from the string by the caller. 154 | * 155 | * @param certString Base64-encoded String describing a PEM-formatted SSL 156 | * certificate. 157 | * @return A Certificate object generated from the provided String, or 158 | * null if an error occurs. 159 | */ 160 | private static Certificate generateCertificate(String certString) { 161 | byte [] decoded = Base64.getDecoder().decode(certString); 162 | ByteArrayInputStream bytes = new ByteArrayInputStream(decoded); 163 | CertificateFactory certFactory = null; 164 | 165 | try { 166 | certFactory = CertificateFactory.getInstance("X.509"); 167 | } catch (CertificateException e) { 168 | System.out.println("Could not prepare certificate factory."); 169 | //e.printStackTrace(); 170 | return null; 171 | } 172 | 173 | Certificate cert = null; 174 | try { 175 | cert = certFactory.generateCertificate(bytes); 176 | } catch (CertificateException e) { 177 | System.out.println("Unable to generate certificate."); 178 | //e.printStackTrace(); 179 | return null; 180 | } 181 | return cert; 182 | } 183 | 184 | /** 185 | * Add a Base64-encoded, PEM-formatted SSL certificate to a specified 186 | * KeyStore. 187 | * 188 | * @param keyStore The KeyStore that will store the new certificate. 189 | * @param certString A Base64-encoded, PEM-formatted String describing an 190 | * SSL certificate. The "-----BEGIN CERTIFICATE-----" and 191 | * "-----END CERTIFICATE-----" endings should already be 192 | * removed by the caller. 193 | * @param keyStoreName A String specifying the file name of the KeyStore. 194 | * @param keyStorePass A String specifying the password for the KeyStore. 195 | */ 196 | private static void addCertToKeyStore(KeyStore keyStore, 197 | String certString) { 198 | Certificate cert = generateCertificate(certString); 199 | try { 200 | keyStore.setCertificateEntry("mykey", cert); 201 | } catch (KeyStoreException e) { 202 | System.out.println("Unable to add certificate to the TrustStore."); 203 | //e.printStackTrace(); 204 | } 205 | 206 | String keyStoreName = System.getProperty("javax.net.ssl.trustStore"), 207 | keyStorePass = System.getProperty("javax.net.ssl.trustStorePassword"); 208 | try { 209 | keyStore.store(new FileOutputStream(keyStoreName), keyStorePass.toCharArray()); 210 | System.out.println("Successfully added certificate to TrustStore."); 211 | } catch (KeyStoreException | IOException | NoSuchAlgorithmException | 212 | CertificateException e) { 213 | System.out.println("Unable to store updated TrustStore."); 214 | //e.printStackTrace(); 215 | } 216 | } 217 | 218 | private static MongoClient createClient() { 219 | 220 | //Update the following lines if using Self Signed Certs and uncomment lines 223, 224, 241 221 | String keyStoreName = null, 222 | keyStorePass = null; 223 | //System.out.println("Using TrustStore name \"" + keyStoreName + 224 | // "\" and password \"" + keyStorePass + "\""); 225 | KeyStore keyStore = initializeKeyStore(keyStoreName, keyStorePass); 226 | String url = null, 227 | certString = null; 228 | //End updates for self signed certs 229 | 230 | if (System.getenv("VCAP_SERVICES") != null) { 231 | // When running in Bluemix, the VCAP_SERVICES env var will have the 232 | // credentials for all bound/connected services 233 | // Parse the VCAP JSON structure looking for mongodb. 234 | JsonObject mongoCredentials = VCAPHelper.getCloudCredentials("mongodb"); 235 | 236 | if (mongoCredentials == null) { 237 | System.out.println("No MongoDB database service bound to this application"); 238 | return null; 239 | } 240 | url = mongoCredentials.get("uri").getAsString(); 241 | //certString = mongoCredentials.get("ca_certificate_base64").getAsString(); 242 | } else { 243 | System.out.println("Running locally. Looking for credentials in mongo.properties"); 244 | url = VCAPHelper.getLocalProperties("mongo.properties").getProperty("mongo_url"); 245 | if (url == null || url.length() == 0) { 246 | System.out.println("To use a database, set the MongoDB url in src/main/resources/mongo.properties"); 247 | return null; 248 | } 249 | 250 | certString = VCAPHelper.getLocalProperties("mongo.properties").getProperty("mongo_ssl"); 251 | } 252 | 253 | if (keyStore != null) { 254 | System.setProperty("javax.net.ssl.trustStore", keyStoreName); 255 | System.setProperty("javax.net.ssl.trustStorePassword", keyStorePass); 256 | 257 | addCertToKeyStore(keyStore, certString); 258 | } else { 259 | System.out.println("A TrustStore could not be found or created."); 260 | } 261 | 262 | try { 263 | System.out.println("Connecting to MongoDb"); 264 | MongoClient client = new MongoClient(new MongoClientURI(url)); 265 | return client; 266 | } catch (Exception e) { 267 | System.out.println("Unable to connect to database"); 268 | //e.printStackTrace(); 269 | return null; 270 | } 271 | } 272 | 273 | @Override 274 | public Collection getAll() { 275 | Collection docs = new HashSet(); 276 | try { 277 | MongoIterable foundDocs = collection.find(); 278 | for (Document doc : foundDocs) { 279 | Visitor visitor = new Visitor(); 280 | visitor.set_id(doc.getObjectId("_id").toString()); 281 | visitor.setName(doc.getString("name")); 282 | docs.add(visitor); 283 | } 284 | } catch (Exception e) { 285 | return null; 286 | } 287 | return docs; 288 | } 289 | 290 | @Override 291 | public Visitor get(String name) { 292 | Document doc = collection.find(Filters.eq("_id", name)).first(); 293 | Visitor visitor = new Visitor(); 294 | visitor.set_id(doc.getObjectId("_id").toString()); 295 | visitor.setName(doc.getString("name")); 296 | return visitor; 297 | } 298 | 299 | @Override 300 | public Visitor persist(Visitor td) { 301 | Document doc = new Document("name", td.getName()).append("count", 1); 302 | collection.insertOne(doc); 303 | doc = collection.find(Filters.eq("name", td.getName())).first(); 304 | td.set_id(doc.getObjectId("_id").toString()); 305 | return td; 306 | } 307 | 308 | @Override 309 | public Visitor update(String id, Visitor newVisitor) { 310 | collection.updateOne(Filters.eq("name", newVisitor.getName()), new Document("$set", new Document("name", newVisitor.getName()).append("count", 2))); 311 | 312 | Document myDoc = collection.find(Filters.eq("name", newVisitor.getName())).first(); 313 | newVisitor.set_id(myDoc.getObjectId("_id").toString()); 314 | return newVisitor; 315 | } 316 | 317 | @Override 318 | public void delete(String name) { 319 | collection.deleteOne(Filters.eq("name", name)); 320 | } 321 | 322 | @Override 323 | public int count() throws Exception { 324 | return (int) collection.count(); 325 | } 326 | } 327 | -------------------------------------------------------------------------------- /src/main/java/wasdev/sample/store/VCAPHelper.java: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright (c) 2018 IBM Corp. * 3 | * * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); * 5 | * you may not use this file except in compliance with the License. * 6 | * You may obtain a copy of the License at * 7 | * * 8 | * http://www.apache.org/licenses/LICENSE-2.0 * 9 | * * 10 | * Unless required by applicable law or agreed to in writing, software * 11 | * distributed under the License is distributed on an "AS IS" BASIS, * 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 13 | * See the License for the specific language governing permissions and * 14 | * limitations under the License. * 15 | ******************************************************************************/ 16 | package wasdev.sample.store; 17 | 18 | import java.io.IOException; 19 | import java.io.InputStream; 20 | import java.util.Properties; 21 | import java.util.Set; 22 | import java.util.Map.Entry; 23 | 24 | import com.google.gson.JsonArray; 25 | import com.google.gson.JsonElement; 26 | import com.google.gson.JsonObject; 27 | import com.google.gson.JsonParser; 28 | 29 | public class VCAPHelper { 30 | static String VCAP_SERVICES = System.getenv("VCAP_SERVICES"); 31 | 32 | public static JsonObject getCloudCredentials(String serviceName) { 33 | if(VCAP_SERVICES == null){ 34 | return null; 35 | } 36 | //Convert VCAP_SERVICES String to JSON 37 | JsonObject obj = (JsonObject) new JsonParser().parse(VCAP_SERVICES); 38 | 39 | // Look for the VCAP key that holds the service info 40 | Entry dbEntry = matchService(obj.entrySet(), 41 | serviceName); 42 | 43 | if (dbEntry == null) { 44 | System.out.println("VCAP_SERVICES: Could not find " + serviceName); 45 | return null; 46 | } 47 | 48 | obj = (JsonObject) ((JsonArray) dbEntry.getValue()).get(0); 49 | System.out.println("VCAP_SERVICES: Found " + dbEntry.getKey()); 50 | 51 | return (JsonObject) obj.get("credentials"); 52 | } 53 | 54 | private static Entry matchService(Set> entries, String serviceName) { 55 | for (Entry eachEntry : entries) { 56 | // Service with 'serviceName' in the name 57 | if (eachEntry.getKey().toLowerCase().contains(serviceName)) { 58 | return eachEntry; 59 | } 60 | // user-provided service with 'serviceName' in the name 61 | else if (eachEntry.getKey().equals("user-provided")) { 62 | JsonArray upss = eachEntry.getValue().getAsJsonArray(); 63 | for (JsonElement ups : upss) { 64 | String name = ups.getAsJsonObject().get("name").getAsString(); 65 | if (name.toLowerCase().contains(serviceName)) { 66 | return eachEntry; 67 | } 68 | } 69 | } 70 | // Service with 'serviceName' in the db_type (if present) 71 | else { 72 | JsonElement element = eachEntry.getValue(); 73 | if (element.isJsonArray()) { 74 | JsonArray array = element.getAsJsonArray(); 75 | if (array.size() > 0) { 76 | JsonObject container = array.get(0).getAsJsonObject(); 77 | if (container.has("credentials")) { 78 | JsonObject credentials = container.getAsJsonObject("credentials"); 79 | if (credentials.has("db_type")) { 80 | String dbType = credentials.get("db_type").getAsString(); 81 | if (dbType.equals(serviceName)) { 82 | return eachEntry; 83 | } 84 | } 85 | } 86 | } 87 | } 88 | } 89 | } 90 | return null; 91 | } 92 | 93 | public static Properties getLocalProperties(String fileName){ 94 | Properties properties = new Properties(); 95 | InputStream inputStream = VCAPHelper.class.getClassLoader().getResourceAsStream(fileName); 96 | try { 97 | properties.load(inputStream); 98 | } catch (IOException e) { 99 | e.printStackTrace(); 100 | } 101 | return properties; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/main/java/wasdev/sample/store/VisitorStore.java: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright IBM Corp. 2018 * 3 | * * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); * 5 | * you may not use this file except in compliance with the License. * 6 | * You may obtain a copy of the License at * 7 | * * 8 | * http://www.apache.org/licenses/LICENSE-2.0 * 9 | * * 10 | * Unless required by applicable law or agreed to in writing, software * 11 | * distributed under the License is distributed on an "AS IS" BASIS, * 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 13 | * See the License for the specific language governing permissions and * 14 | * limitations under the License. * 15 | ******************************************************************************/ 16 | package wasdev.sample.store; 17 | 18 | import java.util.Collection; 19 | 20 | import wasdev.sample.Visitor; 21 | 22 | /** 23 | * Defines the API for a ToDo store. 24 | * 25 | */ 26 | public interface VisitorStore { 27 | 28 | /** 29 | * Get the target db object. 30 | * 31 | * @return Database. 32 | * @throws Exception 33 | */ 34 | public Object getDB(); 35 | 36 | 37 | /** 38 | * Gets all Visitors from the store. 39 | * 40 | * @return All Visitors. 41 | * @throws Exception 42 | */ 43 | public Collection getAll(); 44 | 45 | /** 46 | * Gets an individual ToDo from the store. 47 | * @param id The ID of the ToDo to get. 48 | * @return The ToDo. 49 | */ 50 | public Visitor get(String id); 51 | 52 | /** 53 | * Persists a Visitor to the store. 54 | * @param td The ToDo to persist. 55 | * @return The persisted ToDo. The ToDo will not have a unique ID.. 56 | */ 57 | public Visitor persist(Visitor vi); 58 | 59 | /** 60 | * Updates a ToDo in the store. 61 | * @param id The ID of the Visitor to update. 62 | * @param td The Visitor with updated information. 63 | * @return The updated Visitor. 64 | */ 65 | public Visitor update(String id, Visitor vi); 66 | 67 | /** 68 | * Deletes a ToDo from the store. 69 | * @param id The ID of the Visitor to delete. 70 | */ 71 | public void delete(String id); 72 | 73 | /** 74 | * Counts the number of Visitors 75 | * @return The total number of Visitors. 76 | * @throws Exception 77 | */ 78 | public int count() throws Exception; 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/wasdev/sample/store/VisitorStoreFactory.java: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright (c) 2017 IBM Corp. * 3 | * * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); * 5 | * you may not use this file except in compliance with the License. * 6 | * You may obtain a copy of the License at * 7 | * * 8 | * http://www.apache.org/licenses/LICENSE-2.0 * 9 | * * 10 | * Unless required by applicable law or agreed to in writing, software * 11 | * distributed under the License is distributed on an "AS IS" BASIS, * 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 13 | * See the License for the specific language governing permissions and * 14 | * limitations under the License. * 15 | ******************************************************************************/ 16 | package wasdev.sample.store; 17 | 18 | import java.util.Properties; 19 | 20 | public class VisitorStoreFactory { 21 | 22 | private static VisitorStore instance; 23 | static { 24 | // Only use MongoDB if credentials are available. 25 | if (VCAPHelper.getCloudCredentials("mongodb") == null && 26 | (VCAPHelper.getLocalProperties("mongo.properties").getProperty("mongo_url") == null || 27 | VCAPHelper.getLocalProperties("mongo.properties").getProperty("mongo_url").equals(""))) { 28 | CloudantVisitorStore cvif = new CloudantVisitorStore(); 29 | if (cvif.getDB() != null) { 30 | instance = cvif; 31 | } 32 | } 33 | else { 34 | MongoDbVisitorStore cvif = new MongoDbVisitorStore(); 35 | if (cvif.getDB() != null) { 36 | instance = cvif; 37 | } 38 | } 39 | } 40 | 41 | public static VisitorStore getInstance() { 42 | return instance; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/resources/cloudant.properties: -------------------------------------------------------------------------------- 1 | # These properties are meant for local development only. 2 | # When running in IBM Cloud as a Cloud Foundry application, the credentials of bound services are available in the VCAP_SERVICES environment variable. 3 | # When running in IBM Cloud Kubernetes Service or in a container, set CLOUDANT_URL environment variable. 4 | 5 | # cloudant_url= username:password@username-bluemix.cloudant.com 6 | cloudant_url= -------------------------------------------------------------------------------- /src/main/resources/mongo.properties: -------------------------------------------------------------------------------- 1 | # These properties are meant for local development only and will not be read when application is running in Bluemix. 2 | # When running in Bluemix, the credentials of bound services are available in the VCAP_SERVICES environment variable. 3 | mongo_url= 4 | mongo_ssl= 5 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/ibm-web-ext.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/main/webapp/antixss.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright IBM Corporation 2017 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | "use strict"; 18 | 19 | class AntiXSS { 20 | static sanitizeInput(str) { 21 | return String(str).replace(/&(?!amp;|lt;|gt;)/g, '&').replace(//g, '>'); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Hello World 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 50 | 51 | 52 | 53 | 54 |
55 |

56 |
57 |

58 | 59 |
60 |

61 | 62 |

63 |
64 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 122 | -------------------------------------------------------------------------------- /src/main/webapp/js/lib/jquery.i18n/jquery.i18n.emitter.bidi.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * BIDI embedding support for jQuery.i18n 3 | * 4 | * Copyright (C) 2015, David Chan 5 | * 6 | * This code is dual licensed GPLv2 or later and MIT. You don't have to do 7 | * anything special to choose one license or the other and you don't have to 8 | * notify anyone which license you are using. You are free to use this code 9 | * in commercial projects as long as the copyright header is left intact. 10 | * See files GPL-LICENSE and MIT-LICENSE for details. 11 | * 12 | * @licence GNU General Public Licence 2.0 or later 13 | * @licence MIT License 14 | */ 15 | 16 | ( function ( $ ) { 17 | 'use strict'; 18 | var strongDirRegExp; 19 | 20 | /** 21 | * Matches the first strong directionality codepoint: 22 | * - in group 1 if it is LTR 23 | * - in group 2 if it is RTL 24 | * Does not match if there is no strong directionality codepoint. 25 | * 26 | * Generated by UnicodeJS (see tools/strongDir) from the UCD; see 27 | * https://phabricator.wikimedia.org/diffusion/GUJS/ . 28 | */ 29 | strongDirRegExp = new RegExp( 30 | '(?:' + 31 | '(' + 32 | '[\u0041-\u005a\u0061-\u007a\u00aa\u00b5\u00ba\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02b8\u02bb-\u02c1\u02d0\u02d1\u02e0-\u02e4\u02ee\u0370-\u0373\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0482\u048a-\u052f\u0531-\u0556\u0559-\u055f\u0561-\u0587\u0589\u0903-\u0939\u093b\u093d-\u0940\u0949-\u094c\u094e-\u0950\u0958-\u0961\u0964-\u0980\u0982\u0983\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd-\u09c0\u09c7\u09c8\u09cb\u09cc\u09ce\u09d7\u09dc\u09dd\u09df-\u09e1\u09e6-\u09f1\u09f4-\u09fa\u0a03\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a3e-\u0a40\u0a59-\u0a5c\u0a5e\u0a66-\u0a6f\u0a72-\u0a74\u0a83\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd-\u0ac0\u0ac9\u0acb\u0acc\u0ad0\u0ae0\u0ae1\u0ae6-\u0af0\u0af9\u0b02\u0b03\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b3e\u0b40\u0b47\u0b48\u0b4b\u0b4c\u0b57\u0b5c\u0b5d\u0b5f-\u0b61\u0b66-\u0b77\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bbe\u0bbf\u0bc1\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcc\u0bd0\u0bd7\u0be6-\u0bf2\u0c01-\u0c03\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c41-\u0c44\u0c58-\u0c5a\u0c60\u0c61\u0c66-\u0c6f\u0c7f\u0c82\u0c83\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd-\u0cc4\u0cc6-\u0cc8\u0cca\u0ccb\u0cd5\u0cd6\u0cde\u0ce0\u0ce1\u0ce6-\u0cef\u0cf1\u0cf2\u0d02\u0d03\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d-\u0d40\u0d46-\u0d48\u0d4a-\u0d4c\u0d4e\u0d57\u0d5f-\u0d61\u0d66-\u0d75\u0d79-\u0d7f\u0d82\u0d83\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0dcf-\u0dd1\u0dd8-\u0ddf\u0de6-\u0def\u0df2-\u0df4\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e4f-\u0e5b\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0ed0-\u0ed9\u0edc-\u0edf\u0f00-\u0f17\u0f1a-\u0f34\u0f36\u0f38\u0f3e-\u0f47\u0f49-\u0f6c\u0f7f\u0f85\u0f88-\u0f8c\u0fbe-\u0fc5\u0fc7-\u0fcc\u0fce-\u0fda\u1000-\u102c\u1031\u1038\u103b\u103c\u103f-\u1057\u105a-\u105d\u1061-\u1070\u1075-\u1081\u1083\u1084\u1087-\u108c\u108e-\u109c\u109e-\u10c5\u10c7\u10cd\u10d0-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1360-\u137c\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u167f\u1681-\u169a\u16a0-\u16f8\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1735\u1736\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17b6\u17be-\u17c5\u17c7\u17c8\u17d4-\u17da\u17dc\u17e0-\u17e9\u1810-\u1819\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1923-\u1926\u1929-\u192b\u1930\u1931\u1933-\u1938\u1946-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u19d0-\u19da\u1a00-\u1a16\u1a19\u1a1a\u1a1e-\u1a55\u1a57\u1a61\u1a63\u1a64\u1a6d-\u1a72\u1a80-\u1a89\u1a90-\u1a99\u1aa0-\u1aad\u1b04-\u1b33\u1b35\u1b3b\u1b3d-\u1b41\u1b43-\u1b4b\u1b50-\u1b6a\u1b74-\u1b7c\u1b82-\u1ba1\u1ba6\u1ba7\u1baa\u1bae-\u1be5\u1be7\u1bea-\u1bec\u1bee\u1bf2\u1bf3\u1bfc-\u1c2b\u1c34\u1c35\u1c3b-\u1c49\u1c4d-\u1c7f\u1cc0-\u1cc7\u1cd3\u1ce1\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u200e\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u214f\u2160-\u2188\u2336-\u237a\u2395\u249c-\u24e9\u26ac\u2800-\u28ff\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d70\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u302e\u302f\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309d-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u3190-\u31ba\u31f0-\u321c\u3220-\u324f\u3260-\u327b\u327f-\u32b0\u32c0-\u32cb\u32d0-\u32fe\u3300-\u3376\u337b-\u33dd\u33e0-\u33fe\u3400-\u4db5\u4e00-\u9fd5\ua000-\ua48c\ua4d0-\ua60c\ua610-\ua62b\ua640-\ua66e\ua680-\ua69d\ua6a0-\ua6ef\ua6f2-\ua6f7\ua722-\ua787\ua789-\ua7ad\ua7b0-\ua7b7\ua7f7-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua824\ua827\ua830-\ua837\ua840-\ua873\ua880-\ua8c3\ua8ce-\ua8d9\ua8f2-\ua8fd\ua900-\ua925\ua92e-\ua946\ua952\ua953\ua95f-\ua97c\ua983-\ua9b2\ua9b4\ua9b5\ua9ba\ua9bb\ua9bd-\ua9cd\ua9cf-\ua9d9\ua9de-\ua9e4\ua9e6-\ua9fe\uaa00-\uaa28\uaa2f\uaa30\uaa33\uaa34\uaa40-\uaa42\uaa44-\uaa4b\uaa4d\uaa50-\uaa59\uaa5c-\uaa7b\uaa7d-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaaeb\uaaee-\uaaf5\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab65\uab70-\uabe4\uabe6\uabe7\uabe9-\uabec\uabf0-\uabf9\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\ue000-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc]|\ud800[\udc00-\udc0b]|\ud800[\udc0d-\udc26]|\ud800[\udc28-\udc3a]|\ud800\udc3c|\ud800\udc3d|\ud800[\udc3f-\udc4d]|\ud800[\udc50-\udc5d]|\ud800[\udc80-\udcfa]|\ud800\udd00|\ud800\udd02|\ud800[\udd07-\udd33]|\ud800[\udd37-\udd3f]|\ud800[\uddd0-\uddfc]|\ud800[\ude80-\ude9c]|\ud800[\udea0-\uded0]|\ud800[\udf00-\udf23]|\ud800[\udf30-\udf4a]|\ud800[\udf50-\udf75]|\ud800[\udf80-\udf9d]|\ud800[\udf9f-\udfc3]|\ud800[\udfc8-\udfd5]|\ud801[\udc00-\udc9d]|\ud801[\udca0-\udca9]|\ud801[\udd00-\udd27]|\ud801[\udd30-\udd63]|\ud801\udd6f|\ud801[\ude00-\udf36]|\ud801[\udf40-\udf55]|\ud801[\udf60-\udf67]|\ud804\udc00|\ud804[\udc02-\udc37]|\ud804[\udc47-\udc4d]|\ud804[\udc66-\udc6f]|\ud804[\udc82-\udcb2]|\ud804\udcb7|\ud804\udcb8|\ud804[\udcbb-\udcc1]|\ud804[\udcd0-\udce8]|\ud804[\udcf0-\udcf9]|\ud804[\udd03-\udd26]|\ud804\udd2c|\ud804[\udd36-\udd43]|\ud804[\udd50-\udd72]|\ud804[\udd74-\udd76]|\ud804[\udd82-\uddb5]|\ud804[\uddbf-\uddc9]|\ud804\uddcd|\ud804[\uddd0-\udddf]|\ud804[\udde1-\uddf4]|\ud804[\ude00-\ude11]|\ud804[\ude13-\ude2e]|\ud804\ude32|\ud804\ude33|\ud804\ude35|\ud804[\ude38-\ude3d]|\ud804[\ude80-\ude86]|\ud804\ude88|\ud804[\ude8a-\ude8d]|\ud804[\ude8f-\ude9d]|\ud804[\ude9f-\udea9]|\ud804[\udeb0-\udede]|\ud804[\udee0-\udee2]|\ud804[\udef0-\udef9]|\ud804\udf02|\ud804\udf03|\ud804[\udf05-\udf0c]|\ud804\udf0f|\ud804\udf10|\ud804[\udf13-\udf28]|\ud804[\udf2a-\udf30]|\ud804\udf32|\ud804\udf33|\ud804[\udf35-\udf39]|\ud804[\udf3d-\udf3f]|\ud804[\udf41-\udf44]|\ud804\udf47|\ud804\udf48|\ud804[\udf4b-\udf4d]|\ud804\udf50|\ud804\udf57|\ud804[\udf5d-\udf63]|\ud805[\udc80-\udcb2]|\ud805\udcb9|\ud805[\udcbb-\udcbe]|\ud805\udcc1|\ud805[\udcc4-\udcc7]|\ud805[\udcd0-\udcd9]|\ud805[\udd80-\uddb1]|\ud805[\uddb8-\uddbb]|\ud805\uddbe|\ud805[\uddc1-\udddb]|\ud805[\ude00-\ude32]|\ud805\ude3b|\ud805\ude3c|\ud805\ude3e|\ud805[\ude41-\ude44]|\ud805[\ude50-\ude59]|\ud805[\ude80-\udeaa]|\ud805\udeac|\ud805\udeae|\ud805\udeaf|\ud805\udeb6|\ud805[\udec0-\udec9]|\ud805[\udf00-\udf19]|\ud805\udf20|\ud805\udf21|\ud805\udf26|\ud805[\udf30-\udf3f]|\ud806[\udca0-\udcf2]|\ud806\udcff|\ud806[\udec0-\udef8]|\ud808[\udc00-\udf99]|\ud809[\udc00-\udc6e]|\ud809[\udc70-\udc74]|\ud809[\udc80-\udd43]|\ud80c[\udc00-\udfff]|\ud80d[\udc00-\udc2e]|\ud811[\udc00-\ude46]|\ud81a[\udc00-\ude38]|\ud81a[\ude40-\ude5e]|\ud81a[\ude60-\ude69]|\ud81a\ude6e|\ud81a\ude6f|\ud81a[\uded0-\udeed]|\ud81a\udef5|\ud81a[\udf00-\udf2f]|\ud81a[\udf37-\udf45]|\ud81a[\udf50-\udf59]|\ud81a[\udf5b-\udf61]|\ud81a[\udf63-\udf77]|\ud81a[\udf7d-\udf8f]|\ud81b[\udf00-\udf44]|\ud81b[\udf50-\udf7e]|\ud81b[\udf93-\udf9f]|\ud82c\udc00|\ud82c\udc01|\ud82f[\udc00-\udc6a]|\ud82f[\udc70-\udc7c]|\ud82f[\udc80-\udc88]|\ud82f[\udc90-\udc99]|\ud82f\udc9c|\ud82f\udc9f|\ud834[\udc00-\udcf5]|\ud834[\udd00-\udd26]|\ud834[\udd29-\udd66]|\ud834[\udd6a-\udd72]|\ud834\udd83|\ud834\udd84|\ud834[\udd8c-\udda9]|\ud834[\uddae-\udde8]|\ud834[\udf60-\udf71]|\ud835[\udc00-\udc54]|\ud835[\udc56-\udc9c]|\ud835\udc9e|\ud835\udc9f|\ud835\udca2|\ud835\udca5|\ud835\udca6|\ud835[\udca9-\udcac]|\ud835[\udcae-\udcb9]|\ud835\udcbb|\ud835[\udcbd-\udcc3]|\ud835[\udcc5-\udd05]|\ud835[\udd07-\udd0a]|\ud835[\udd0d-\udd14]|\ud835[\udd16-\udd1c]|\ud835[\udd1e-\udd39]|\ud835[\udd3b-\udd3e]|\ud835[\udd40-\udd44]|\ud835\udd46|\ud835[\udd4a-\udd50]|\ud835[\udd52-\udea5]|\ud835[\udea8-\udeda]|\ud835[\udedc-\udf14]|\ud835[\udf16-\udf4e]|\ud835[\udf50-\udf88]|\ud835[\udf8a-\udfc2]|\ud835[\udfc4-\udfcb]|\ud836[\udc00-\uddff]|\ud836[\ude37-\ude3a]|\ud836[\ude6d-\ude74]|\ud836[\ude76-\ude83]|\ud836[\ude85-\ude8b]|\ud83c[\udd10-\udd2e]|\ud83c[\udd30-\udd69]|\ud83c[\udd70-\udd9a]|\ud83c[\udde6-\ude02]|\ud83c[\ude10-\ude3a]|\ud83c[\ude40-\ude48]|\ud83c\ude50|\ud83c\ude51|[\ud840-\ud868][\udc00-\udfff]|\ud869[\udc00-\uded6]|\ud869[\udf00-\udfff]|[\ud86a-\ud86c][\udc00-\udfff]|\ud86d[\udc00-\udf34]|\ud86d[\udf40-\udfff]|\ud86e[\udc00-\udc1d]|\ud86e[\udc20-\udfff]|[\ud86f-\ud872][\udc00-\udfff]|\ud873[\udc00-\udea1]|\ud87e[\udc00-\ude1d]|[\udb80-\udbbe][\udc00-\udfff]|\udbbf[\udc00-\udffd]|[\udbc0-\udbfe][\udc00-\udfff]|\udbff[\udc00-\udffd]' + 33 | ')|(' + 34 | '[\u0590\u05be\u05c0\u05c3\u05c6\u05c8-\u05ff\u07c0-\u07ea\u07f4\u07f5\u07fa-\u0815\u081a\u0824\u0828\u082e-\u0858\u085c-\u089f\u200f\ufb1d\ufb1f-\ufb28\ufb2a-\ufb4f\u0608\u060b\u060d\u061b-\u064a\u066d-\u066f\u0671-\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u0710\u0712-\u072f\u074b-\u07a5\u07b1-\u07bf\u08a0-\u08e2\ufb50-\ufd3d\ufd40-\ufdcf\ufdf0-\ufdfc\ufdfe\ufdff\ufe70-\ufefe]|\ud802[\udc00-\udd1e]|\ud802[\udd20-\ude00]|\ud802\ude04|\ud802[\ude07-\ude0b]|\ud802[\ude10-\ude37]|\ud802[\ude3b-\ude3e]|\ud802[\ude40-\udee4]|\ud802[\udee7-\udf38]|\ud802[\udf40-\udfff]|\ud803[\udc00-\ude5f]|\ud803[\ude7f-\udfff]|\ud83a[\udc00-\udccf]|\ud83a[\udcd7-\udfff]|\ud83b[\udc00-\uddff]|\ud83b[\udf00-\udfff]|\ud83b[\udf00-\udfff]|\ud83b[\udf00-\udfff]|\ud83b[\udf00-\udfff]|\ud83b[\udf00-\udfff]|\ud83b[\udf00-\udfff]|\ud83b[\udf00-\udfff]|\ud83b[\udf00-\udfff]|\ud83b[\udf00-\udfff]|\ud83b[\udf00-\udfff]|\ud83b[\udf00-\udfff]|\ud83b[\udf00-\udfff]|\ud83b[\udf00-\udfff]|\ud83b[\ude00-\udeef]|\ud83b[\udef2-\udeff]' + 35 | ')' + 36 | ')' 37 | ); 38 | 39 | /** 40 | * Gets directionality of the first strongly directional codepoint 41 | * 42 | * This is the rule the BIDI algorithm uses to determine the directionality of 43 | * paragraphs ( http://unicode.org/reports/tr9/#The_Paragraph_Level ) and 44 | * FSI isolates ( http://unicode.org/reports/tr9/#Explicit_Directional_Isolates ). 45 | * 46 | * TODO: Does not handle BIDI control characters inside the text. 47 | * TODO: Does not handle unallocated characters. 48 | * 49 | * @param {string} text The text from which to extract initial directionality. 50 | * @return {string} Directionality (either 'ltr' or 'rtl') 51 | */ 52 | function strongDirFromContent( text ) { 53 | var m = text.match( strongDirRegExp ); 54 | if ( !m ) { 55 | return null; 56 | } 57 | if ( m[ 2 ] === undefined ) { 58 | return 'ltr'; 59 | } 60 | return 'rtl'; 61 | } 62 | 63 | $.extend( $.i18n.parser.emitter, { 64 | /** 65 | * Wraps argument with unicode control characters for directionality safety 66 | * 67 | * This solves the problem where directionality-neutral characters at the edge of 68 | * the argument string get interpreted with the wrong directionality from the 69 | * enclosing context, giving renderings that look corrupted like "(Ben_(WMF". 70 | * 71 | * The wrapping is LRE...PDF or RLE...PDF, depending on the detected 72 | * directionality of the argument string, using the BIDI algorithm's own "First 73 | * strong directional codepoint" rule. Essentially, this works round the fact that 74 | * there is no embedding equivalent of U+2068 FSI (isolation with heuristic 75 | * direction inference). The latter is cleaner but still not widely supported. 76 | * 77 | * @param {string[]} nodes The text nodes from which to take the first item. 78 | * @return {string} Wrapped String of content as needed. 79 | */ 80 | bidi: function ( nodes ) { 81 | var dir = strongDirFromContent( nodes[ 0 ] ); 82 | if ( dir === 'ltr' ) { 83 | // Wrap in LEFT-TO-RIGHT EMBEDDING ... POP DIRECTIONAL FORMATTING 84 | return '\u202A' + nodes[ 0 ] + '\u202C'; 85 | } 86 | if ( dir === 'rtl' ) { 87 | // Wrap in RIGHT-TO-LEFT EMBEDDING ... POP DIRECTIONAL FORMATTING 88 | return '\u202B' + nodes[ 0 ] + '\u202C'; 89 | } 90 | // No strong directionality: do not wrap 91 | return nodes[ 0 ]; 92 | } 93 | } ); 94 | }( jQuery ) ); 95 | -------------------------------------------------------------------------------- /src/main/webapp/js/lib/jquery.i18n/jquery.i18n.emitter.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * jQuery Internationalization library 3 | * 4 | * Copyright (C) 2011-2013 Santhosh Thottingal, Neil Kandalgaonkar 5 | * 6 | * jquery.i18n is dual licensed GPLv2 or later and MIT. You don't have to do 7 | * anything special to choose one license or the other and you don't have to 8 | * notify anyone which license you are using. You are free to use 9 | * UniversalLanguageSelector in commercial projects as long as the copyright 10 | * header is left intact. See files GPL-LICENSE and MIT-LICENSE for details. 11 | * 12 | * @licence GNU General Public Licence 2.0 or later 13 | * @licence MIT License 14 | */ 15 | 16 | ( function ( $ ) { 17 | 'use strict'; 18 | 19 | var MessageParserEmitter = function () { 20 | this.language = $.i18n.languages[ String.locale ] || $.i18n.languages[ 'default' ]; 21 | }; 22 | 23 | MessageParserEmitter.prototype = { 24 | constructor: MessageParserEmitter, 25 | 26 | /** 27 | * (We put this method definition here, and not in prototype, to make 28 | * sure it's not overwritten by any magic.) Walk entire node structure, 29 | * applying replacements and template functions when appropriate 30 | * 31 | * @param {Mixed} node abstract syntax tree (top node or subnode) 32 | * @param {Array} replacements for $1, $2, ... $n 33 | * @return {Mixed} single-string node or array of nodes suitable for 34 | * jQuery appending. 35 | */ 36 | emit: function ( node, replacements ) { 37 | var ret, subnodes, operation, 38 | messageParserEmitter = this; 39 | 40 | switch ( typeof node ) { 41 | case 'string': 42 | case 'number': 43 | ret = node; 44 | break; 45 | case 'object': 46 | // node is an array of nodes 47 | subnodes = $.map( node.slice( 1 ), function ( n ) { 48 | return messageParserEmitter.emit( n, replacements ); 49 | } ); 50 | 51 | operation = node[ 0 ].toLowerCase(); 52 | 53 | if ( typeof messageParserEmitter[ operation ] === 'function' ) { 54 | ret = messageParserEmitter[ operation ]( subnodes, replacements ); 55 | } else { 56 | throw new Error( 'unknown operation "' + operation + '"' ); 57 | } 58 | 59 | break; 60 | case 'undefined': 61 | // Parsing the empty string (as an entire expression, or as a 62 | // paramExpression in a template) results in undefined 63 | // Perhaps a more clever parser can detect this, and return the 64 | // empty string? Or is that useful information? 65 | // The logical thing is probably to return the empty string here 66 | // when we encounter undefined. 67 | ret = ''; 68 | break; 69 | default: 70 | throw new Error( 'unexpected type in AST: ' + typeof node ); 71 | } 72 | 73 | return ret; 74 | }, 75 | 76 | /** 77 | * Parsing has been applied depth-first we can assume that all nodes 78 | * here are single nodes Must return a single node to parents -- a 79 | * jQuery with synthetic span However, unwrap any other synthetic spans 80 | * in our children and pass them upwards 81 | * 82 | * @param {Array} nodes Mixed, some single nodes, some arrays of nodes. 83 | * @return {string} 84 | */ 85 | concat: function ( nodes ) { 86 | var result = ''; 87 | 88 | $.each( nodes, function ( i, node ) { 89 | // strings, integers, anything else 90 | result += node; 91 | } ); 92 | 93 | return result; 94 | }, 95 | 96 | /** 97 | * Return escaped replacement of correct index, or string if 98 | * unavailable. Note that we expect the parsed parameter to be 99 | * zero-based. i.e. $1 should have become [ 0 ]. if the specified 100 | * parameter is not found return the same string (e.g. "$99" -> 101 | * parameter 98 -> not found -> return "$99" ) TODO throw error if 102 | * nodes.length > 1 ? 103 | * 104 | * @param {Array} nodes One element, integer, n >= 0 105 | * @param {Array} replacements for $1, $2, ... $n 106 | * @return {string} replacement 107 | */ 108 | replace: function ( nodes, replacements ) { 109 | var index = parseInt( nodes[ 0 ], 10 ); 110 | 111 | if ( index < replacements.length ) { 112 | // replacement is not a string, don't touch! 113 | return replacements[ index ]; 114 | } else { 115 | // index not found, fallback to displaying variable 116 | return '$' + ( index + 1 ); 117 | } 118 | }, 119 | 120 | /** 121 | * Transform parsed structure into pluralization n.b. The first node may 122 | * be a non-integer (for instance, a string representing an Arabic 123 | * number). So convert it back with the current language's 124 | * convertNumber. 125 | * 126 | * @param {Array} nodes List [ {String|Number}, {String}, {String} ... ] 127 | * @return {string} selected pluralized form according to current 128 | * language. 129 | */ 130 | plural: function ( nodes ) { 131 | var count = parseFloat( this.language.convertNumber( nodes[ 0 ], 10 ) ), 132 | forms = nodes.slice( 1 ); 133 | 134 | return forms.length ? this.language.convertPlural( count, forms ) : ''; 135 | }, 136 | 137 | /** 138 | * Transform parsed structure into gender Usage 139 | * {{gender:gender|masculine|feminine|neutral}}. 140 | * 141 | * @param {Array} nodes List [ {String}, {String}, {String} , {String} ] 142 | * @return {string} selected gender form according to current language 143 | */ 144 | gender: function ( nodes ) { 145 | var gender = nodes[ 0 ], 146 | forms = nodes.slice( 1 ); 147 | 148 | return this.language.gender( gender, forms ); 149 | }, 150 | 151 | /** 152 | * Transform parsed structure into grammar conversion. Invoked by 153 | * putting {{grammar:form|word}} in a message 154 | * 155 | * @param {Array} nodes List [{Grammar case eg: genitive}, {String word}] 156 | * @return {string} selected grammatical form according to current 157 | * language. 158 | */ 159 | grammar: function ( nodes ) { 160 | var form = nodes[ 0 ], 161 | word = nodes[ 1 ]; 162 | 163 | return word && form && this.language.convertGrammar( word, form ); 164 | } 165 | }; 166 | 167 | $.extend( $.i18n.parser.emitter, new MessageParserEmitter() ); 168 | }( jQuery ) ); 169 | -------------------------------------------------------------------------------- /src/main/webapp/js/lib/jquery.i18n/jquery.i18n.fallbacks.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * jQuery Internationalization library 3 | * 4 | * Copyright (C) 2012 Santhosh Thottingal 5 | * 6 | * jquery.i18n is dual licensed GPLv2 or later and MIT. You don't have to do anything special to 7 | * choose one license or the other and you don't have to notify anyone which license you are using. 8 | * You are free to use UniversalLanguageSelector in commercial projects as long as the copyright 9 | * header is left intact. See files GPL-LICENSE and MIT-LICENSE for details. 10 | * 11 | * @licence GNU General Public Licence 2.0 or later 12 | * @licence MIT License 13 | */ 14 | ( function ( $ ) { 15 | 'use strict'; 16 | 17 | $.i18n = $.i18n || {}; 18 | $.extend( $.i18n.fallbacks, { 19 | ab: [ 'ru' ], 20 | ace: [ 'id' ], 21 | aln: [ 'sq' ], 22 | // Not so standard - als is supposed to be Tosk Albanian, 23 | // but in Wikipedia it's used for a Germanic language. 24 | als: [ 'gsw', 'de' ], 25 | an: [ 'es' ], 26 | anp: [ 'hi' ], 27 | arn: [ 'es' ], 28 | arz: [ 'ar' ], 29 | av: [ 'ru' ], 30 | ay: [ 'es' ], 31 | ba: [ 'ru' ], 32 | bar: [ 'de' ], 33 | 'bat-smg': [ 'sgs', 'lt' ], 34 | bcc: [ 'fa' ], 35 | 'be-x-old': [ 'be-tarask' ], 36 | bh: [ 'bho' ], 37 | bjn: [ 'id' ], 38 | bm: [ 'fr' ], 39 | bpy: [ 'bn' ], 40 | bqi: [ 'fa' ], 41 | bug: [ 'id' ], 42 | 'cbk-zam': [ 'es' ], 43 | ce: [ 'ru' ], 44 | crh: [ 'crh-latn' ], 45 | 'crh-cyrl': [ 'ru' ], 46 | csb: [ 'pl' ], 47 | cv: [ 'ru' ], 48 | 'de-at': [ 'de' ], 49 | 'de-ch': [ 'de' ], 50 | 'de-formal': [ 'de' ], 51 | dsb: [ 'de' ], 52 | dtp: [ 'ms' ], 53 | egl: [ 'it' ], 54 | eml: [ 'it' ], 55 | ff: [ 'fr' ], 56 | fit: [ 'fi' ], 57 | 'fiu-vro': [ 'vro', 'et' ], 58 | frc: [ 'fr' ], 59 | frp: [ 'fr' ], 60 | frr: [ 'de' ], 61 | fur: [ 'it' ], 62 | gag: [ 'tr' ], 63 | gan: [ 'gan-hant', 'zh-hant', 'zh-hans' ], 64 | 'gan-hans': [ 'zh-hans' ], 65 | 'gan-hant': [ 'zh-hant', 'zh-hans' ], 66 | gl: [ 'pt' ], 67 | glk: [ 'fa' ], 68 | gn: [ 'es' ], 69 | gsw: [ 'de' ], 70 | hif: [ 'hif-latn' ], 71 | hsb: [ 'de' ], 72 | ht: [ 'fr' ], 73 | ii: [ 'zh-cn', 'zh-hans' ], 74 | inh: [ 'ru' ], 75 | iu: [ 'ike-cans' ], 76 | jut: [ 'da' ], 77 | jv: [ 'id' ], 78 | kaa: [ 'kk-latn', 'kk-cyrl' ], 79 | kbd: [ 'kbd-cyrl' ], 80 | khw: [ 'ur' ], 81 | kiu: [ 'tr' ], 82 | kk: [ 'kk-cyrl' ], 83 | 'kk-arab': [ 'kk-cyrl' ], 84 | 'kk-latn': [ 'kk-cyrl' ], 85 | 'kk-cn': [ 'kk-arab', 'kk-cyrl' ], 86 | 'kk-kz': [ 'kk-cyrl' ], 87 | 'kk-tr': [ 'kk-latn', 'kk-cyrl' ], 88 | kl: [ 'da' ], 89 | 'ko-kp': [ 'ko' ], 90 | koi: [ 'ru' ], 91 | krc: [ 'ru' ], 92 | ks: [ 'ks-arab' ], 93 | ksh: [ 'de' ], 94 | ku: [ 'ku-latn' ], 95 | 'ku-arab': [ 'ckb' ], 96 | kv: [ 'ru' ], 97 | lad: [ 'es' ], 98 | lb: [ 'de' ], 99 | lbe: [ 'ru' ], 100 | lez: [ 'ru' ], 101 | li: [ 'nl' ], 102 | lij: [ 'it' ], 103 | liv: [ 'et' ], 104 | lmo: [ 'it' ], 105 | ln: [ 'fr' ], 106 | ltg: [ 'lv' ], 107 | lzz: [ 'tr' ], 108 | mai: [ 'hi' ], 109 | 'map-bms': [ 'jv', 'id' ], 110 | mg: [ 'fr' ], 111 | mhr: [ 'ru' ], 112 | min: [ 'id' ], 113 | mo: [ 'ro' ], 114 | mrj: [ 'ru' ], 115 | mwl: [ 'pt' ], 116 | myv: [ 'ru' ], 117 | mzn: [ 'fa' ], 118 | nah: [ 'es' ], 119 | nap: [ 'it' ], 120 | nds: [ 'de' ], 121 | 'nds-nl': [ 'nl' ], 122 | 'nl-informal': [ 'nl' ], 123 | no: [ 'nb' ], 124 | os: [ 'ru' ], 125 | pcd: [ 'fr' ], 126 | pdc: [ 'de' ], 127 | pdt: [ 'de' ], 128 | pfl: [ 'de' ], 129 | pms: [ 'it' ], 130 | pt: [ 'pt-br' ], 131 | 'pt-br': [ 'pt' ], 132 | qu: [ 'es' ], 133 | qug: [ 'qu', 'es' ], 134 | rgn: [ 'it' ], 135 | rmy: [ 'ro' ], 136 | 'roa-rup': [ 'rup' ], 137 | rue: [ 'uk', 'ru' ], 138 | ruq: [ 'ruq-latn', 'ro' ], 139 | 'ruq-cyrl': [ 'mk' ], 140 | 'ruq-latn': [ 'ro' ], 141 | sa: [ 'hi' ], 142 | sah: [ 'ru' ], 143 | scn: [ 'it' ], 144 | sg: [ 'fr' ], 145 | sgs: [ 'lt' ], 146 | sli: [ 'de' ], 147 | sr: [ 'sr-ec' ], 148 | srn: [ 'nl' ], 149 | stq: [ 'de' ], 150 | su: [ 'id' ], 151 | szl: [ 'pl' ], 152 | tcy: [ 'kn' ], 153 | tg: [ 'tg-cyrl' ], 154 | tt: [ 'tt-cyrl', 'ru' ], 155 | 'tt-cyrl': [ 'ru' ], 156 | ty: [ 'fr' ], 157 | udm: [ 'ru' ], 158 | ug: [ 'ug-arab' ], 159 | uk: [ 'ru' ], 160 | vec: [ 'it' ], 161 | vep: [ 'et' ], 162 | vls: [ 'nl' ], 163 | vmf: [ 'de' ], 164 | vot: [ 'fi' ], 165 | vro: [ 'et' ], 166 | wa: [ 'fr' ], 167 | wo: [ 'fr' ], 168 | wuu: [ 'zh-hans' ], 169 | xal: [ 'ru' ], 170 | xmf: [ 'ka' ], 171 | yi: [ 'he' ], 172 | za: [ 'zh-hans' ], 173 | zea: [ 'nl' ], 174 | zh: [ 'zh-hans' ], 175 | 'zh-classical': [ 'lzh' ], 176 | 'zh-cn': [ 'zh-hans' ], 177 | 'zh-hant': [ 'zh-hans' ], 178 | 'zh-hk': [ 'zh-hant', 'zh-hans' ], 179 | 'zh-min-nan': [ 'nan' ], 180 | 'zh-mo': [ 'zh-hk', 'zh-hant', 'zh-hans' ], 181 | 'zh-my': [ 'zh-sg', 'zh-hans' ], 182 | 'zh-sg': [ 'zh-hans' ], 183 | 'zh-tw': [ 'zh-hant', 'zh-hans' ], 184 | 'zh-yue': [ 'yue' ] 185 | } ); 186 | }( jQuery ) ); 187 | -------------------------------------------------------------------------------- /src/main/webapp/js/lib/jquery.i18n/jquery.i18n.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * jQuery Internationalization library 3 | * 4 | * Copyright (C) 2012 Santhosh Thottingal 5 | * 6 | * jquery.i18n is dual licensed GPLv2 or later and MIT. You don't have to do 7 | * anything special to choose one license or the other and you don't have to 8 | * notify anyone which license you are using. You are free to use 9 | * UniversalLanguageSelector in commercial projects as long as the copyright 10 | * header is left intact. See files GPL-LICENSE and MIT-LICENSE for details. 11 | * 12 | * @licence GNU General Public Licence 2.0 or later 13 | * @licence MIT License 14 | */ 15 | 16 | ( function ( $ ) { 17 | 'use strict'; 18 | 19 | var nav, I18N, 20 | slice = Array.prototype.slice; 21 | /** 22 | * @constructor 23 | * @param {Object} options 24 | */ 25 | I18N = function ( options ) { 26 | // Load defaults 27 | this.options = $.extend( {}, I18N.defaults, options ); 28 | 29 | this.parser = this.options.parser; 30 | this.locale = this.options.locale; 31 | this.messageStore = this.options.messageStore; 32 | this.languages = {}; 33 | 34 | this.init(); 35 | }; 36 | 37 | I18N.prototype = { 38 | /** 39 | * Initialize by loading locales and setting up 40 | * String.prototype.toLocaleString and String.locale. 41 | */ 42 | init: function () { 43 | var i18n = this; 44 | 45 | // Set locale of String environment 46 | String.locale = i18n.locale; 47 | 48 | // Override String.localeString method 49 | String.prototype.toLocaleString = function () { 50 | var localeParts, localePartIndex, value, locale, fallbackIndex, 51 | tryingLocale, message; 52 | 53 | value = this.valueOf(); 54 | locale = i18n.locale; 55 | fallbackIndex = 0; 56 | 57 | while ( locale ) { 58 | // Iterate through locales starting at most-specific until 59 | // localization is found. As in fi-Latn-FI, fi-Latn and fi. 60 | localeParts = locale.split( '-' ); 61 | localePartIndex = localeParts.length; 62 | 63 | do { 64 | tryingLocale = localeParts.slice( 0, localePartIndex ).join( '-' ); 65 | message = i18n.messageStore.get( tryingLocale, value ); 66 | 67 | if ( message ) { 68 | return message; 69 | } 70 | 71 | localePartIndex--; 72 | } while ( localePartIndex ); 73 | 74 | if ( locale === 'en' ) { 75 | break; 76 | } 77 | 78 | locale = ( $.i18n.fallbacks[ i18n.locale ] && $.i18n.fallbacks[ i18n.locale ][ fallbackIndex ] ) || 79 | i18n.options.fallbackLocale; 80 | $.i18n.log( 'Trying fallback locale for ' + i18n.locale + ': ' + locale + ' (' + value + ')' ); 81 | 82 | fallbackIndex++; 83 | } 84 | 85 | // key not found 86 | return ''; 87 | }; 88 | }, 89 | 90 | /* 91 | * Destroy the i18n instance. 92 | */ 93 | destroy: function () { 94 | $.removeData( document, 'i18n' ); 95 | }, 96 | 97 | /** 98 | * General message loading API This can take a URL string for 99 | * the json formatted messages. Example: 100 | * load('path/to/all_localizations.json'); 101 | * 102 | * To load a localization file for a locale: 103 | * 104 | * load('path/to/de-messages.json', 'de' ); 105 | * 106 | * 107 | * To load a localization file from a directory: 108 | * 109 | * load('path/to/i18n/directory', 'de' ); 110 | * 111 | * The above method has the advantage of fallback resolution. 112 | * ie, it will automatically load the fallback locales for de. 113 | * For most usecases, this is the recommended method. 114 | * It is optional to have trailing slash at end. 115 | * 116 | * A data object containing message key- message translation mappings 117 | * can also be passed. Example: 118 | * 119 | * load( { 'hello' : 'Hello' }, optionalLocale ); 120 | * 121 | * 122 | * A source map containing key-value pair of languagename and locations 123 | * can also be passed. Example: 124 | * 125 | * load( { 126 | * bn: 'i18n/bn.json', 127 | * he: 'i18n/he.json', 128 | * en: 'i18n/en.json' 129 | * } ) 130 | * 131 | * 132 | * If the data argument is null/undefined/false, 133 | * all cached messages for the i18n instance will get reset. 134 | * 135 | * @param {string|Object} source 136 | * @param {string} locale Language tag 137 | * @return {jQuery.Promise} 138 | */ 139 | load: function ( source, locale ) { 140 | var fallbackLocales, locIndex, fallbackLocale, sourceMap = {}; 141 | if ( !source && !locale ) { 142 | source = 'i18n/' + $.i18n().locale + '.json'; 143 | locale = $.i18n().locale; 144 | } 145 | if ( typeof source === 'string' && 146 | // source extension should be json, but can have query params after that. 147 | source.split( '?' )[ 0 ].split( '.' ).pop() !== 'json' 148 | ) { 149 | // Load specified locale then check for fallbacks when directory is specified in load() 150 | sourceMap[ locale ] = source + '/' + locale + '.json'; 151 | fallbackLocales = ( $.i18n.fallbacks[ locale ] || [] ) 152 | .concat( this.options.fallbackLocale ); 153 | for ( locIndex = 0; locIndex < fallbackLocales.length; locIndex++ ) { 154 | fallbackLocale = fallbackLocales[ locIndex ]; 155 | sourceMap[ fallbackLocale ] = source + '/' + fallbackLocale + '.json'; 156 | } 157 | return this.load( sourceMap ); 158 | } else { 159 | return this.messageStore.load( source, locale ); 160 | } 161 | 162 | }, 163 | 164 | /** 165 | * Does parameter and magic word substitution. 166 | * 167 | * @param {string} key Message key 168 | * @param {Array} parameters Message parameters 169 | * @return {string} 170 | */ 171 | parse: function ( key, parameters ) { 172 | var message = key.toLocaleString(); 173 | // FIXME: This changes the state of the I18N object, 174 | // should probably not change the 'this.parser' but just 175 | // pass it to the parser. 176 | this.parser.language = $.i18n.languages[ $.i18n().locale ] || $.i18n.languages[ 'default' ]; 177 | if ( message === '' ) { 178 | message = key; 179 | } 180 | return this.parser.parse( message, parameters ); 181 | } 182 | }; 183 | 184 | /** 185 | * Process a message from the $.I18N instance 186 | * for the current document, stored in jQuery.data(document). 187 | * 188 | * @param {string} key Key of the message. 189 | * @param {string} param1 [param...] Variadic list of parameters for {key}. 190 | * @return {string|$.I18N} Parsed message, or if no key was given 191 | * the instance of $.I18N is returned. 192 | */ 193 | $.i18n = function ( key, param1 ) { 194 | var parameters, 195 | i18n = $.data( document, 'i18n' ), 196 | options = typeof key === 'object' && key; 197 | 198 | // If the locale option for this call is different then the setup so far, 199 | // update it automatically. This doesn't just change the context for this 200 | // call but for all future call as well. 201 | // If there is no i18n setup yet, don't do this. It will be taken care of 202 | // by the `new I18N` construction below. 203 | // NOTE: It should only change language for this one call. 204 | // Then cache instances of I18N somewhere. 205 | if ( options && options.locale && i18n && i18n.locale !== options.locale ) { 206 | String.locale = i18n.locale = options.locale; 207 | } 208 | 209 | if ( !i18n ) { 210 | i18n = new I18N( options ); 211 | $.data( document, 'i18n', i18n ); 212 | } 213 | 214 | if ( typeof key === 'string' ) { 215 | if ( param1 !== undefined ) { 216 | parameters = slice.call( arguments, 1 ); 217 | } else { 218 | parameters = []; 219 | } 220 | 221 | return i18n.parse( key, parameters ); 222 | } else { 223 | // FIXME: remove this feature/bug. 224 | return i18n; 225 | } 226 | }; 227 | 228 | $.fn.i18n = function () { 229 | var i18n = $.data( document, 'i18n' ); 230 | 231 | if ( !i18n ) { 232 | i18n = new I18N(); 233 | $.data( document, 'i18n', i18n ); 234 | } 235 | String.locale = i18n.locale; 236 | return this.each( function () { 237 | var $this = $( this ), 238 | messageKey = $this.data( 'i18n' ), 239 | lBracket, rBracket, type, key; 240 | 241 | if ( messageKey ) { 242 | lBracket = messageKey.indexOf( '[' ); 243 | rBracket = messageKey.indexOf( ']' ); 244 | if ( lBracket !== -1 && rBracket !== -1 && lBracket < rBracket ) { 245 | type = messageKey.slice( lBracket + 1, rBracket ); 246 | key = messageKey.slice( rBracket + 1 ); 247 | if ( type === 'html' ) { 248 | $this.html( i18n.parse( key ) ); 249 | } else { 250 | $this.attr( type, i18n.parse( key ) ); 251 | } 252 | } else { 253 | $this.text( i18n.parse( messageKey ) ); 254 | } 255 | } else { 256 | $this.find( '[data-i18n]' ).i18n(); 257 | } 258 | } ); 259 | }; 260 | 261 | String.locale = String.locale || $( 'html' ).attr( 'lang' ); 262 | 263 | if ( !String.locale ) { 264 | if ( typeof window.navigator !== undefined ) { 265 | nav = window.navigator; 266 | String.locale = nav.language || nav.userLanguage || ''; 267 | } else { 268 | String.locale = ''; 269 | } 270 | } 271 | 272 | $.i18n.languages = {}; 273 | $.i18n.messageStore = $.i18n.messageStore || {}; 274 | $.i18n.parser = { 275 | // The default parser only handles variable substitution 276 | parse: function ( message, parameters ) { 277 | return message.replace( /\$(\d+)/g, function ( str, match ) { 278 | var index = parseInt( match, 10 ) - 1; 279 | return parameters[ index ] !== undefined ? parameters[ index ] : '$' + match; 280 | } ); 281 | }, 282 | emitter: {} 283 | }; 284 | $.i18n.fallbacks = {}; 285 | $.i18n.debug = false; 286 | $.i18n.log = function ( /* arguments */ ) { 287 | if ( window.console && $.i18n.debug ) { 288 | window.console.log.apply( window.console, arguments ); 289 | } 290 | }; 291 | /* Static members */ 292 | I18N.defaults = { 293 | locale: String.locale, 294 | fallbackLocale: 'en', 295 | parser: $.i18n.parser, 296 | messageStore: $.i18n.messageStore 297 | }; 298 | 299 | // Expose constructor 300 | $.i18n.constructor = I18N; 301 | }( jQuery ) ); 302 | -------------------------------------------------------------------------------- /src/main/webapp/js/lib/jquery.i18n/jquery.i18n.language.js: -------------------------------------------------------------------------------- 1 | /* global pluralRuleParser */ 2 | ( function ( $ ) { 3 | 'use strict'; 4 | 5 | // jscs:disable 6 | var language = { 7 | // CLDR plural rules generated using 8 | // libs/CLDRPluralRuleParser/tools/PluralXML2JSON.html 9 | pluralRules: { 10 | ak: { 11 | one: 'n = 0..1' 12 | }, 13 | am: { 14 | one: 'i = 0 or n = 1' 15 | }, 16 | ar: { 17 | zero: 'n = 0', 18 | one: 'n = 1', 19 | two: 'n = 2', 20 | few: 'n % 100 = 3..10', 21 | many: 'n % 100 = 11..99' 22 | }, 23 | ars: { 24 | zero: 'n = 0', 25 | one: 'n = 1', 26 | two: 'n = 2', 27 | few: 'n % 100 = 3..10', 28 | many: 'n % 100 = 11..99' 29 | }, 30 | as: { 31 | one: 'i = 0 or n = 1' 32 | }, 33 | be: { 34 | one: 'n % 10 = 1 and n % 100 != 11', 35 | few: 'n % 10 = 2..4 and n % 100 != 12..14', 36 | many: 'n % 10 = 0 or n % 10 = 5..9 or n % 100 = 11..14' 37 | }, 38 | bh: { 39 | one: 'n = 0..1' 40 | }, 41 | bn: { 42 | one: 'i = 0 or n = 1' 43 | }, 44 | br: { 45 | one: 'n % 10 = 1 and n % 100 != 11,71,91', 46 | two: 'n % 10 = 2 and n % 100 != 12,72,92', 47 | few: 'n % 10 = 3..4,9 and n % 100 != 10..19,70..79,90..99', 48 | many: 'n != 0 and n % 1000000 = 0' 49 | }, 50 | bs: { 51 | one: 'v = 0 and i % 10 = 1 and i % 100 != 11 or f % 10 = 1 and f % 100 != 11', 52 | few: 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14' 53 | }, 54 | cs: { 55 | one: 'i = 1 and v = 0', 56 | few: 'i = 2..4 and v = 0', 57 | many: 'v != 0' 58 | }, 59 | cy: { 60 | zero: 'n = 0', 61 | one: 'n = 1', 62 | two: 'n = 2', 63 | few: 'n = 3', 64 | many: 'n = 6' 65 | }, 66 | da: { 67 | one: 'n = 1 or t != 0 and i = 0,1' 68 | }, 69 | dsb: { 70 | one: 'v = 0 and i % 100 = 1 or f % 100 = 1', 71 | two: 'v = 0 and i % 100 = 2 or f % 100 = 2', 72 | few: 'v = 0 and i % 100 = 3..4 or f % 100 = 3..4' 73 | }, 74 | fa: { 75 | one: 'i = 0 or n = 1' 76 | }, 77 | ff: { 78 | one: 'i = 0,1' 79 | }, 80 | fil: { 81 | one: 'v = 0 and i = 1,2,3 or v = 0 and i % 10 != 4,6,9 or v != 0 and f % 10 != 4,6,9' 82 | }, 83 | fr: { 84 | one: 'i = 0,1' 85 | }, 86 | ga: { 87 | one: 'n = 1', 88 | two: 'n = 2', 89 | few: 'n = 3..6', 90 | many: 'n = 7..10' 91 | }, 92 | gd: { 93 | one: 'n = 1,11', 94 | two: 'n = 2,12', 95 | few: 'n = 3..10,13..19' 96 | }, 97 | gu: { 98 | one: 'i = 0 or n = 1' 99 | }, 100 | guw: { 101 | one: 'n = 0..1' 102 | }, 103 | gv: { 104 | one: 'v = 0 and i % 10 = 1', 105 | two: 'v = 0 and i % 10 = 2', 106 | few: 'v = 0 and i % 100 = 0,20,40,60,80', 107 | many: 'v != 0' 108 | }, 109 | he: { 110 | one: 'i = 1 and v = 0', 111 | two: 'i = 2 and v = 0', 112 | many: 'v = 0 and n != 0..10 and n % 10 = 0' 113 | }, 114 | hi: { 115 | one: 'i = 0 or n = 1' 116 | }, 117 | hr: { 118 | one: 'v = 0 and i % 10 = 1 and i % 100 != 11 or f % 10 = 1 and f % 100 != 11', 119 | few: 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14' 120 | }, 121 | hsb: { 122 | one: 'v = 0 and i % 100 = 1 or f % 100 = 1', 123 | two: 'v = 0 and i % 100 = 2 or f % 100 = 2', 124 | few: 'v = 0 and i % 100 = 3..4 or f % 100 = 3..4' 125 | }, 126 | hy: { 127 | one: 'i = 0,1' 128 | }, 129 | is: { 130 | one: 't = 0 and i % 10 = 1 and i % 100 != 11 or t != 0' 131 | }, 132 | iu: { 133 | one: 'n = 1', 134 | two: 'n = 2' 135 | }, 136 | iw: { 137 | one: 'i = 1 and v = 0', 138 | two: 'i = 2 and v = 0', 139 | many: 'v = 0 and n != 0..10 and n % 10 = 0' 140 | }, 141 | kab: { 142 | one: 'i = 0,1' 143 | }, 144 | kn: { 145 | one: 'i = 0 or n = 1' 146 | }, 147 | kw: { 148 | one: 'n = 1', 149 | two: 'n = 2' 150 | }, 151 | lag: { 152 | zero: 'n = 0', 153 | one: 'i = 0,1 and n != 0' 154 | }, 155 | ln: { 156 | one: 'n = 0..1' 157 | }, 158 | lt: { 159 | one: 'n % 10 = 1 and n % 100 != 11..19', 160 | few: 'n % 10 = 2..9 and n % 100 != 11..19', 161 | many: 'f != 0' 162 | }, 163 | lv: { 164 | zero: 'n % 10 = 0 or n % 100 = 11..19 or v = 2 and f % 100 = 11..19', 165 | one: 'n % 10 = 1 and n % 100 != 11 or v = 2 and f % 10 = 1 and f % 100 != 11 or v != 2 and f % 10 = 1' 166 | }, 167 | mg: { 168 | one: 'n = 0..1' 169 | }, 170 | mk: { 171 | one: 'v = 0 and i % 10 = 1 or f % 10 = 1' 172 | }, 173 | mo: { 174 | one: 'i = 1 and v = 0', 175 | few: 'v != 0 or n = 0 or n != 1 and n % 100 = 1..19' 176 | }, 177 | mr: { 178 | one: 'i = 0 or n = 1' 179 | }, 180 | mt: { 181 | one: 'n = 1', 182 | few: 'n = 0 or n % 100 = 2..10', 183 | many: 'n % 100 = 11..19' 184 | }, 185 | naq: { 186 | one: 'n = 1', 187 | two: 'n = 2' 188 | }, 189 | nso: { 190 | one: 'n = 0..1' 191 | }, 192 | pa: { 193 | one: 'n = 0..1' 194 | }, 195 | pl: { 196 | one: 'i = 1 and v = 0', 197 | few: 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14', 198 | many: 'v = 0 and i != 1 and i % 10 = 0..1 or v = 0 and i % 10 = 5..9 or v = 0 and i % 100 = 12..14' 199 | }, 200 | prg: { 201 | zero: 'n % 10 = 0 or n % 100 = 11..19 or v = 2 and f % 100 = 11..19', 202 | one: 'n % 10 = 1 and n % 100 != 11 or v = 2 and f % 10 = 1 and f % 100 != 11 or v != 2 and f % 10 = 1' 203 | }, 204 | pt: { 205 | one: 'i = 0..1' 206 | }, 207 | ro: { 208 | one: 'i = 1 and v = 0', 209 | few: 'v != 0 or n = 0 or n != 1 and n % 100 = 1..19' 210 | }, 211 | ru: { 212 | one: 'v = 0 and i % 10 = 1 and i % 100 != 11', 213 | few: 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14', 214 | many: 'v = 0 and i % 10 = 0 or v = 0 and i % 10 = 5..9 or v = 0 and i % 100 = 11..14' 215 | }, 216 | se: { 217 | one: 'n = 1', 218 | two: 'n = 2' 219 | }, 220 | sh: { 221 | one: 'v = 0 and i % 10 = 1 and i % 100 != 11 or f % 10 = 1 and f % 100 != 11', 222 | few: 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14' 223 | }, 224 | shi: { 225 | one: 'i = 0 or n = 1', 226 | few: 'n = 2..10' 227 | }, 228 | si: { 229 | one: 'n = 0,1 or i = 0 and f = 1' 230 | }, 231 | sk: { 232 | one: 'i = 1 and v = 0', 233 | few: 'i = 2..4 and v = 0', 234 | many: 'v != 0' 235 | }, 236 | sl: { 237 | one: 'v = 0 and i % 100 = 1', 238 | two: 'v = 0 and i % 100 = 2', 239 | few: 'v = 0 and i % 100 = 3..4 or v != 0' 240 | }, 241 | sma: { 242 | one: 'n = 1', 243 | two: 'n = 2' 244 | }, 245 | smi: { 246 | one: 'n = 1', 247 | two: 'n = 2' 248 | }, 249 | smj: { 250 | one: 'n = 1', 251 | two: 'n = 2' 252 | }, 253 | smn: { 254 | one: 'n = 1', 255 | two: 'n = 2' 256 | }, 257 | sms: { 258 | one: 'n = 1', 259 | two: 'n = 2' 260 | }, 261 | sr: { 262 | one: 'v = 0 and i % 10 = 1 and i % 100 != 11 or f % 10 = 1 and f % 100 != 11', 263 | few: 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14' 264 | }, 265 | ti: { 266 | one: 'n = 0..1' 267 | }, 268 | tl: { 269 | one: 'v = 0 and i = 1,2,3 or v = 0 and i % 10 != 4,6,9 or v != 0 and f % 10 != 4,6,9' 270 | }, 271 | tzm: { 272 | one: 'n = 0..1 or n = 11..99' 273 | }, 274 | uk: { 275 | one: 'v = 0 and i % 10 = 1 and i % 100 != 11', 276 | few: 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14', 277 | many: 'v = 0 and i % 10 = 0 or v = 0 and i % 10 = 5..9 or v = 0 and i % 100 = 11..14' 278 | }, 279 | wa: { 280 | one: 'n = 0..1' 281 | }, 282 | zu: { 283 | one: 'i = 0 or n = 1' 284 | } 285 | }, 286 | // jscs:enable 287 | 288 | /** 289 | * Plural form transformations, needed for some languages. 290 | * 291 | * @param {integer} count 292 | * Non-localized quantifier 293 | * @param {Array} forms 294 | * List of plural forms 295 | * @return {string} Correct form for quantifier in this language 296 | */ 297 | convertPlural: function ( count, forms ) { 298 | var pluralRules, 299 | pluralFormIndex, 300 | index, 301 | explicitPluralPattern = new RegExp( '\\d+=', 'i' ), 302 | formCount, 303 | form; 304 | 305 | if ( !forms || forms.length === 0 ) { 306 | return ''; 307 | } 308 | 309 | // Handle for Explicit 0= & 1= values 310 | for ( index = 0; index < forms.length; index++ ) { 311 | form = forms[ index ]; 312 | if ( explicitPluralPattern.test( form ) ) { 313 | formCount = parseInt( form.slice( 0, form.indexOf( '=' ) ), 10 ); 314 | if ( formCount === count ) { 315 | return ( form.slice( form.indexOf( '=' ) + 1 ) ); 316 | } 317 | forms[ index ] = undefined; 318 | } 319 | } 320 | 321 | forms = $.map( forms, function ( form ) { 322 | if ( form !== undefined ) { 323 | return form; 324 | } 325 | } ); 326 | 327 | pluralRules = this.pluralRules[ $.i18n().locale ]; 328 | 329 | if ( !pluralRules ) { 330 | // default fallback. 331 | return ( count === 1 ) ? forms[ 0 ] : forms[ 1 ]; 332 | } 333 | 334 | pluralFormIndex = this.getPluralForm( count, pluralRules ); 335 | pluralFormIndex = Math.min( pluralFormIndex, forms.length - 1 ); 336 | 337 | return forms[ pluralFormIndex ]; 338 | }, 339 | 340 | /** 341 | * For the number, get the plural for index 342 | * 343 | * @param {integer} number 344 | * @param {Object} pluralRules 345 | * @return {integer} plural form index 346 | */ 347 | getPluralForm: function ( number, pluralRules ) { 348 | var i, 349 | pluralForms = [ 'zero', 'one', 'two', 'few', 'many', 'other' ], 350 | pluralFormIndex = 0; 351 | 352 | for ( i = 0; i < pluralForms.length; i++ ) { 353 | if ( pluralRules[ pluralForms[ i ] ] ) { 354 | if ( pluralRuleParser( pluralRules[ pluralForms[ i ] ], number ) ) { 355 | return pluralFormIndex; 356 | } 357 | 358 | pluralFormIndex++; 359 | } 360 | } 361 | 362 | return pluralFormIndex; 363 | }, 364 | 365 | /** 366 | * Converts a number using digitTransformTable. 367 | * 368 | * @param {number} num Value to be converted 369 | * @param {boolean} integer Convert the return value to an integer 370 | * @return {string} The number converted into a String. 371 | */ 372 | convertNumber: function ( num, integer ) { 373 | var tmp, item, i, 374 | transformTable, numberString, convertedNumber; 375 | 376 | // Set the target Transform table: 377 | transformTable = this.digitTransformTable( $.i18n().locale ); 378 | numberString = String( num ); 379 | convertedNumber = ''; 380 | 381 | if ( !transformTable ) { 382 | return num; 383 | } 384 | 385 | // Check if the restore to Latin number flag is set: 386 | if ( integer ) { 387 | if ( parseFloat( num, 10 ) === num ) { 388 | return num; 389 | } 390 | 391 | tmp = []; 392 | 393 | for ( item in transformTable ) { 394 | tmp[ transformTable[ item ] ] = item; 395 | } 396 | 397 | transformTable = tmp; 398 | } 399 | 400 | for ( i = 0; i < numberString.length; i++ ) { 401 | if ( transformTable[ numberString[ i ] ] ) { 402 | convertedNumber += transformTable[ numberString[ i ] ]; 403 | } else { 404 | convertedNumber += numberString[ i ]; 405 | } 406 | } 407 | 408 | return integer ? parseFloat( convertedNumber, 10 ) : convertedNumber; 409 | }, 410 | 411 | /** 412 | * Grammatical transformations, needed for inflected languages. 413 | * Invoked by putting {{grammar:form|word}} in a message. 414 | * Override this method for languages that need special grammar rules 415 | * applied dynamically. 416 | * 417 | * @param {string} word 418 | * @param {string} form 419 | * @return {string} 420 | */ 421 | // eslint-disable-next-line no-unused-vars 422 | convertGrammar: function ( word, form ) { 423 | return word; 424 | }, 425 | 426 | /** 427 | * Provides an alternative text depending on specified gender. Usage 428 | * {{gender:[gender|user object]|masculine|feminine|neutral}}. If second 429 | * or third parameter are not specified, masculine is used. 430 | * 431 | * These details may be overriden per language. 432 | * 433 | * @param {string} gender 434 | * male, female, or anything else for neutral. 435 | * @param {Array} forms 436 | * List of gender forms 437 | * 438 | * @return {string} 439 | */ 440 | gender: function ( gender, forms ) { 441 | if ( !forms || forms.length === 0 ) { 442 | return ''; 443 | } 444 | 445 | while ( forms.length < 2 ) { 446 | forms.push( forms[ forms.length - 1 ] ); 447 | } 448 | 449 | if ( gender === 'male' ) { 450 | return forms[ 0 ]; 451 | } 452 | 453 | if ( gender === 'female' ) { 454 | return forms[ 1 ]; 455 | } 456 | 457 | return ( forms.length === 3 ) ? forms[ 2 ] : forms[ 0 ]; 458 | }, 459 | 460 | /** 461 | * Get the digit transform table for the given language 462 | * See http://cldr.unicode.org/translation/numbering-systems 463 | * 464 | * @param {string} language 465 | * @return {Array|boolean} List of digits in the passed language or false 466 | * representation, or boolean false if there is no information. 467 | */ 468 | digitTransformTable: function ( language ) { 469 | var tables = { 470 | ar: '٠١٢٣٤٥٦٧٨٩', 471 | fa: '۰۱۲۳۴۵۶۷۸۹', 472 | ml: '൦൧൨൩൪൫൬൭൮൯', 473 | kn: '೦೧೨೩೪೫೬೭೮೯', 474 | lo: '໐໑໒໓໔໕໖໗໘໙', 475 | or: '୦୧୨୩୪୫୬୭୮୯', 476 | kh: '០១២៣៤៥៦៧៨៩', 477 | pa: '੦੧੨੩੪੫੬੭੮੯', 478 | gu: '૦૧૨૩૪૫૬૭૮૯', 479 | hi: '०१२३४५६७८९', 480 | my: '၀၁၂၃၄၅၆၇၈၉', 481 | ta: '௦௧௨௩௪௫௬௭௮௯', 482 | te: '౦౧౨౩౪౫౬౭౮౯', 483 | th: '๐๑๒๓๔๕๖๗๘๙', // FIXME use iso 639 codes 484 | bo: '༠༡༢༣༤༥༦༧༨༩' // FIXME use iso 639 codes 485 | }; 486 | 487 | if ( !tables[ language ] ) { 488 | return false; 489 | } 490 | 491 | return tables[ language ].split( '' ); 492 | } 493 | }; 494 | 495 | $.extend( $.i18n.languages, { 496 | 'default': language 497 | } ); 498 | }( jQuery ) ); 499 | -------------------------------------------------------------------------------- /src/main/webapp/js/lib/jquery.i18n/jquery.i18n.messagestore.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * jQuery Internationalization library - Message Store 3 | * 4 | * Copyright (C) 2012 Santhosh Thottingal 5 | * 6 | * jquery.i18n is dual licensed GPLv2 or later and MIT. You don't have to do anything special to 7 | * choose one license or the other and you don't have to notify anyone which license you are using. 8 | * You are free to use UniversalLanguageSelector in commercial projects as long as the copyright 9 | * header is left intact. See files GPL-LICENSE and MIT-LICENSE for details. 10 | * 11 | * @licence GNU General Public Licence 2.0 or later 12 | * @licence MIT License 13 | */ 14 | 15 | ( function ( $ ) { 16 | 'use strict'; 17 | 18 | var MessageStore = function () { 19 | this.messages = {}; 20 | this.sources = {}; 21 | }; 22 | 23 | function jsonMessageLoader( url ) { 24 | var deferred = $.Deferred(); 25 | 26 | $.getJSON( url ) 27 | .done( deferred.resolve ) 28 | .fail( function ( jqxhr, settings, exception ) { 29 | $.i18n.log( 'Error in loading messages from ' + url + ' Exception: ' + exception ); 30 | // Ignore 404 exception, because we are handling fallabacks explicitly 31 | deferred.resolve(); 32 | } ); 33 | 34 | return deferred.promise(); 35 | } 36 | 37 | /** 38 | * See https://github.com/wikimedia/jquery.i18n/wiki/Specification#wiki-Message_File_Loading 39 | */ 40 | MessageStore.prototype = { 41 | 42 | /** 43 | * General message loading API This can take a URL string for 44 | * the json formatted messages. 45 | * load('path/to/all_localizations.json'); 46 | * 47 | * This can also load a localization file for a locale 48 | * load( 'path/to/de-messages.json', 'de' ); 49 | * 50 | * A data object containing message key- message translation mappings 51 | * can also be passed Eg: 52 | * 53 | * load( { 'hello' : 'Hello' }, optionalLocale ); 54 | * If the data argument is 55 | * null/undefined/false, 56 | * all cached messages for the i18n instance will get reset. 57 | * 58 | * @param {string|Object} source 59 | * @param {string} locale Language tag 60 | * @return {jQuery.Promise} 61 | */ 62 | load: function ( source, locale ) { 63 | var key = null, 64 | deferred = null, 65 | deferreds = [], 66 | messageStore = this; 67 | 68 | if ( typeof source === 'string' ) { 69 | // This is a URL to the messages file. 70 | $.i18n.log( 'Loading messages from: ' + source ); 71 | deferred = jsonMessageLoader( source ) 72 | .done( function ( localization ) { 73 | messageStore.set( locale, localization ); 74 | } ); 75 | 76 | return deferred.promise(); 77 | } 78 | 79 | if ( locale ) { 80 | // source is an key-value pair of messages for given locale 81 | messageStore.set( locale, source ); 82 | 83 | return $.Deferred().resolve(); 84 | } else { 85 | // source is a key-value pair of locales and their source 86 | for ( key in source ) { 87 | if ( Object.prototype.hasOwnProperty.call( source, key ) ) { 88 | locale = key; 89 | // No {locale} given, assume data is a group of languages, 90 | // call this function again for each language. 91 | deferreds.push( messageStore.load( source[ key ], locale ) ); 92 | } 93 | } 94 | return $.when.apply( $, deferreds ); 95 | } 96 | 97 | }, 98 | 99 | /** 100 | * Set messages to the given locale. 101 | * If locale exists, add messages to the locale. 102 | * 103 | * @param {string} locale 104 | * @param {Object} messages 105 | */ 106 | set: function ( locale, messages ) { 107 | if ( !this.messages[ locale ] ) { 108 | this.messages[ locale ] = messages; 109 | } else { 110 | this.messages[ locale ] = $.extend( this.messages[ locale ], messages ); 111 | } 112 | }, 113 | 114 | /** 115 | * 116 | * @param {string} locale 117 | * @param {string} messageKey 118 | * @return {boolean} 119 | */ 120 | get: function ( locale, messageKey ) { 121 | return this.messages[ locale ] && this.messages[ locale ][ messageKey ]; 122 | } 123 | }; 124 | 125 | $.extend( $.i18n.messageStore, new MessageStore() ); 126 | }( jQuery ) ); 127 | -------------------------------------------------------------------------------- /src/main/webapp/js/lib/jquery.i18n/jquery.i18n.parser.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * jQuery Internationalization library 3 | * 4 | * Copyright (C) 2011-2013 Santhosh Thottingal, Neil Kandalgaonkar 5 | * 6 | * jquery.i18n is dual licensed GPLv2 or later and MIT. You don't have to do 7 | * anything special to choose one license or the other and you don't have to 8 | * notify anyone which license you are using. You are free to use 9 | * UniversalLanguageSelector in commercial projects as long as the copyright 10 | * header is left intact. See files GPL-LICENSE and MIT-LICENSE for details. 11 | * 12 | * @licence GNU General Public Licence 2.0 or later 13 | * @licence MIT License 14 | */ 15 | 16 | ( function ( $ ) { 17 | 'use strict'; 18 | 19 | var MessageParser = function ( options ) { 20 | this.options = $.extend( {}, $.i18n.parser.defaults, options ); 21 | this.language = $.i18n.languages[ String.locale ] || $.i18n.languages[ 'default' ]; 22 | this.emitter = $.i18n.parser.emitter; 23 | }; 24 | 25 | MessageParser.prototype = { 26 | 27 | constructor: MessageParser, 28 | 29 | simpleParse: function ( message, parameters ) { 30 | return message.replace( /\$(\d+)/g, function ( str, match ) { 31 | var index = parseInt( match, 10 ) - 1; 32 | 33 | return parameters[ index ] !== undefined ? parameters[ index ] : '$' + match; 34 | } ); 35 | }, 36 | 37 | parse: function ( message, replacements ) { 38 | if ( message.indexOf( '{{' ) < 0 ) { 39 | return this.simpleParse( message, replacements ); 40 | } 41 | 42 | this.emitter.language = $.i18n.languages[ $.i18n().locale ] || 43 | $.i18n.languages[ 'default' ]; 44 | 45 | return this.emitter.emit( this.ast( message ), replacements ); 46 | }, 47 | 48 | ast: function ( message ) { 49 | var pipe, colon, backslash, anyCharacter, dollar, digits, regularLiteral, 50 | regularLiteralWithoutBar, regularLiteralWithoutSpace, escapedOrLiteralWithoutBar, 51 | escapedOrRegularLiteral, templateContents, templateName, openTemplate, 52 | closeTemplate, expression, paramExpression, result, 53 | pos = 0; 54 | 55 | // Try parsers until one works, if none work return null 56 | function choice( parserSyntax ) { 57 | return function () { 58 | var i, result; 59 | 60 | for ( i = 0; i < parserSyntax.length; i++ ) { 61 | result = parserSyntax[ i ](); 62 | 63 | if ( result !== null ) { 64 | return result; 65 | } 66 | } 67 | 68 | return null; 69 | }; 70 | } 71 | 72 | // Try several parserSyntax-es in a row. 73 | // All must succeed; otherwise, return null. 74 | // This is the only eager one. 75 | function sequence( parserSyntax ) { 76 | var i, res, 77 | originalPos = pos, 78 | result = []; 79 | 80 | for ( i = 0; i < parserSyntax.length; i++ ) { 81 | res = parserSyntax[ i ](); 82 | 83 | if ( res === null ) { 84 | pos = originalPos; 85 | 86 | return null; 87 | } 88 | 89 | result.push( res ); 90 | } 91 | 92 | return result; 93 | } 94 | 95 | // Run the same parser over and over until it fails. 96 | // Must succeed a minimum of n times; otherwise, return null. 97 | function nOrMore( n, p ) { 98 | return function () { 99 | var originalPos = pos, 100 | result = [], 101 | parsed = p(); 102 | 103 | while ( parsed !== null ) { 104 | result.push( parsed ); 105 | parsed = p(); 106 | } 107 | 108 | if ( result.length < n ) { 109 | pos = originalPos; 110 | 111 | return null; 112 | } 113 | 114 | return result; 115 | }; 116 | } 117 | 118 | // Helpers -- just make parserSyntax out of simpler JS builtin types 119 | 120 | function makeStringParser( s ) { 121 | var len = s.length; 122 | 123 | return function () { 124 | var result = null; 125 | 126 | if ( message.slice( pos, pos + len ) === s ) { 127 | result = s; 128 | pos += len; 129 | } 130 | 131 | return result; 132 | }; 133 | } 134 | 135 | function makeRegexParser( regex ) { 136 | return function () { 137 | var matches = message.slice( pos ).match( regex ); 138 | 139 | if ( matches === null ) { 140 | return null; 141 | } 142 | 143 | pos += matches[ 0 ].length; 144 | 145 | return matches[ 0 ]; 146 | }; 147 | } 148 | 149 | pipe = makeStringParser( '|' ); 150 | colon = makeStringParser( ':' ); 151 | backslash = makeStringParser( '\\' ); 152 | anyCharacter = makeRegexParser( /^./ ); 153 | dollar = makeStringParser( '$' ); 154 | digits = makeRegexParser( /^\d+/ ); 155 | regularLiteral = makeRegexParser( /^[^{}\[\]$\\]/ ); 156 | regularLiteralWithoutBar = makeRegexParser( /^[^{}\[\]$\\|]/ ); 157 | regularLiteralWithoutSpace = makeRegexParser( /^[^{}\[\]$\s]/ ); 158 | 159 | // There is a general pattern: 160 | // parse a thing; 161 | // if it worked, apply transform, 162 | // otherwise return null. 163 | // But using this as a combinator seems to cause problems 164 | // when combined with nOrMore(). 165 | // May be some scoping issue. 166 | function transform( p, fn ) { 167 | return function () { 168 | var result = p(); 169 | 170 | return result === null ? null : fn( result ); 171 | }; 172 | } 173 | 174 | // Used to define "literals" within template parameters. The pipe 175 | // character is the parameter delimeter, so by default 176 | // it is not a literal in the parameter 177 | function literalWithoutBar() { 178 | var result = nOrMore( 1, escapedOrLiteralWithoutBar )(); 179 | 180 | return result === null ? null : result.join( '' ); 181 | } 182 | 183 | function literal() { 184 | var result = nOrMore( 1, escapedOrRegularLiteral )(); 185 | 186 | return result === null ? null : result.join( '' ); 187 | } 188 | 189 | function escapedLiteral() { 190 | var result = sequence( [ backslash, anyCharacter ] ); 191 | 192 | return result === null ? null : result[ 1 ]; 193 | } 194 | 195 | choice( [ escapedLiteral, regularLiteralWithoutSpace ] ); 196 | escapedOrLiteralWithoutBar = choice( [ escapedLiteral, regularLiteralWithoutBar ] ); 197 | escapedOrRegularLiteral = choice( [ escapedLiteral, regularLiteral ] ); 198 | 199 | function replacement() { 200 | var result = sequence( [ dollar, digits ] ); 201 | 202 | if ( result === null ) { 203 | return null; 204 | } 205 | 206 | return [ 'REPLACE', parseInt( result[ 1 ], 10 ) - 1 ]; 207 | } 208 | 209 | templateName = transform( 210 | // see $wgLegalTitleChars 211 | // not allowing : due to the need to catch "PLURAL:$1" 212 | makeRegexParser( /^[ !"$&'()*,.\/0-9;=?@A-Z\^_`a-z~\x80-\xFF+\-]+/ ), 213 | 214 | function ( result ) { 215 | return result.toString(); 216 | } 217 | ); 218 | 219 | function templateParam() { 220 | var expr, 221 | result = sequence( [ pipe, nOrMore( 0, paramExpression ) ] ); 222 | 223 | if ( result === null ) { 224 | return null; 225 | } 226 | 227 | expr = result[ 1 ]; 228 | 229 | // use a "CONCAT" operator if there are multiple nodes, 230 | // otherwise return the first node, raw. 231 | return expr.length > 1 ? [ 'CONCAT' ].concat( expr ) : expr[ 0 ]; 232 | } 233 | 234 | function templateWithReplacement() { 235 | var result = sequence( [ templateName, colon, replacement ] ); 236 | 237 | return result === null ? null : [ result[ 0 ], result[ 2 ] ]; 238 | } 239 | 240 | function templateWithOutReplacement() { 241 | var result = sequence( [ templateName, colon, paramExpression ] ); 242 | 243 | return result === null ? null : [ result[ 0 ], result[ 2 ] ]; 244 | } 245 | 246 | templateContents = choice( [ 247 | function () { 248 | var res = sequence( [ 249 | // templates can have placeholders for dynamic 250 | // replacement eg: {{PLURAL:$1|one car|$1 cars}} 251 | // or no placeholders eg: 252 | // {{GRAMMAR:genitive|{{SITENAME}}} 253 | choice( [ templateWithReplacement, templateWithOutReplacement ] ), 254 | nOrMore( 0, templateParam ) 255 | ] ); 256 | 257 | return res === null ? null : res[ 0 ].concat( res[ 1 ] ); 258 | }, 259 | function () { 260 | var res = sequence( [ templateName, nOrMore( 0, templateParam ) ] ); 261 | 262 | if ( res === null ) { 263 | return null; 264 | } 265 | 266 | return [ res[ 0 ] ].concat( res[ 1 ] ); 267 | } 268 | ] ); 269 | 270 | openTemplate = makeStringParser( '{{' ); 271 | closeTemplate = makeStringParser( '}}' ); 272 | 273 | function template() { 274 | var result = sequence( [ openTemplate, templateContents, closeTemplate ] ); 275 | 276 | return result === null ? null : result[ 1 ]; 277 | } 278 | 279 | expression = choice( [ template, replacement, literal ] ); 280 | paramExpression = choice( [ template, replacement, literalWithoutBar ] ); 281 | 282 | function start() { 283 | var result = nOrMore( 0, expression )(); 284 | 285 | if ( result === null ) { 286 | return null; 287 | } 288 | 289 | return [ 'CONCAT' ].concat( result ); 290 | } 291 | 292 | result = start(); 293 | 294 | /* 295 | * For success, the pos must have gotten to the end of the input 296 | * and returned a non-null. 297 | * n.b. This is part of language infrastructure, so we do not throw an internationalizable message. 298 | */ 299 | if ( result === null || pos !== message.length ) { 300 | throw new Error( 'Parse error at position ' + pos.toString() + ' in input: ' + message ); 301 | } 302 | 303 | return result; 304 | } 305 | 306 | }; 307 | 308 | $.extend( $.i18n.parser, new MessageParser() ); 309 | }( jQuery ) ); 310 | -------------------------------------------------------------------------------- /src/main/webapp/styles.css: -------------------------------------------------------------------------------- 1 | html { 2 | position: relative; 3 | min-height: 100%; 4 | } 5 | body { 6 | margin-bottom: 60px; 7 | } 8 | .footer { 9 | position: absolute; 10 | bottom: 0; 11 | width: 100%; 12 | height: 60px; 13 | line-height: 60px; 14 | text-align: center; 15 | background-color: #f5f7fa; 16 | } 17 | .helloInput { 18 | width: 300px; 19 | } 20 | -------------------------------------------------------------------------------- /src/main/wlp/server.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | beanValidation-1.1 5 | cdi-1.2 6 | ejbLite-3.2 7 | el-3.0 8 | jaxrs-2.0 9 | jdbc-4.1 10 | jndi-1.0 11 | jpa-2.1 12 | jsf-2.2 13 | jsonp-1.0 14 | jsp-2.3 15 | managedBeans-1.0 16 | servlet-3.1 17 | websocket-1.1 18 | 19 | 20 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/test/java/test/TestApplication.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2016 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | *******************************************************************************/ 16 | package test; 17 | 18 | public class TestApplication { 19 | 20 | } 21 | --------------------------------------------------------------------------------