Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
alx_java2b_20250412
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_java2b_20250412
Commits
ab4ad38c
Commit
ab4ad38c
authored
May 11, 2025
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PdfWriter - tylko lista produktów
parent
1375999f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
127 additions
and
0 deletions
+127
-0
pom.xml
PC26-RestSerwer/pom.xml
+1
-0
RProductPDF.java
PC26-RestSerwer/src/main/java/sklep/rest/RProductPDF.java
+34
-0
ObslugaXSL.java
PC26-RestSerwer/src/main/java/sklep/rest/ext/ObslugaXSL.java
+53
-0
PdfWriter.java
PC26-RestSerwer/src/main/java/sklep/rest/ext/PdfWriter.java
+39
-0
No files found.
PC26-RestSerwer/pom.xml
View file @
ab4ad38c
...
@@ -35,6 +35,7 @@
...
@@ -35,6 +35,7 @@
<groupId>
org.apache.xmlgraphics
</groupId>
<groupId>
org.apache.xmlgraphics
</groupId>
<artifactId>
fop
</artifactId>
<artifactId>
fop
</artifactId>
<version>
2.11
</version>
<version>
2.11
</version>
</dependency>
</dependency>
</dependencies>
</dependencies>
...
...
PC26-RestSerwer/src/main/java/sklep/rest/RProductPDF.java
0 → 100644
View file @
ab4ad38c
package
sklep
.
rest
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.PathParam
;
import
jakarta.ws.rs.Produces
;
import
sklep.db.DBConnection
;
import
sklep.db.DBException
;
import
sklep.db.ProductDAO
;
import
sklep.db.RecordNotFound
;
import
sklep.model.Product
;
import
sklep.model.ProductList
;
@Path
(
"/products.pdf"
)
@Produces
(
"application/pdf"
)
public
class
RProductPDF
{
@GET
public
ProductList
allProducts
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
return
new
ProductList
(
productDAO
.
readAll
());
}
}
@GET
@Path
(
"/{id}"
)
// przykładowo /products.pdf/3
public
Product
oneProduct
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
return
productDAO
.
findById
(
productId
);
}
}
}
PC26-RestSerwer/src/main/java/sklep/rest/ext/ObslugaXSL.java
0 → 100644
View file @
ab4ad38c
package
sklep
.
rest
.
ext
;
import
java.io.BufferedOutputStream
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.net.URI
;
import
jakarta.servlet.ServletContext
;
import
jakarta.ws.rs.WebApplicationException
;
import
jakarta.xml.bind.JAXBContext
;
import
jakarta.xml.bind.util.JAXBSource
;
import
javax.xml.transform.Result
;
import
javax.xml.transform.Transformer
;
import
javax.xml.transform.TransformerFactory
;
import
javax.xml.transform.sax.SAXResult
;
import
javax.xml.transform.stream.StreamSource
;
import
org.apache.fop.apps.Fop
;
import
org.apache.fop.apps.FopFactory
;
import
org.apache.xmlgraphics.util.MimeConstants
;
public
class
ObslugaXSL
{
private
ServletContext
servletContext
;
public
ObslugaXSL
(
ServletContext
servletContext
)
{
this
.
servletContext
=
servletContext
;
}
// obiekt -(za pomocą JAXB)-> XML -(za pomocą transformera)-> XML(XSL-FO)
// --(za pomocą Apache FOP)-> PDF -> output
public
void
wypiszPDF
(
Object
obj
,
OutputStream
output
)
{
try
(
InputStream
configStream
=
servletContext
.
getResourceAsStream
(
"WEB-INF/fop-conf.xml"
))
{
JAXBContext
ctx
=
JAXBContext
.
newInstance
(
obj
.
getClass
());
JAXBSource
src
=
new
JAXBSource
(
ctx
,
obj
);
FopFactory
fopFactory
=
FopFactory
.
newInstance
(
new
URI
(
""
),
configStream
);
try
(
BufferedOutputStream
pdfOut
=
new
BufferedOutputStream
(
output
))
{
Fop
fop
=
fopFactory
.
newFop
(
MimeConstants
.
MIME_PDF
,
pdfOut
);
TransformerFactory
tf
=
TransformerFactory
.
newInstance
();
StreamSource
xsl
=
new
StreamSource
(
servletContext
.
getResourceAsStream
(
"WEB-INF/sklep-fo.xsl"
));
Transformer
tr
=
tf
.
newTransformer
(
xsl
);
Result
res
=
new
SAXResult
(
fop
.
getDefaultHandler
());
tr
.
transform
(
src
,
res
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
throw
new
WebApplicationException
(
"Problem FOP "
+
e
.
getMessage
(),
e
,
500
);
}
}
}
PC26-RestSerwer/src/main/java/sklep/rest/ext/PdfWriter.java
0 → 100644
View file @
ab4ad38c
package
sklep
.
rest
.
ext
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.lang.annotation.Annotation
;
import
java.lang.reflect.Type
;
import
jakarta.servlet.ServletContext
;
import
jakarta.ws.rs.WebApplicationException
;
import
jakarta.ws.rs.core.Context
;
import
jakarta.ws.rs.core.MediaType
;
import
jakarta.ws.rs.core.MultivaluedMap
;
import
jakarta.ws.rs.ext.MessageBodyWriter
;
import
jakarta.ws.rs.ext.Provider
;
import
sklep.model.ProductList
;
@Provider
public
class
PdfWriter
implements
MessageBodyWriter
<
ProductList
>
{
private
static
final
MediaType
PDF_TYPE
=
new
MediaType
(
"application"
,
"pdf"
);
@Context
private
ServletContext
servletContext
;
@Override
public
boolean
isWriteable
(
Class
<?>
type
,
Type
genericType
,
Annotation
[]
annotations
,
MediaType
mediaType
)
{
return
(
type
==
ProductList
.
class
)
&&
PDF_TYPE
.
isCompatible
(
mediaType
);
}
@Override
public
void
writeTo
(
ProductList
obj
,
Class
<?>
type
,
Type
genericType
,
Annotation
[]
annotations
,
MediaType
mediaType
,
MultivaluedMap
<
String
,
Object
>
httpHeaders
,
OutputStream
output
)
throws
IOException
,
WebApplicationException
{
ObslugaXSL
obslugaXSL
=
new
ObslugaXSL
(
servletContext
);
obslugaXSL
.
wypiszPDF
(
obj
,
output
);
}
}
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