tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From radcor...@apache.org
Subject [tomee] 01/06: created REST service.
Date Fri, 04 Jan 2019 11:59:59 GMT
This is an automated email from the ASF dual-hosted git repository.

radcortez pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomee.git

commit 036144446c8a60cfc18cba57b282fc45b9df2b25
Author: cotnic <mitja@cotnic.com>
AuthorDate: Sat Dec 29 09:11:09 2018 +0100

    created REST service.
    
    TODO: Implement the JWT JWK functionalities
---
 examples/mp-rest-jwt-jwk/README.adoc               |  0
 .../src/main/java/org/superbiz/entity/Product.java | 59 +++++++++++++++
 .../main/java/org/superbiz/rest/ProductRest.java   | 77 +++++++++++++++++++
 .../java/org/superbiz/rest/RestApplication.java    | 28 +++++++
 .../java/org/superbiz/service/ProductService.java  | 54 ++++++++++++++
 .../test/java/org/superbiz/rest/ProductsTest.java  | 86 ++++++++++++++++++++++
 6 files changed, 304 insertions(+)

diff --git a/examples/mp-rest-jwt-jwk/README.adoc b/examples/mp-rest-jwt-jwk/README.adoc
new file mode 100644
index 0000000..e69de29
diff --git a/examples/mp-rest-jwt-jwk/src/main/java/org/superbiz/entity/Product.java b/examples/mp-rest-jwt-jwk/src/main/java/org/superbiz/entity/Product.java
new file mode 100644
index 0000000..ae59578
--- /dev/null
+++ b/examples/mp-rest-jwt-jwk/src/main/java/org/superbiz/entity/Product.java
@@ -0,0 +1,59 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.entity;
+
+import java.math.BigDecimal;
+
+public class Product {
+
+    private Integer id;
+    private String name;
+    private BigDecimal price;
+    private Integer stock;
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public BigDecimal getPrice() {
+        return price;
+    }
+
+    public void setPrice(BigDecimal price) {
+        this.price = price;
+    }
+
+    public Integer getStock() {
+        return stock;
+    }
+
+    public void setStock(Integer stock) {
+        this.stock = stock;
+    }
+}
diff --git a/examples/mp-rest-jwt-jwk/src/main/java/org/superbiz/rest/ProductRest.java b/examples/mp-rest-jwt-jwk/src/main/java/org/superbiz/rest/ProductRest.java
new file mode 100644
index 0000000..499f634
--- /dev/null
+++ b/examples/mp-rest-jwt-jwk/src/main/java/org/superbiz/rest/ProductRest.java
@@ -0,0 +1,77 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.rest;
+
+
+import org.superbiz.entity.Product;
+import org.superbiz.service.ProductService;
+
+import javax.annotation.security.RolesAllowed;
+import javax.inject.Inject;
+import javax.ws.rs.*;
+import javax.ws.rs.core.MediaType;
+import java.util.List;
+
+@Path("store")
+@Produces(MediaType.APPLICATION_JSON)
+@Consumes(MediaType.APPLICATION_JSON)
+public class ProductRest {
+
+    @Inject
+    private ProductService productService;
+
+    @GET
+    @Produces(MediaType.TEXT_PLAIN)
+    public String status() {
+        return "running";
+    }
+
+    @GET
+    @Path("/products")
+    @RolesAllowed({"guest", "admin"})
+    public List<Product> getListOfMovies() {
+        return productService.getProducts();
+    }
+
+    @GET
+    @Path("/products/{id}")
+    @RolesAllowed({"guest", "admin"})
+    public Product getMovie(@PathParam("id") int id) {
+        return productService.getProduct(id);
+    }
+
+    @POST
+    @Path("/products")
+    @RolesAllowed({"admin"})
+    public void addMovie(Product product) {
+        productService.addProduct(product);
+    }
+
+    @DELETE
+    @Path("/products/{id}")
+    @RolesAllowed({"admin"})
+    public void deleteMovie(@PathParam("id") int id) {
+        productService.deleteProduct(id);
+    }
+
+    @PUT
+    @Path("/products")
+    @RolesAllowed({"admin"})
+    public void updateMovie(Product product) {
+        productService.updateProduct(product);
+    }
+}
diff --git a/examples/mp-rest-jwt-jwk/src/main/java/org/superbiz/rest/RestApplication.java
b/examples/mp-rest-jwt-jwk/src/main/java/org/superbiz/rest/RestApplication.java
new file mode 100644
index 0000000..b20d999
--- /dev/null
+++ b/examples/mp-rest-jwt-jwk/src/main/java/org/superbiz/rest/RestApplication.java
@@ -0,0 +1,28 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.rest;
+
+
+import org.eclipse.microprofile.auth.LoginConfig;
+
+import javax.ws.rs.ApplicationPath;
+import javax.ws.rs.core.Application;
+
+@ApplicationPath("/rest")
+@LoginConfig(authMethod = "MP-JWT")
+public class RestApplication extends Application {
+}
diff --git a/examples/mp-rest-jwt-jwk/src/main/java/org/superbiz/service/ProductService.java
b/examples/mp-rest-jwt-jwk/src/main/java/org/superbiz/service/ProductService.java
new file mode 100644
index 0000000..698c507
--- /dev/null
+++ b/examples/mp-rest-jwt-jwk/src/main/java/org/superbiz/service/ProductService.java
@@ -0,0 +1,54 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.service;
+
+import org.superbiz.entity.Product;
+
+import javax.annotation.PostConstruct;
+import javax.enterprise.context.ApplicationScoped;
+import java.util.*;
+
+@ApplicationScoped
+public class ProductService {
+
+    private Map<Integer, Product> productsInStore;
+
+    @PostConstruct
+    public void ProductService() {
+        productsInStore = new HashMap();
+    }
+
+    public List<Product> getProducts() {
+        return new ArrayList<>(productsInStore.values());
+    }
+
+    public Product getProduct(int id) {
+        return productsInStore.get(id);
+    }
+
+    public void addProduct(Product product) {
+        productsInStore.put(product.getId(), product);
+    }
+
+    public void deleteProduct(int id) {
+        productsInStore.remove(id);
+    }
+
+    public void updateProduct(Product product) {
+        productsInStore.put(product.getId(), product);
+    }
+}
diff --git a/examples/mp-rest-jwt-jwk/src/test/java/org/superbiz/rest/ProductsTest.java b/examples/mp-rest-jwt-jwk/src/test/java/org/superbiz/rest/ProductsTest.java
new file mode 100644
index 0000000..c8ceb6f
--- /dev/null
+++ b/examples/mp-rest-jwt-jwk/src/test/java/org/superbiz/rest/ProductsTest.java
@@ -0,0 +1,86 @@
+package org.superbiz.rest;
+
+import org.apache.cxf.feature.LoggingFeature;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.johnzon.jaxrs.JohnzonProvider;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.superbiz.entity.Product;
+import org.superbiz.service.ProductService;
+
+import java.net.URL;
+import java.util.logging.Logger;
+
+import static java.util.Collections.singletonList;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(Arquillian.class)
+public class ProductsTest {
+
+    private final static Logger LOGGER = Logger.getLogger(ProductsTest.class.getName());
+    @ArquillianResource
+    private URL base;
+
+    @Deployment(testable = false)
+    public static WebArchive createDeployment() {
+        final WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test.war")
+                .addClasses(Product.class, ProductService.class, ProductsTest.class)
+                .addClasses(ProductRest.class, RestApplication.class)
+//                .addClass(MoviesMPJWTConfigurationProvider.class)
+                .addAsWebInfResource(new StringAsset("<beans/>"), "beans.xml");
+
+        System.out.println(webArchive.toString(true));
+
+        return webArchive;
+    }
+
+    @Test
+    public void runningOfProductsApiTest() {
+
+        final WebClient webClient = WebClient
+                .create(base.toExternalForm(), singletonList(new JohnzonProvider<>()),
+                        singletonList(new LoggingFeature()), null);
+
+
+        //Testing rest endpoint deployment (GET  without security header)
+        String responsePayload = webClient.reset().path("/rest/store/").get(String.class);
+        LOGGER.info("responsePayload = " + responsePayload);
+        assertTrue(responsePayload.equalsIgnoreCase("running"));
+    }
+
+    @Test
+    public void createProductTest() {
+
+        throw new RuntimeException("TODO Implement!");
+    }
+
+    @Test
+    public void getAllProductsTest() {
+
+        throw new RuntimeException("TODO Implement!");
+    }
+
+    @Test
+    public void getProductWithIdTest() {
+
+        throw new RuntimeException("TODO Implement!");
+    }
+
+    @Test
+    public void updateProductTest() {
+
+        throw new RuntimeException("TODO Implement!");
+    }
+
+    @Test
+    public void deleteProductTest() {
+
+        throw new RuntimeException("TODO Implement!");
+    }
+}


Mime
View raw message