Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions samples/quickstart/app/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (c) 2020, 2023, Oracle and/or its affiliates. -->
<!-- Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.oracle.weblogic.example</groupId>
<artifactId>todo</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>todo</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1.4</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<finalName>todo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>enforce-build-environment</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>1.8.0</version>
<message>You must use JDK 8 to build the project WebLogic 12.2.1.4 only supports JDK 8</message>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2020, 2023, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package org.todo.services;

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import org.todo.services.resource.ItemResource;
import org.todo.services.resource.ItemsResource;

@ApplicationPath("/rest")
public class TodoListApplication extends Application {
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<>();
s.add(ItemsResource.class);
s.add(ItemResource.class);
return s;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2020, 2023, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package org.todo.services.entity;

import javax.json.Json;
import javax.json.JsonObject;

public class Item {
private int key;
private String description;
private boolean complete;

public Item(int id) {
key = id;
complete = false;
}

public int id() {
return key;
}

public String desc() {
return description;
}

public Item desc(String value) {
description = value;
return this;
}

public boolean done() {
return complete;
}

public Item done(boolean value) {
complete = value;
return this;
}

public JsonObject toJson() {
return Json.createObjectBuilder()
.add("id", id())
.add( "description", desc())
.add( "done", done())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) 2020, 2023, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package org.todo.services.resource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.json.JsonObject;
import javax.naming.NamingException;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.todo.services.entity.Item;

@Path("/item")
public class ItemResource {

public final static String selectSql = "select task, completed from ToDos where taskId = %s";
public final static String deleteSql = "delete from ToDos where taskId = %s";
public final static String insertSql = "insert into ToDos(task, completed) values('%s', false);";
public final static String updateSql = "update ToDos set completed = %s where taskId = %s";

@GET
@Path("/{id}")
@Produces("application/json")
public JsonObject item(@PathParam("id") String id) {
Item result = null;
try (Connection conn = ItemsResource.datasource().getConnection()) {
Statement statement = conn.createStatement();
String queryStr = String.format(selectSql, id);
System.out.println(queryStr);
ResultSet resultSet = statement.executeQuery(queryStr);
if (resultSet.next()) {
String task = resultSet.getString("task");
boolean complete = resultSet.getBoolean("completed");
result = new Item(Integer.parseInt(id)).desc(task).done(complete);
}
} catch (SQLException | NamingException ex) {
ex.printStackTrace();
}
return result == null ? null : result.toJson();
}

@DELETE
@Path("/{id}")
public void delete(@PathParam("id") String id) {
runQuery(String.format(deleteSql, id));
}

@PUT
@Path("/{taskDescription}")
public void addNewItem(@PathParam("taskDescription") String description) {
runQuery(String.format(insertSql, description));
}

@PUT
@Path("/{id}/{status}")
public void updateStatus(@PathParam("id") String id, @PathParam("status") String status) {
runQuery(String.format(updateSql, id, status));
}

private void runQuery(String query) {
try (Connection conn = ItemsResource.datasource().getConnection()) {
Statement statement = conn.createStatement();
System.out.println(query);
statement.executeUpdate(query);
} catch (SQLException | NamingException ex) {
ex.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright (c) 2020, 2023, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package org.todo.services.resource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.todo.services.entity.Item;

/**
* REST service for the To-Do list application using MySQL DB.
* /items retrieves the full list of tasks
* /items/init drops the table, creates the table, and loads the table with some starting tasks
*/
@Path("/items/")
@Produces(MediaType.APPLICATION_JSON)
public class ItemsResource {

public static DataSource datasource() throws NamingException {
InitialContext ctx = new InitialContext();
return (DataSource) ctx.lookup("jdbc/ToDoDB");
}

public List<Item> items() {
List<Item> result = new ArrayList<>();

try (Connection conn = datasource().getConnection()){
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("select taskId, task, completed from ToDos");
while (resultSet.next()) {
int id = resultSet.getInt("taskId");
String task = resultSet.getString("task");
boolean complete = resultSet.getBoolean("completed");
result.add(new Item(id).desc(task).done(complete));
}
} catch (SQLException | NamingException ex) {
ex.printStackTrace();
}
return result;
}

@GET
public JsonArray itemsJson() {
JsonArrayBuilder result = Json.createArrayBuilder();
for (Item item : items()) {
result.add(item.toJson());
}
return result.build();
}

@GET
@Path("/drop/")
@Produces(MediaType.TEXT_PLAIN)
public Response dropTable() {
try (Connection conn = datasource().getConnection()) {
Statement stmt = conn.createStatement();

String dropTable = "drop table ToDos;";
System.out.println(dropTable);
stmt.executeUpdate(dropTable);
} catch (SQLException | NamingException ex) {
// ok to fail, table may not exist yet.
return Response.ok().entity(ex.getLocalizedMessage() + "\n").build();
}
return Response.ok().entity("ToDos table dropped.\n").build();
}

@GET
@Path("/init/")
@Produces(MediaType.TEXT_PLAIN)
public Response initTable() {
dropTable();
try (Connection conn = datasource().getConnection()){
Statement stmt = conn.createStatement();

String createTable = "create table ToDos (" +
"taskId INT NOT NULL AUTO_INCREMENT, " +
"task VARCHAR(200) NOT NULL, " +
"completed BOOLEAN," +
"constraint todo_pk PRIMARY KEY (taskId));";

System.out.println(createTable);
stmt.executeUpdate(createTable);

String[] tasks = {"Install Verrazzano", "Move ToDo List to the cloud", "Celebrate", "Clean off my desk"};
for (String task : tasks) {
String insert = String.format(ItemResource.insertSql, task);
System.out.println(insert);
stmt.executeUpdate(insert);
}

} catch (SQLException | NamingException ex) {
ex.printStackTrace();
return Response.serverError().entity("ERROR: " + ex.getLocalizedMessage() + "\n").build();
}
return Response.ok().entity("ToDos table initialized.\n").build();
}
}
13 changes: 13 additions & 0 deletions samples/quickstart/app/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (c) 2020, 2023, Oracle and/or its affiliates. -->
<!-- Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. -->

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Derek's ToDo List</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Loading