Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
ws-calculator-service
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DAI 23-24
ws-calculator-service
Commits
ec32b948
Commit
ec32b948
authored
Nov 27, 2023
by
Administrator
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds advance examples
These examples include the use of JAX-B advanced features.
parent
f2553ac4
Changes
14
Show whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
448 additions
and
17 deletions
+448
-17
pom.xml
pom.xml
+1
-1
CalculatorException.java
.../es/uvigo/esei/dai/ws/calculator/CalculatorException.java
+4
-4
CalculatorService.java
...va/es/uvigo/esei/dai/ws/calculator/CalculatorService.java
+41
-5
CalculatorServiceClient.java
...uvigo/esei/dai/ws/calculator/CalculatorServiceClient.java
+33
-1
CalculatorServiceImpl.java
...s/uvigo/esei/dai/ws/calculator/CalculatorServiceImpl.java
+87
-6
Operation.java
src/main/java/es/uvigo/esei/dai/ws/calculator/Operation.java
+27
-0
NamedOperations.java
...vigo/esei/dai/ws/calculator/adapters/NamedOperations.java
+31
-0
NamedOperationsEntry.java
...esei/dai/ws/calculator/adapters/NamedOperationsEntry.java
+38
-0
NamedOperationsMap.java
...o/esei/dai/ws/calculator/adapters/NamedOperationsMap.java
+42
-0
NamedOperationsMapAdapter.java
...dai/ws/calculator/adapters/NamedOperationsMapAdapter.java
+20
-0
NamedResults.java
...s/uvigo/esei/dai/ws/calculator/adapters/NamedResults.java
+29
-0
NamedResultsEntry.java
...go/esei/dai/ws/calculator/adapters/NamedResultsEntry.java
+36
-0
NamedResultsMap.java
...vigo/esei/dai/ws/calculator/adapters/NamedResultsMap.java
+40
-0
NamedResultsMapAdapter.java
...ei/dai/ws/calculator/adapters/NamedResultsMapAdapter.java
+19
-0
No files found.
pom.xml
View file @
ec32b948
...
...
@@ -7,7 +7,7 @@
<groupId>
es.uvigo.esei.dai.ws
</groupId>
<artifactId>
calculator_service
</artifactId>
<version>
1.
0
.0
</version>
<version>
1.
1
.0
</version>
<name>
Ejemplos de DAI - Servicios Web: Calculator Service
</name>
<inceptionYear>
2014
</inceptionYear>
...
...
src/main/java/es/uvigo/esei/dai/ws/calculator/CalculatorException.java
View file @
ec32b948
src/main/java/es/uvigo/esei/dai/ws/calculator/CalculatorService.java
View file @
ec32b948
package
es
.
uvigo
.
esei
.
dai
.
ws
.
calculator
;
import
java.util.Collection
;
import
es.uvigo.esei.dai.ws.calculator.adapters.NamedOperations
;
import
es.uvigo.esei.dai.ws.calculator.adapters.NamedResults
;
import
jakarta.jws.WebMethod
;
import
jakarta.jws.WebService
;
@WebService
// @SOAPBinding(style = Style.RPC)
public
interface
CalculatorService
{
@WebMethod
public
double
add
(
double
op1
,
double
op2
);
...
...
@@ -16,5 +19,38 @@ public interface CalculatorService {
public
double
multiply
(
double
op1
,
double
op2
);
@WebMethod
public
double
divide
(
double
op1
,
double
op2
)
throws
CalculatorException
;
public
double
divide
(
double
op1
,
double
op2
)
throws
CalculatorException
;
@WebMethod
public
double
operate
(
Operation
operation
,
double
op1
,
double
op2
)
throws
CalculatorException
;
@WebMethod
public
double
serialOperate
(
Operation
operation
,
double
...
op
)
throws
CalculatorException
;
@WebMethod
public
double
arrayOperate
(
Operation
operation
,
double
[]
op
)
throws
CalculatorException
;
@WebMethod
public
double
listOperate
(
Operation
operation
,
Collection
<
Double
>
op
)
throws
CalculatorException
;
/*
* JAXB no soporta esta declaración de método porque hace uso de Map,
* que es una interfaz y JAXB no es capaz de trabajar con interfaces
* directamente.
*
* @WebMethod
* public Map<String, Double> mapOperate(
* Map<String, Operation> operations, double op1, double op2
* ) throws CalculatorException;
*/
@WebMethod
public
NamedResults
mapOperate
(
NamedOperations
operations
,
double
op1
,
double
op2
)
throws
CalculatorException
;
}
src/main/java/es/uvigo/esei/dai/ws/calculator/CalculatorServiceClient.java
View file @
ec32b948
...
...
@@ -2,9 +2,14 @@ package es.uvigo.esei.dai.ws.calculator;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.Map
;
import
javax.xml.namespace.QName
;
import
es.uvigo.esei.dai.ws.calculator.adapters.NamedOperations
;
import
es.uvigo.esei.dai.ws.calculator.adapters.NamedResults
;
import
jakarta.xml.ws.Service
;
public
class
CalculatorServiceClient
{
...
...
@@ -20,10 +25,37 @@ public class CalculatorServiceClient {
final
CalculatorService
calculator
=
service
.
getPort
(
CalculatorService
.
class
);
System
.
out
.
println
(
"Basic methods results"
);
System
.
out
.
println
(
calculator
.
add
(
20
d
,
30
d
));
System
.
out
.
println
(
calculator
.
subtract
(
20
d
,
30
d
));
System
.
out
.
println
(
calculator
.
multiply
(
20
d
,
0
d
));
System
.
out
.
println
(
calculator
.
multiply
(
20
d
,
3
0
d
));
System
.
out
.
println
(
calculator
.
divide
(
20
d
,
30
d
));
System
.
out
.
println
(
"\nOperate results"
);
System
.
out
.
println
(
calculator
.
operate
(
Operation
.
ADD
,
20
d
,
30
d
));
System
.
out
.
println
(
calculator
.
operate
(
Operation
.
SUB
,
20
d
,
30
d
));
System
.
out
.
println
(
calculator
.
operate
(
Operation
.
MUL
,
20
d
,
30
d
));
System
.
out
.
println
(
calculator
.
operate
(
Operation
.
DIV
,
20
d
,
30
d
));
System
.
out
.
println
(
"\nCollections results"
);
System
.
out
.
println
(
"Serial: "
+
calculator
.
serialOperate
(
Operation
.
ADD
,
10
d
,
20
d
,
30
d
));
System
.
out
.
println
(
"Array: "
+
calculator
.
arrayOperate
(
Operation
.
ADD
,
new
double
[]{
10
d
,
20
d
,
30
d
}));
System
.
out
.
println
(
"List: "
+
calculator
.
listOperate
(
Operation
.
ADD
,
Arrays
.
asList
(
10
d
,
20
d
,
30
d
)));
System
.
out
.
println
(
"\nMap results"
);
final
Map
<
String
,
Operation
>
operations
=
new
HashMap
<
String
,
Operation
>();
for
(
Operation
operation
:
Operation
.
values
())
{
operations
.
put
(
operation
.
name
(),
operation
);
}
final
NamedResults
results
=
calculator
.
mapOperate
(
new
NamedOperations
(
operations
),
20
d
,
30
d
);
for
(
Map
.
Entry
<
String
,
Double
>
entry
:
results
.
getResults
().
entrySet
())
{
System
.
out
.
println
(
entry
.
getKey
()
+
": "
+
entry
.
getValue
());
}
System
.
out
.
println
(
"\nException"
);
try
{
System
.
out
.
println
(
calculator
.
divide
(
20
d
,
0
d
));
}
catch
(
CalculatorException
e
)
{
...
...
src/main/java/es/uvigo/esei/dai/ws/calculator/CalculatorServiceImpl.java
View file @
ec32b948
package
es
.
uvigo
.
esei
.
dai
.
ws
.
calculator
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.Map
;
import
es.uvigo.esei.dai.ws.calculator.adapters.NamedOperations
;
import
es.uvigo.esei.dai.ws.calculator.adapters.NamedResults
;
import
jakarta.jws.WebService
;
@WebService
(
endpointInterface
=
"es.uvigo.esei.dai.ws.calculator.CalculatorService"
)
public
class
CalculatorServiceImpl
implements
CalculatorService
{
@Override
public
double
add
(
double
op1
,
double
op2
)
{
return
op1
+
op2
;
return
this
.
operate
(
Operation
.
ADD
,
op1
,
op2
)
;
}
@Override
public
double
subtract
(
double
op1
,
double
op2
)
{
return
op1
-
op2
;
return
this
.
operate
(
Operation
.
SUB
,
op1
,
op2
)
;
}
@Override
public
double
multiply
(
double
op1
,
double
op2
)
{
return
op1
*
op2
;
return
this
.
operate
(
Operation
.
MUL
,
op1
,
op2
)
;
}
@Override
public
double
divide
(
double
op1
,
double
op2
)
throws
CalculatorException
{
if
(
op2
==
0
d
)
throw
new
CalculatorException
(
"op2 can't be zero"
);
return
this
.
operate
(
Operation
.
DIV
,
op1
,
op2
);
}
@Override
public
double
operate
(
Operation
operation
,
double
op1
,
double
op2
)
throws
CalculatorException
{
return
operation
.
calculate
(
op1
,
op2
);
}
@Override
public
double
serialOperate
(
Operation
operation
,
double
...
op
)
throws
CalculatorException
{
if
(
op
.
length
<=
1
)
{
throw
new
CalculatorException
(
"There must be, at least, two operands"
);
}
else
{
double
result
=
op
[
0
];
for
(
int
i
=
1
;
i
<
op
.
length
;
i
++)
{
result
=
operation
.
calculate
(
result
,
op
[
i
]);
}
return
result
;
}
}
@Override
public
double
arrayOperate
(
Operation
operation
,
double
[]
op
)
throws
CalculatorException
{
return
this
.
serialOperate
(
operation
,
op
);
}
@Override
public
double
listOperate
(
Operation
operation
,
Collection
<
Double
>
op
)
throws
CalculatorException
{
if
(
op
.
size
()
<=
1
)
{
throw
new
CalculatorException
(
"There must be, at least, two operands"
);
}
else
{
final
List
<
Double
>
values
=
new
ArrayList
<
Double
>(
op
);
double
result
=
values
.
get
(
0
);
for
(
int
i
=
1
;
i
<
op
.
size
();
i
++)
{
result
=
operation
.
calculate
(
result
,
values
.
get
(
i
));
}
return
result
;
}
}
/*
* Ejemplo de implementación para declaración no válida de "mapOperate"
*
* @Override
* public Map<String, Double> mapOperate(
* Map<String, Operation> operations, double op1, double op2
* ) throws CalculatorException {
* final Map<String, Double> results = new HashMap<String, Double>();
*
* for (Map.Entry<String, Operation> entry : operations.entrySet()) {
* results.put(entry.getKey(), entry.getValue().calculate(op1, op2));
* }
* return results;
* }
*/
@Override
public
NamedResults
mapOperate
(
NamedOperations
operations
,
double
op1
,
double
op2
)
throws
CalculatorException
{
final
NamedResults
results
=
new
NamedResults
();
final
Map
<
String
,
Operation
>
operationsMap
=
operations
.
getOperations
();
final
Map
<
String
,
Double
>
resultsMap
=
results
.
getResults
();
for
(
Map
.
Entry
<
String
,
Operation
>
entry
:
operationsMap
.
entrySet
())
{
resultsMap
.
put
(
entry
.
getKey
(),
entry
.
getValue
().
calculate
(
op1
,
op2
));
}
return
op1
/
op2
;
return
results
;
}
}
src/main/java/es/uvigo/esei/dai/ws/calculator/Operation.java
0 → 100644
View file @
ec32b948
package
es
.
uvigo
.
esei
.
dai
.
ws
.
calculator
;
public
enum
Operation
{
ADD
,
SUB
,
MUL
,
DIV
;
public
double
calculate
(
double
op1
,
double
op2
)
throws
CalculatorException
{
switch
(
this
)
{
case
ADD:
return
op1
+
op2
;
case
SUB:
return
op1
-
op2
;
case
MUL:
return
op1
*
op2
;
case
DIV:
if
(
op2
==
0
d
)
{
throw
new
CalculatorException
(
"op2 can't be zero"
,
"op2 can't be zero"
);
}
else
{
return
op1
/
op2
;
}
default
:
throw
new
CalculatorException
(
"Unknown operation: "
+
this
,
"Unknown operation: "
+
this
);
}
}
}
src/main/java/es/uvigo/esei/dai/ws/calculator/adapters/NamedOperations.java
0 → 100644
View file @
ec32b948
package
es
.
uvigo
.
esei
.
dai
.
ws
.
calculator
.
adapters
;
import
java.util.HashMap
;
import
java.util.Map
;
import
es.uvigo.esei.dai.ws.calculator.Operation
;
import
jakarta.xml.bind.annotation.XmlRootElement
;
import
jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter
;
@XmlRootElement
public
class
NamedOperations
{
private
Map
<
String
,
Operation
>
operations
;
public
NamedOperations
()
{
this
.
operations
=
new
HashMap
<
String
,
Operation
>();
}
public
NamedOperations
(
Map
<
String
,
Operation
>
operations
)
{
super
();
this
.
operations
=
operations
;
}
@XmlJavaTypeAdapter
(
NamedOperationsMapAdapter
.
class
)
public
Map
<
String
,
Operation
>
getOperations
()
{
return
operations
;
}
public
void
setOperations
(
Map
<
String
,
Operation
>
operations
)
{
this
.
operations
=
operations
;
}
}
src/main/java/es/uvigo/esei/dai/ws/calculator/adapters/NamedOperationsEntry.java
0 → 100644
View file @
ec32b948
package
es
.
uvigo
.
esei
.
dai
.
ws
.
calculator
.
adapters
;
import
java.util.Map
;
import
es.uvigo.esei.dai.ws.calculator.Operation
;
public
class
NamedOperationsEntry
{
private
String
key
;
private
Operation
value
;
public
NamedOperationsEntry
()
{
}
public
NamedOperationsEntry
(
String
key
,
Operation
value
)
{
this
.
key
=
key
;
this
.
value
=
value
;
}
public
NamedOperationsEntry
(
Map
.
Entry
<
String
,
Operation
>
entry
)
{
this
(
entry
.
getKey
(),
entry
.
getValue
());
}
public
String
getKey
()
{
return
key
;
}
public
void
setKey
(
String
key
)
{
this
.
key
=
key
;
}
public
Operation
getValue
()
{
return
value
;
}
public
void
setValue
(
Operation
value
)
{
this
.
value
=
value
;
}
}
src/main/java/es/uvigo/esei/dai/ws/calculator/adapters/NamedOperationsMap.java
0 → 100644
View file @
ec32b948
package
es
.
uvigo
.
esei
.
dai
.
ws
.
calculator
.
adapters
;
import
java.util.HashMap
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.Map
;
import
es.uvigo.esei.dai.ws.calculator.Operation
;
public
class
NamedOperationsMap
{
private
List
<
NamedOperationsEntry
>
entries
;
public
NamedOperationsMap
()
{
this
.
entries
=
new
LinkedList
<
NamedOperationsEntry
>();
}
public
NamedOperationsMap
(
Map
<
String
,
Operation
>
map
)
{
this
();
for
(
Map
.
Entry
<
String
,
Operation
>
entry
:
map
.
entrySet
())
{
this
.
entries
.
add
(
new
NamedOperationsEntry
(
entry
));
}
}
public
Map
<
String
,
Operation
>
createMap
()
{
final
Map
<
String
,
Operation
>
map
=
new
HashMap
<
String
,
Operation
>();
for
(
NamedOperationsEntry
entry
:
this
.
entries
)
{
map
.
put
(
entry
.
getKey
(),
entry
.
getValue
());
}
return
map
;
}
public
List
<
NamedOperationsEntry
>
getEntries
()
{
return
entries
;
}
public
void
setEntries
(
List
<
NamedOperationsEntry
>
entries
)
{
this
.
entries
=
entries
;
}
}
src/main/java/es/uvigo/esei/dai/ws/calculator/adapters/NamedOperationsMapAdapter.java
0 → 100644
View file @
ec32b948
package
es
.
uvigo
.
esei
.
dai
.
ws
.
calculator
.
adapters
;
import
java.util.Map
;
import
es.uvigo.esei.dai.ws.calculator.Operation
;
import
jakarta.xml.bind.annotation.adapters.XmlAdapter
;
public
class
NamedOperationsMapAdapter
extends
XmlAdapter
<
NamedOperationsMap
,
Map
<
String
,
Operation
>>
{
@Override
public
Map
<
String
,
Operation
>
unmarshal
(
NamedOperationsMap
v
)
{
return
v
.
createMap
();
}
@Override
public
NamedOperationsMap
marshal
(
Map
<
String
,
Operation
>
v
)
{
return
new
NamedOperationsMap
(
v
);
}
}
src/main/java/es/uvigo/esei/dai/ws/calculator/adapters/NamedResults.java
0 → 100644
View file @
ec32b948
package
es
.
uvigo
.
esei
.
dai
.
ws
.
calculator
.
adapters
;
import
java.util.HashMap
;
import
java.util.Map
;
import
jakarta.xml.bind.annotation.XmlRootElement
;
import
jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter
;
@XmlRootElement
public
class
NamedResults
{
private
Map
<
String
,
Double
>
results
;
public
NamedResults
()
{
this
.
results
=
new
HashMap
<
String
,
Double
>();
}
public
NamedResults
(
Map
<
String
,
Double
>
results
)
{
this
.
results
=
results
;
}
@XmlJavaTypeAdapter
(
NamedResultsMapAdapter
.
class
)
public
Map
<
String
,
Double
>
getResults
()
{
return
results
;
}
public
void
setResults
(
Map
<
String
,
Double
>
results
)
{
this
.
results
=
results
;
}
}
src/main/java/es/uvigo/esei/dai/ws/calculator/adapters/NamedResultsEntry.java
0 → 100644
View file @
ec32b948
package
es
.
uvigo
.
esei
.
dai
.
ws
.
calculator
.
adapters
;
import
java.util.Map
;
public
class
NamedResultsEntry
{
private
String
key
;
private
Double
value
;
public
NamedResultsEntry
()
{
}
public
NamedResultsEntry
(
String
key
,
Double
value
)
{
this
.
key
=
key
;
this
.
value
=
value
;
}
public
NamedResultsEntry
(
Map
.
Entry
<
String
,
Double
>
entry
)
{
this
(
entry
.
getKey
(),
entry
.
getValue
());
}
public
String
getKey
()
{
return
key
;
}
public
void
setKey
(
String
key
)
{
this
.
key
=
key
;
}
public
Double
getValue
()
{
return
value
;
}
public
void
setValue
(
Double
value
)
{
this
.
value
=
value
;
}
}
src/main/java/es/uvigo/esei/dai/ws/calculator/adapters/NamedResultsMap.java
0 → 100644
View file @
ec32b948
package
es
.
uvigo
.
esei
.
dai
.
ws
.
calculator
.
adapters
;
import
java.util.HashMap
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.Map
;
public
class
NamedResultsMap
{
private
List
<
NamedResultsEntry
>
entries
;
public
NamedResultsMap
()
{
this
.
entries
=
new
LinkedList
<
NamedResultsEntry
>();
}
public
NamedResultsMap
(
Map
<
String
,
Double
>
map
)
{
this
();
for
(
Map
.
Entry
<
String
,
Double
>
entry
:
map
.
entrySet
())
{
this
.
entries
.
add
(
new
NamedResultsEntry
(
entry
));
}
}
public
Map
<
String
,
Double
>
createMap
()
{
final
Map
<
String
,
Double
>
map
=
new
HashMap
<
String
,
Double
>();
for
(
NamedResultsEntry
entry
:
this
.
entries
)
{
map
.
put
(
entry
.
getKey
(),
entry
.
getValue
());
}
return
map
;
}
public
List
<
NamedResultsEntry
>
getEntries
()
{
return
entries
;
}
public
void
setEntries
(
List
<
NamedResultsEntry
>
entries
)
{
this
.
entries
=
entries
;
}
}
src/main/java/es/uvigo/esei/dai/ws/calculator/adapters/NamedResultsMapAdapter.java
0 → 100644
View file @
ec32b948
package
es
.
uvigo
.
esei
.
dai
.
ws
.
calculator
.
adapters
;
import
java.util.Map
;
import
jakarta.xml.bind.annotation.adapters.XmlAdapter
;
public
class
NamedResultsMapAdapter
extends
XmlAdapter
<
NamedResultsMap
,
Map
<
String
,
Double
>>
{
@Override
public
Map
<
String
,
Double
>
unmarshal
(
NamedResultsMap
v
)
{
return
v
.
createMap
();
}
@Override
public
NamedResultsMap
marshal
(
Map
<
String
,
Double
>
v
)
{
return
new
NamedResultsMap
(
v
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment