Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
alx_20230801
Overview
Overview
Details
Activity
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
Patryk Czarnik
alx_20230801
Commits
7c7a030b
You need to sign in or sign up before continuing.
Commit
7c7a030b
authored
Aug 04, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ValidationHandler
parent
c8182d1d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
155 additions
and
0 deletions
+155
-0
ValidationHandler.java
...main/java/ogloszenia/soap/handlers/ValidationHandler.java
+84
-0
Walidacja.java
...wer/src/main/java/ogloszenia/soap/handlers/Walidacja.java
+67
-0
handlers.xml
...niaSerwer/src/main/resources/ogloszenia/soap/handlers.xml
+4
-0
No files found.
OgloszeniaSerwer/src/main/java/ogloszenia/soap/handlers/ValidationHandler.java
0 → 100644
View file @
7c7a030b
package
ogloszenia
.
soap
.
handlers
;
import
java.io.InputStream
;
import
java.util.Collections
;
import
java.util.Set
;
import
jakarta.servlet.ServletContext
;
import
javax.xml.namespace.QName
;
import
javax.xml.transform.Source
;
import
javax.xml.transform.stream.StreamSource
;
import
org.w3c.dom.Node
;
import
jakarta.xml.soap.Name
;
import
jakarta.xml.soap.SOAPBody
;
import
jakarta.xml.soap.SOAPConstants
;
import
jakarta.xml.soap.SOAPFactory
;
import
jakarta.xml.soap.SOAPFault
;
import
jakarta.xml.soap.SOAPMessage
;
import
jakarta.xml.ws.handler.MessageContext
;
import
jakarta.xml.ws.handler.soap.SOAPHandler
;
import
jakarta.xml.ws.handler.soap.SOAPMessageContext
;
/*
* Ten Handler dokonuje walidacji względem XML Schema i dopisuje do odpowiedzi nagłówek czas
*/
public
class
ValidationHandler
implements
SOAPHandler
<
SOAPMessageContext
>
{
@Override
public
boolean
handleMessage
(
SOAPMessageContext
context
)
{
boolean
wychodzaca
=
(
Boolean
)
context
.
get
(
SOAPMessageContext
.
MESSAGE_OUTBOUND_PROPERTY
);
try
{
SOAPMessage
message
=
context
.
getMessage
();
// walidacja wzgl. schemy
SOAPBody
soapBody
=
message
.
getSOAPBody
();
Node
elementZapytania
=
null
;
for
(
Node
nd
=
soapBody
.
getFirstChild
();
nd
!=
null
;
nd
=
nd
.
getNextSibling
())
{
if
(
nd
.
getNodeType
()
==
Node
.
ELEMENT_NODE
)
{
elementZapytania
=
nd
;
}
}
if
(
elementZapytania
!=
null
)
{
ServletContext
servletContext
=
(
ServletContext
)
context
.
get
(
SOAPMessageContext
.
SERVLET_CONTEXT
);
try
(
InputStream
input
=
servletContext
.
getResourceAsStream
(
"/WEB-INF/wsdl/OgloszeniaService_schema1.xsd"
))
{
Source
xsdSource
=
new
StreamSource
(
input
);
Walidacja
walidacja
=
new
Walidacja
(
xsdSource
);
walidacja
.
waliduj
(
elementZapytania
);
if
(
walidacja
.
bylyBledy
())
{
System
.
out
.
println
(
"Błędy walidacji "
+
walidacja
.
getBledy
());
String
winny
=
wychodzaca
?
"Server"
:
"Client"
;
soapBody
.
removeChild
(
elementZapytania
);
Name
qname
=
SOAPFactory
.
newInstance
().
createName
(
winny
,
null
,
SOAPConstants
.
URI_NS_SOAP_ENVELOPE
);
SOAPFault
fault
=
soapBody
.
addFault
();
fault
.
setFaultCode
(
qname
);
fault
.
setFaultString
(
"Błędy walidacji po stronie "
+
winny
+
": "
+
walidacja
.
getBledy
());
return
false
;
}
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
true
;
}
@Override
public
boolean
handleFault
(
SOAPMessageContext
context
)
{
return
false
;
}
@Override
public
void
close
(
MessageContext
context
)
{
}
@Override
public
Set
<
QName
>
getHeaders
()
{
return
Collections
.
emptySet
();
}
}
OgloszeniaSerwer/src/main/java/ogloszenia/soap/handlers/Walidacja.java
0 → 100644
View file @
7c7a030b
package
ogloszenia
.
soap
.
handlers
;
import
java.io.IOException
;
import
java.util.LinkedList
;
import
java.util.List
;
import
javax.xml.XMLConstants
;
import
javax.xml.transform.Source
;
import
javax.xml.transform.dom.DOMSource
;
import
javax.xml.validation.Schema
;
import
javax.xml.validation.SchemaFactory
;
import
javax.xml.validation.Validator
;
import
org.w3c.dom.Node
;
import
org.xml.sax.ErrorHandler
;
import
org.xml.sax.SAXException
;
import
org.xml.sax.SAXParseException
;
class
Walidacja
{
private
Source
schemaSource
;
private
boolean
bylyBledy
=
false
;
private
List
<
String
>
bledy
=
new
LinkedList
<>();
public
Walidacja
(
Source
schemaSource
)
{
this
.
schemaSource
=
schemaSource
;
}
public
void
waliduj
(
Node
dokument
)
{
Source
src
=
new
DOMSource
(
dokument
);
try
{
SchemaFactory
sf
=
SchemaFactory
.
newInstance
(
XMLConstants
.
W3C_XML_SCHEMA_NS_URI
);
Schema
schema
=
sf
.
newSchema
(
schemaSource
);
Validator
validator
=
schema
.
newValidator
();
validator
.
setErrorHandler
(
new
WalidacjaErrorHandler
());
validator
.
validate
(
src
);
}
catch
(
SAXException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
public
boolean
bylyBledy
()
{
return
bylyBledy
;
}
public
List
<
String
>
getBledy
()
{
return
bledy
;
}
private
class
WalidacjaErrorHandler
implements
ErrorHandler
{
public
void
warning
(
SAXParseException
e
)
throws
SAXException
{
bledy
.
add
(
e
.
getMessage
());
}
public
void
error
(
SAXParseException
e
)
throws
SAXException
{
bylyBledy
=
true
;
bledy
.
add
(
e
.
getMessage
());
}
public
void
fatalError
(
SAXParseException
e
)
throws
SAXException
{
bylyBledy
=
true
;
bledy
.
add
(
e
.
getMessage
());
}
}
}
OgloszeniaSerwer/src/main/resources/ogloszenia/soap/handlers.xml
View file @
7c7a030b
...
...
@@ -5,6 +5,10 @@
<handler-class>
ogloszenia.soap.handlers.InfoHandler
</handler-class>
</handler>
<handler>
<handler-name>
Walidacja
</handler-name>
<handler-class>
ogloszenia.soap.handlers.ValidationHandler
</handler-class>
</handler>
<handler>
<handler-name>
Password
</handler-name>
<handler-class>
ogloszenia.soap.handlers.PasswordHandler
</handler-class>
</handler>
...
...
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