Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20240528-BJava
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
20240528-BJava
Commits
36e8ef77
Commit
36e8ef77
authored
Jun 28, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Więcej metod w RProducts
parent
4cd6cb08
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
115 additions
and
1 deletions
+115
-1
PhotoUtil.java
PC34-SpringJersey/src/main/java/sklep/rest/PhotoUtil.java
+65
-0
RProducts.java
PC34-SpringJersey/src/main/java/sklep/rest/RProducts.java
+48
-0
SecurityConfig.java
...ngJersey/src/main/java/sklep/security/SecurityConfig.java
+2
-1
No files found.
PC34-SpringJersey/src/main/java/sklep/rest/PhotoUtil.java
0 → 100644
View file @
36e8ef77
package
sklep
.
rest
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.http.HttpStatusCode
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.server.ResponseStatusException
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
@Component
public
class
PhotoUtil
{
private
static
final
String
EXT
=
".jpg"
;
@Value
(
"${alx.photo_dir}"
)
private
String
dir
;
public
File
getFile
(
int
productId
)
{
Path
path
=
getPath
(
productId
);
File
file
=
path
.
toFile
();
if
(
file
.
exists
())
{
return
file
;
}
else
{
throw
new
ResponseStatusException
(
HttpStatusCode
.
valueOf
(
404
),
"Brak zdjęcia nr "
+
productId
);
}
}
public
byte
[]
readBytes
(
int
productId
)
{
Path
path
=
getPath
(
productId
);
try
{
return
Files
.
readAllBytes
(
path
);
}
catch
(
IOException
e
)
{
throw
new
ResponseStatusException
(
HttpStatusCode
.
valueOf
(
404
),
"Brak zdjęcia nr "
+
productId
);
}
}
public
void
writeStream
(
int
productId
,
InputStream
inputStream
)
{
try
{
Path
path
=
getPath
(
productId
);
Files
.
copy
(
inputStream
,
path
,
StandardCopyOption
.
REPLACE_EXISTING
);
}
catch
(
Exception
e
)
{
// wypisujemy błąd, ale metoda kończy się normalnie
e
.
printStackTrace
();
}
}
private
Path
getPath
(
int
productId
)
{
String
fileName
=
productId
+
EXT
;
return
Paths
.
get
(
dir
,
fileName
);
}
public
void
writeBytes
(
int
productId
,
byte
[]
bytes
)
{
try
{
Path
path
=
getPath
(
productId
);
Files
.
write
(
path
,
bytes
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
PC34-SpringJersey/src/main/java/sklep/rest/RProducts.java
View file @
36e8ef77
package
sklep
.
rest
;
package
sklep
.
rest
;
import
jakarta.ws.rs.*
;
import
jakarta.ws.rs.*
;
import
jakarta.ws.rs.core.Response
;
import
jakarta.ws.rs.core.UriBuilder
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.dao.EmptyResultDataAccessException
;
import
org.springframework.web.bind.annotation.PutMapping
;
import
sklep.model.Product
;
import
sklep.model.Product
;
import
sklep.repository.ProductRepository
;
import
sklep.repository.ProductRepository
;
import
java.net.URI
;
import
java.util.List
;
import
java.util.List
;
@Path
(
"/products"
)
@Path
(
"/products"
)
@Produces
({
"application/json"
})
@Produces
({
"application/json"
})
@Consumes
({
"application/json"
})
public
class
RProducts
{
public
class
RProducts
{
@Autowired
@Autowired
private
ProductRepository
productRepository
;
private
ProductRepository
productRepository
;
@Autowired
private
PhotoUtil
photoUtil
;
@GET
@GET
public
List
<
Product
>
getProducts
()
{
public
List
<
Product
>
getProducts
()
{
...
@@ -24,4 +32,44 @@ public class RProducts {
...
@@ -24,4 +32,44 @@ public class RProducts {
return
productRepository
.
findById
(
id
).
orElseThrow
(()
->
new
WebApplicationException
(
404
));
return
productRepository
.
findById
(
id
).
orElseThrow
(()
->
new
WebApplicationException
(
404
));
}
}
@PUT
@Path
(
"/{id}"
)
public
void
update
(
@PathParam
(
"id"
)
Integer
productId
,
Product
product
)
{
product
.
setId
(
productId
);
productRepository
.
save
(
product
);
}
@POST
public
Response
insert
(
Product
product
)
{
// Aby mieć pewność, że zapytanie tworzy nowy rekord, ustawiam productId na null.
product
.
setId
(
null
);
productRepository
.
save
(
product
);
URI
uri
=
UriBuilder
.
fromResource
(
RProducts
.
class
).
path
(
"{id}"
).
build
(
product
.
getId
());
return
Response
.
created
(
uri
).
build
();
// odpowiedź zgodnie z zasadami HTTP informuje o tym, że powstał nowy rekord pod podanym adresem
}
@DELETE
@Path
(
"/{id}"
)
public
void
delete
(
@PathParam
(
"id"
)
Integer
productId
)
{
try
{
productRepository
.
deleteById
(
productId
);
}
catch
(
EmptyResultDataAccessException
e
)
{
throw
new
WebApplicationException
(
e
,
404
);
}
}
@GET
@Path
(
"/{id}/photo"
)
@Produces
(
"image/jpeg"
)
public
byte
[]
getPhoto
(
@PathParam
(
"id"
)
int
productId
)
{
return
photoUtil
.
readBytes
(
productId
);
}
@PUT
@Path
(
"/{id}/photo"
)
@Consumes
(
"image/jpeg"
)
public
void
uploadPhoto
(
@PathParam
(
"id"
)
int
productId
,
byte
[]
bytes
)
{
photoUtil
.
writeBytes
(
productId
,
bytes
);
}
}
}
PC34-SpringJersey/src/main/java/sklep/security/SecurityConfig.java
View file @
36e8ef77
...
@@ -19,7 +19,8 @@ public class SecurityConfig {
...
@@ -19,7 +19,8 @@ public class SecurityConfig {
.
authorizeHttpRequests
((
authz
)
->
authz
.
authorizeHttpRequests
((
authz
)
->
authz
.
anyRequest
().
permitAll
()
.
anyRequest
().
permitAll
()
)
)
.
httpBasic
(
Customizer
.
withDefaults
());
.
httpBasic
(
Customizer
.
withDefaults
())
.
csrf
(
config
->
config
.
disable
());
return
httpSecurity
.
build
();
return
httpSecurity
.
build
();
}
}
...
...
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