@@ -3,13 +3,18 @@ package app.softnetwork.resource.service
3
3
import akka .http .scaladsl .server .Route
4
4
import akka .stream .scaladsl .{FileIO , Source }
5
5
import akka .util .ByteString
6
- import app .softnetwork .api .server .ApiEndpoint
6
+ import app .softnetwork .api .server .{ ApiEndpoint , ApiErrors }
7
7
import app .softnetwork .resource .config .ResourceSettings
8
8
import app .softnetwork .resource .handlers .GenericResourceHandler
9
9
import app .softnetwork .resource .message .ResourceMessages ._
10
10
import app .softnetwork .resource .spi .{ResourceProvider , SimpleResource }
11
11
import app .softnetwork .session .service .SessionEndpoints
12
- import org .json4s .Formats
12
+ import com .softwaremill .session .{
13
+ GetSessionTransport ,
14
+ SetSessionTransport ,
15
+ TapirCsrfCheckMode ,
16
+ TapirSessionContinuity
17
+ }
13
18
import org .softnetwork .session .model .Session
14
19
import sttp .capabilities .akka .AkkaStreams
15
20
import sttp .model .headers .CookieValueWithMeta
@@ -28,50 +33,46 @@ trait ResourceServiceEndpoints extends LoadResourceService with ApiEndpoint {
28
33
29
34
import app .softnetwork .serialization ._
30
35
31
- implicit def formats : Formats = commonFormats
32
-
33
36
def sessionEndpoints : SessionEndpoints
34
37
38
+ def sc : TapirSessionContinuity [Session ] = sessionEndpoints.sc
39
+
40
+ def st : SetSessionTransport = sessionEndpoints.st
41
+
42
+ def gt : GetSessionTransport = sessionEndpoints.gt
43
+
44
+ def checkMode : TapirCsrfCheckMode [Session ] = sessionEndpoints.checkMode
45
+
46
+ def error (e : ResourceError ): ApiErrors .ErrorInfo =
47
+ e match {
48
+ case ResourceNotFound => ApiErrors .NotFound (ResourceNotFound )
49
+ case _ => ApiErrors .BadRequest (e.message)
50
+ }
51
+
35
52
def rootEndpoint : PartialServerEndpoint [
36
- (Seq [Option [String ]], Method , Option [String ], Option [String ]),
53
+ (Seq [Option [String ]], Option [String ], Method , Option [String ]),
37
54
((Seq [Option [String ]], Option [CookieValueWithMeta ]), Session ),
38
55
Unit ,
39
- ResourceError ,
56
+ ApiErrors . ErrorInfo ,
40
57
(Seq [Option [String ]], Option [CookieValueWithMeta ]),
41
58
Any ,
42
59
Future
43
- ] =
44
- sessionEndpoints.antiCsrfWithRequiredSession.endpoint
60
+ ] = {
61
+ val partial =
62
+ sessionEndpoints.antiCsrfWithRequiredSession(sc, gt, checkMode)
63
+ partial.endpoint
45
64
.in(ResourceSettings .ResourcePath )
46
- .out(sessionEndpoints.antiCsrfWithRequiredSession.securityOutput)
47
- .errorOut(
48
- oneOf[ResourceError ](
49
- oneOfVariant[ResourceNotFound .type ](
50
- statusCode(StatusCode .NotFound )
51
- .and(emptyOutputAs(ResourceNotFound ).description(" Resource not found" ))
52
- ),
53
- oneOfVariant[UnauthorizedError .type ](
54
- statusCode(StatusCode .Unauthorized )
55
- .and(emptyOutputAs(UnauthorizedError ).description(" Unauthorized" ))
56
- )
57
- )
58
- )
65
+ .out(partial.securityOutput)
66
+ .errorOut(errors)
59
67
.serverSecurityLogic { inputs =>
60
- sessionEndpoints.antiCsrfWithRequiredSession .securityLogic(new FutureMonad ())(inputs).map {
61
- case Left (_) => Left (UnauthorizedError )
68
+ partial .securityLogic(new FutureMonad ())(inputs).map {
69
+ case Left (_) => Left (ApiErrors . Unauthorized ( " Unauthorized " ) )
62
70
case Right (r) => Right ((r._1, r._2))
63
71
}
64
72
}
73
+ }
65
74
66
- val libraryEndpoint : Full [
67
- (Seq [Option [String ]], Method , Option [String ], Option [String ]),
68
- ((Seq [Option [String ]], Option [CookieValueWithMeta ]), Session ),
69
- List [String ],
70
- ResourceError ,
71
- (Seq [Option [String ]], Option [CookieValueWithMeta ], List [SimpleResource ]),
72
- Any ,
73
- Future
74
- ] =
75
+ val libraryEndpoint : ServerEndpoint [Any with AkkaStreams , Future ] =
75
76
rootEndpoint.get
76
77
.in(" library" )
77
78
.in(paths)
@@ -86,40 +87,24 @@ trait ResourceServiceEndpoints extends LoadResourceService with ApiEndpoint {
86
87
def loadResourceBusinessLogic (
87
88
principal : ((Seq [Option [String ]], Option [CookieValueWithMeta ]), Session )
88
89
): List [String ] => Either [
89
- ResourceError ,
90
+ ApiErrors . ErrorInfo ,
90
91
(Seq [Option [String ]], Option [CookieValueWithMeta ], Source [ByteString , Any ])
91
92
] =
92
93
segments =>
93
94
loadResource(segments) match {
94
95
case Some ((path, _)) => Right ((principal._1._1, principal._1._2, FileIO .fromPath(path)))
95
- case _ => Left (ResourceNotFound )
96
+ case _ => Left (error( ResourceNotFound ) )
96
97
}
97
98
98
- val getResourceEndpoint : Full [
99
- (Seq [Option [String ]], Method , Option [String ], Option [String ]),
100
- ((Seq [Option [String ]], Option [CookieValueWithMeta ]), Session ),
101
- List [String ],
102
- ResourceError ,
103
- (Seq [Option [String ]], Option [CookieValueWithMeta ], Source [ByteString , Any ]),
104
- Any with AkkaStreams ,
105
- Future
106
- ] =
99
+ val getResourceEndpoint : ServerEndpoint [Any with AkkaStreams , Future ] =
107
100
rootEndpoint.get
108
101
.in(paths)
109
102
.out(streamBinaryBody(AkkaStreams )(CodecFormat .OctetStream ()))
110
103
.serverLogic(principal =>
111
104
segments => Future .successful(loadResourceBusinessLogic(principal)(segments))
112
105
)
113
106
114
- val getImageEndpoint : Full [
115
- (Seq [Option [String ]], Method , Option [String ], Option [String ]),
116
- ((Seq [Option [String ]], Option [CookieValueWithMeta ]), Session ),
117
- List [String ],
118
- ResourceError ,
119
- (Seq [Option [String ]], Option [CookieValueWithMeta ], Source [ByteString , Any ]),
120
- Any with AkkaStreams ,
121
- Future
122
- ] =
107
+ val getImageEndpoint : ServerEndpoint [Any with AkkaStreams , Future ] =
123
108
rootEndpoint.get
124
109
.in(" images" )
125
110
.in(paths)
@@ -131,12 +116,12 @@ trait ResourceServiceEndpoints extends LoadResourceService with ApiEndpoint {
131
116
)
132
117
133
118
val uploadResourceEndpoint : PartialServerEndpoint [
134
- (Seq [Option [String ]], Method , Option [String ], Option [String ]),
119
+ (Seq [Option [String ]], Option [String ], Method , Option [String ]),
135
120
((Seq [Option [String ]], Option [CookieValueWithMeta ]), Session ),
136
121
(List [String ], UploadResource ),
137
- ResourceError ,
122
+ ApiErrors . ErrorInfo ,
138
123
(Seq [Option [String ]], Option [CookieValueWithMeta ], ResourceResult ),
139
- Any ,
124
+ Any with AkkaStreams ,
140
125
Future
141
126
] =
142
127
rootEndpoint
@@ -155,47 +140,31 @@ trait ResourceServiceEndpoints extends LoadResourceService with ApiEndpoint {
155
140
)
156
141
)
157
142
158
- val addResourceEndpoint : Full [
159
- (Seq [Option [String ]], Method , Option [String ], Option [String ]),
160
- ((Seq [Option [String ]], Option [CookieValueWithMeta ]), Session ),
161
- (List [String ], UploadResource ),
162
- ResourceError ,
163
- (Seq [Option [String ]], Option [CookieValueWithMeta ], ResourceResult ),
164
- Any ,
165
- Future
166
- ] =
143
+ val addResourceEndpoint : ServerEndpoint [Any with AkkaStreams , Future ] =
167
144
uploadResourceEndpoint.post
168
145
.description(" Add a resource" )
169
146
.serverLogic(principal => { case (segments, upload) =>
170
147
uploadResource(principal._2, segments, upload.bytes, update = false ) map {
171
- case Left (l) => Left (l )
148
+ case Left (l) => Left (error(l) )
172
149
case Right (r) => Right ((principal._1._1, principal._1._2, r))
173
150
}
174
151
})
175
152
176
- val updateResourceEndpoint : Full [
177
- (Seq [Option [String ]], Method , Option [String ], Option [String ]),
178
- ((Seq [Option [String ]], Option [CookieValueWithMeta ]), Session ),
179
- (List [String ], UploadResource ),
180
- ResourceError ,
181
- (Seq [Option [String ]], Option [CookieValueWithMeta ], ResourceResult ),
182
- Any ,
183
- Future
184
- ] =
153
+ val updateResourceEndpoint : ServerEndpoint [Any with AkkaStreams , Future ] =
185
154
uploadResourceEndpoint.put
186
155
.description(" Update the resource" )
187
156
.serverLogic(principal => { case (segments, upload) =>
188
157
uploadResource(principal._2, segments, upload.bytes, update = true ) map {
189
- case Left (l) => Left (l )
158
+ case Left (l) => Left (error(l) )
190
159
case Right (r) => Right ((principal._1._1, principal._1._2, r))
191
160
}
192
161
})
193
162
194
163
val uploadImageEndpoint : PartialServerEndpoint [
195
- (Seq [Option [String ]], Method , Option [String ], Option [String ]),
164
+ (Seq [Option [String ]], Option [String ], Method , Option [String ]),
196
165
((Seq [Option [String ]], Option [CookieValueWithMeta ]), Session ),
197
166
(List [String ], UploadImage ),
198
- ResourceError ,
167
+ ApiErrors . ErrorInfo ,
199
168
(Seq [Option [String ]], Option [CookieValueWithMeta ], ResourceResult ),
200
169
Any ,
201
170
Future
@@ -217,38 +186,22 @@ trait ResourceServiceEndpoints extends LoadResourceService with ApiEndpoint {
217
186
)
218
187
)
219
188
220
- val addImageEndpoint : Full [
221
- (Seq [Option [String ]], Method , Option [String ], Option [String ]),
222
- ((Seq [Option [String ]], Option [CookieValueWithMeta ]), Session ),
223
- (List [String ], UploadImage ),
224
- ResourceError ,
225
- (Seq [Option [String ]], Option [CookieValueWithMeta ], ResourceResult ),
226
- Any ,
227
- Future
228
- ] =
189
+ val addImageEndpoint : ServerEndpoint [Any with AkkaStreams , Future ] =
229
190
uploadImageEndpoint.post
230
191
.description(" Add an image" )
231
192
.serverLogic(principal => { case (segments, upload) =>
232
193
uploadResource(principal._2, segments, upload.bytes, update = false ) map {
233
- case Left (l) => Left (l )
194
+ case Left (l) => Left (error(l) )
234
195
case Right (r) => Right ((principal._1._1, principal._1._2, r))
235
196
}
236
197
})
237
198
238
- val updateImageEndpoint : Full [
239
- (Seq [Option [String ]], Method , Option [String ], Option [String ]),
240
- ((Seq [Option [String ]], Option [CookieValueWithMeta ]), Session ),
241
- (List [String ], UploadImage ),
242
- ResourceError ,
243
- (Seq [Option [String ]], Option [CookieValueWithMeta ], ResourceResult ),
244
- Any ,
245
- Future
246
- ] =
199
+ val updateImageEndpoint : ServerEndpoint [Any with AkkaStreams , Future ] =
247
200
uploadImageEndpoint.put
248
201
.description(" Update the image" )
249
202
.serverLogic(principal => { case (segments, upload) =>
250
203
uploadResource(principal._2, segments, upload.bytes, update = true ) map {
251
- case Left (l) => Left (l )
204
+ case Left (l) => Left (error(l) )
252
205
case Right (r) => Right ((principal._1._1, principal._1._2, r))
253
206
}
254
207
})
@@ -264,43 +217,27 @@ trait ResourceServiceEndpoints extends LoadResourceService with ApiEndpoint {
264
217
}
265
218
}
266
219
267
- val deleteResourceEndpoint : Full [
268
- (Seq [Option [String ]], Method , Option [String ], Option [String ]),
269
- ((Seq [Option [String ]], Option [CookieValueWithMeta ]), Session ),
270
- List [String ],
271
- ResourceError ,
272
- (Seq [Option [String ]], Option [CookieValueWithMeta ]),
273
- Any ,
274
- Future
275
- ] =
220
+ val deleteResourceEndpoint : ServerEndpoint [Any with AkkaStreams , Future ] =
276
221
rootEndpoint
277
222
.in(paths)
278
223
.delete
279
224
.serverLogic(principal =>
280
225
segments =>
281
226
deleteResourceBusinessLogic(principal._2, segments) map {
282
- case Left (l) => Left (l )
227
+ case Left (l) => Left (error(l) )
283
228
case Right (_) => Right ((principal._1._1, principal._1._2))
284
229
}
285
230
)
286
231
287
- val deleteImageEndpoint : Full [
288
- (Seq [Option [String ]], Method , Option [String ], Option [String ]),
289
- ((Seq [Option [String ]], Option [CookieValueWithMeta ]), Session ),
290
- List [String ],
291
- ResourceError ,
292
- (Seq [Option [String ]], Option [CookieValueWithMeta ]),
293
- Any ,
294
- Future
295
- ] =
232
+ val deleteImageEndpoint : ServerEndpoint [Any with AkkaStreams , Future ] =
296
233
rootEndpoint
297
234
.in(" images" )
298
235
.in(paths)
299
236
.delete
300
237
.serverLogic(principal =>
301
238
segments =>
302
239
deleteResourceBusinessLogic(principal._2, segments) map {
303
- case Left (l) => Left (l )
240
+ case Left (l) => Left (error(l) )
304
241
case Right (_) => Right ((principal._1._1, principal._1._2))
305
242
}
306
243
)
0 commit comments