This commit is contained in:
2024-04-14 12:22:45 -05:00
parent 8f30705c62
commit f1c589e653
115 changed files with 4562 additions and 70 deletions

View File

@@ -0,0 +1,13 @@
package asdv.mavenproject3;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;
/**
* Configures Jakarta RESTful Web Services for the application.
* @author Juneau
*/
@ApplicationPath("resources")
public class JakartaRestConfiguration extends Application {
}

View File

@@ -0,0 +1,20 @@
package asdv.mavenproject3.resources;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Response;
/**
*
* @author
*/
@Path("jakartaee10")
public class JakartaEE10Resource {
@GET
public Response ping(){
return Response
.ok("ping Jakarta EE")
.build();
}
}

View File

@@ -0,0 +1,225 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package jason;
import java.io.StringReader;
import java.io.StringWriter;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonNumber;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import jakarta.json.JsonWriter;
import jakarta.json.stream.JsonParser;
import java.util.ArrayList;
/**
*
* @author ASDV2
*/
public class Jason
{
public static JsonObject createObject()
{
JsonObject model = Json.createObjectBuilder()
.add("firstName", "Duke")
.add("lastName", "Java")
.add("age", 18)
.add("streetAddress", "100 Internet Dr")
.add("city", "JavaTown")
.add("state", "JA")
.add("postalCode", "12345")
.add("phoneNumbers", Json.createArrayBuilder()
.add(Json.createObjectBuilder().add("type", "mobile").add("number", "111-111-1111"))
.add(Json.createObjectBuilder().add("type", "home").add("number", "222-222-2222")))
.build();
return model;
}
public static JsonObject createDukeObject()
{
/*
JsonObjectBuilder job = Json.createObjectBuilder();
job = job.add("firstName", "Duke");
job = job.add("lastName", "Java");
JsonObject modelPartial = job.build();
return modelPartial;
*/
JsonArrayBuilder jab = Json.createArrayBuilder();
//one array
jab = jab.add( //1st object
Json.createObjectBuilder()
.add("type", "mobile")
.add("number", "111-111-1111"))
//2nd object
.add(
Json.createObjectBuilder()
.add("type", "home")
.add("number", "222-222-2222")
);
JsonObject model = Json.createObjectBuilder()
.add("firstName", "Duke")
.add("lastName", "Java")
.add("age", 18)
.add("streetAddress", "100 Internet Dr")
.add("city", "JavaTown")
.add("state", "JA")
.add("postalCode", "12345")
.add("phoneNumbers", jab)
/*
.add(
Json.createObjectBuilder()
.add("type", "home")
.add("number", "222-222-2222")
)
*/
.build();
return model;
}
public static ArrayList navigateTree(JsonValue tree, String key, ArrayList list)
{
if (key != null)
{
System.out.print("Key " + key + ": ");
list.add("Key " + key + ": ");
}
switch (tree.getValueType())
{
case OBJECT:
System.out.println("OBJECT");
list.add("OBJECT");
JsonObject object = (JsonObject) tree;
for (String name : object.keySet())
{
navigateTree(object.get(name), name, list);
}
break;
case ARRAY:
System.out.println("ARRAY");
list.add("ARRAY");
JsonArray array = (JsonArray) tree;
for (JsonValue val : array)
{
navigateTree(val, null, list);
}
break;
case STRING:
JsonString st = (JsonString) tree;
System.out.println("STRING " + st.getString());
list.add("STRING" + st.getString() ) ;
break;
case NUMBER:
JsonNumber num = (JsonNumber) tree;
System.out.println("NUMBER " + num.toString());
list.add("NUMBER" + num.toString() ) ;
break;
case TRUE:
case FALSE:
case NULL:
System.out.println(tree.getValueType().toString());
list.add(tree.getValueType().toString());
break;
}
return list;
}
public static String getJsonString(JsonObject jo)
{
StringWriter stWriter = new StringWriter();
JsonWriter jsonWriter = Json.createWriter(stWriter);
jsonWriter.writeObject(jo);
jsonWriter.close();
String jsonData = stWriter.toString();
return jsonData;
}
public static String parseObject(JsonObject jo)
{
JsonParser parser = Json.createParser( new StringReader(jo.toString()) );
String s = "";
while (parser.hasNext())
{
JsonParser.Event event = parser.next();
switch (event)
{
case START_ARRAY:
case END_ARRAY:
case START_OBJECT:
case END_OBJECT:
case VALUE_FALSE:
case VALUE_NULL:
case VALUE_TRUE:
s += "\n" + event.toString() + "\n";
break;
case KEY_NAME:
s += event.toString() + " "
+ parser.getString() + " - ";
break;
case VALUE_STRING:
case VALUE_NUMBER:
s += event.toString() + " "
+ parser.getString();
break;
}
}
return s;
}
public static String testCreateComplexObject()
{
JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
jsonObjectBuilder.add("emp_name", "John");
jsonObjectBuilder.add("emp_id", 1016);
jsonObjectBuilder.add("salary", 80000);
//> create Json array with only values
JsonArrayBuilder jab = Json.createArrayBuilder();
jab.add("Nick");
jab.add("John");
jab.add(888);
JsonArray ja = jab.build();
//> the array got created,
//add it to the json as a child element
jsonObjectBuilder.add("direct_contacts", ja);
//> create an array of key-value pairs
JsonArrayBuilder kvArrBld = Json.createArrayBuilder();
// create each key-value pair as seperate object and add it to the array
kvArrBld.add(Json.createObjectBuilder().add("email", "java2novice@gmail.com").build());
kvArrBld.add(Json.createObjectBuilder().add("mobile", "123123123123").build());
JsonArray contactsArr = kvArrBld.build();
// add contacts array object
jsonObjectBuilder.add("contacts", contactsArr);
JsonObject empObj = jsonObjectBuilder.build();
// Write to String writer.
// if you want you can write it to a file as well
StringWriter strWtr = new StringWriter();
JsonWriter jsonWtr = Json.createWriter(strWtr);
jsonWtr.writeObject(empObj);
jsonWtr.close();
System.out.println(strWtr.toString());
System.out.println("calling the method getJsonString()");
System.out.println(getJsonString(empObj));
return empObj.toString();
}
public static void main(String[] args)
{
JsonObject jo = createObject();
System.out.println(jo);
//JsonObject jo = createDukeObject();
//System.out.println(jo);
//navigateTree(jo, null);
//System.out.println(getJsonString(jo));
//System.out.println(parseObject(jo));
//testCreateComplexObject();
}
}

View File

@@ -0,0 +1,81 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/JSF/JSFManagedBean.java to edit this template
*/
package jason;
import jakarta.inject.Named;
import jakarta.enterprise.context.RequestScoped;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
/**
*
* @author asdv5
*/
@Named(value = "jsonBean")
@RequestScoped
public class JsonBean
{
private String s;
private String fromWriter;
private String fromParser;
private String complexObject;
private String suppliersObject;
private ArrayList list = new ArrayList();
public JsonBean()
{
s = Jason.createObject().toString();
list = Jason.navigateTree(JsonValue.TRUE, s, list);
list = Jason.navigateTree(Jason.createObject(), null, list);
fromWriter = Jason.getJsonString(Jason.createObject());
fromParser = Jason.parseObject(Jason.createObject());
complexObject = Jason.testCreateComplexObject();
suppliersObject = JsonSupplier.getSuppliersJsonObject();
}
public String getS()
{
return s;
}
public ArrayList getList()
{
return list;
}
public String getFromWriter()
{
return fromWriter;
}
public String getFromParser()
{
return fromParser;
}
public String getComplexObject() {
return complexObject;
}
public String getSuppliersObject() {
return suppliersObject;
}
public void writeSuppliersToFile() throws FileNotFoundException{
JsonObject jo = JsonSupplier.createJsonObjectFromString(this.suppliersObject);
JsonSupplier.writeJsonToTextFile(jo);
}
public void readSuppliersFromFile() throws FileNotFoundException {
JsonObject jo = JsonSupplier.readFileIntoJson("jason.json");
s = jo.toString();
}
}

View File

@@ -0,0 +1,160 @@
package jason;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonReader;
import jakarta.json.JsonValue;
import jakarta.json.JsonWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class JsonSupplier {
/**
* Creates a JsonObject by traversing the arrayList of LinkedHashMap
*
* @param suppliers ArrayList of LinkedHashMap. Each element of the
* arrayList is a row of suppliers table. The key of the LinkedHashMap is
* the attribute name and the value of the attribute Example ( One liked
* hash map is one row from the table) LinkedHashMap 1 : key: snumber,
* value: s1 key: sname, value: white key: status, value: 20 key: city,
* value London LinkedHashMap 2 : key: snumber, value: s2 key: sname, value:
* black key: status, value: 30 key: city, value Paris The value of the map
* @return JsonObject
*/
public static JsonObject createJsonObjectForSuppliers(ArrayList< LinkedHashMap<String, String>> suppliers) {
JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
int counter = 1;
for (LinkedHashMap<String, String> supplier : suppliers)
{
// create an array of key-value pairs
JsonArrayBuilder arraySupplierBld = Json.createArrayBuilder();
// create each key-value pair as seperate object and add it to the array
Set<Map.Entry<String, String>> entrySet = supplier.entrySet();
for (Map.Entry<String, String> entry : entrySet)
{
arraySupplierBld.add(Json.createObjectBuilder().add(entry.getKey(),
entry.getValue()).build());
}
// add supplier-array to object
String objectID = "+" + (counter++);
jsonBuilder.add(objectID, arraySupplierBld);
}
JsonObject allSuppliersJsonObject = jsonBuilder.build();
return allSuppliersJsonObject;
}
/**
* Creates a JsonObject by reading the String data in JSON format
*
* @param jsonData Json format data
* @return JsonObject
*/
public static JsonObject createJsonObjectFromString(String jsonData) {
JsonReader jsonReader = Json.createReader(new StringReader(jsonData));
JsonObject o = jsonReader.readObject();
return o;
}
/**
* Creates a LinkedHashMap of 4 ENTRYies.Keys name: snumber, sname, status
* and city. Values of map are the parameters.
*
* @param snumber value of map
* @param sname value of map
* @param status value of map
* @param city value of map
* @return
*/
public static LinkedHashMap<String, String> createMapOfSupplier(
String snumber,
String sname,
String status,
String city
)
{
LinkedHashMap<String, String> mapSupplier
= new LinkedHashMap<String, String>();
mapSupplier.put("snumber", snumber);
mapSupplier.put("sname", sname);
mapSupplier.put("status", status);
mapSupplier.put("city", city);
return mapSupplier;
}
public static String getSuppliersJsonObject(){
ArrayList< LinkedHashMap<String, String>> suppliers = new ArrayList();
for (int i = 1; i <= 2; ++i)
{
LinkedHashMap<String, String> oneSupplierRowOfTable
= createMapOfSupplier(
"s1" + i,
"Johnson" + i,
Integer.toString(20 + i),
"london" + i);
suppliers.add(oneSupplierRowOfTable);
}
JsonObject j = createJsonObjectForSuppliers(suppliers);
return j.toString();
}
public static void main(String a[]) {
ArrayList< LinkedHashMap<String, String>> suppliers = new ArrayList<>();
for (int i = 1; i <= 2; ++i)
{
LinkedHashMap<String, String> oneSupplierRowOfTable
= createMapOfSupplier(
"s1" + i,
"Johnson" + i,
Integer.toString(20 + i),
"london" + i);
suppliers.add(oneSupplierRowOfTable);
}
JsonObject j = createJsonObjectForSuppliers(suppliers);
StringWriter strWtr = new StringWriter();
JsonWriter jsonWtr = Json.createWriter(strWtr);
jsonWtr.writeObject(j);
String s = strWtr.toString();
System.out.println(s);
jsonWtr.close();
/*
JSONobj.readJASONdataUsingParser(strWtr.toString());
JsonObject o = createJsonObjectFromString(strWtr.toString());
System.out.println("------------------------------------------------");
JSONobj.writeObjectModelToStream(o);
*/
}
public static void writeJsonToTextFile(JsonObject jo) throws FileNotFoundException {
String jsonString = Jason.getJsonString(jo);
PrintWriter output = new PrintWriter(new FileOutputStream("jason.json", true));
output.print(jsonString);
output.close();
}
public static JsonObject readFileIntoJson(String fileName) throws FileNotFoundException {
File file = new java.io.File(fileName);
Scanner input = new Scanner(file);
String joString = "";
while (!input.hasNext()) {
joString += input.next();
}
input.close();
JsonObject jo = createJsonObjectFromString(joString);
return jo;
}
}

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="3.0" xmlns="https://jakarta.ee/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd">
<!-- Define Persistence Unit -->
<persistence-unit name="my_persistence_unit">
</persistence-unit>
</persistence>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd"
bean-discovery-mode="all">
</beans>

View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<!--
Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->
<glassfish-web-app error-url="">
<class-loader delegate="true"/>
<jsp-config>
<property name="keepgenerated" value="true">
<description>Keep a copy of the generated servlet class' java code.</description>
</property>
</jsp-config>
</glassfish-web-app>

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="5.0" xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd">
<context-param>
<param-name>jakarta.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>jakarta.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>

View File

@@ -0,0 +1,21 @@
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
#{jsonBean.s}
<br/><br/><br/>
#{jsonBean.getList()}
<br/><br/><br/>
#{jsonBean.fromWriter}
<br/><br/><br/>
#{jsonBean.fromParser}
<br/><br/><br/>
#{jsonBean.complexObject}
<br/><br/><br/>
#{jsonBean.suppliersObject}
</h:body>
</html>