Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
U
UploadingFile
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
abdullh.alsoleman
UploadingFile
Commits
690643b0
Commit
690643b0
authored
Nov 23, 2023
by
abdullh.alsoleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
REST_gRPC
parent
2462e153
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
0 additions
and
124 deletions
+0
-124
UploadFileService.java
...File_Server/src/main/java/Services/UploadFileService.java
+0
-75
FileStorage.java
UploadingFile_Server/src/main/java/Store/FileStorage.java
+0
-18
UploadInterceptor.java
...e_Server/src/main/java/interceptor/UploadInterceptor.java
+0
-31
No files found.
UploadingFile_Server/src/main/java/Services/UploadFileService.java
deleted
100644 → 0
View file @
2462e153
package
Services
;
import
Store.FileStorage
;
import
Uploading_Service.*
;
import
io.grpc.Context
;
import
io.grpc.Status
;
import
io.grpc.stub.StreamObserver
;
import
net.devh.boot.grpc.server.service.GrpcService
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
@GrpcService
public
class
UploadFileService
extends
uploadingServiceGrpc
.
uploadingServiceImplBase
{
@Override
public
StreamObserver
<
FileUploadRequest
>
uploadFile
(
StreamObserver
<
FileUploadResponse
>
responseObserver
)
{
Context
.
Key
<
FileMetadata
>
fileMetaContext
=
Context
.
key
(
"file-meta"
);
FileMetadata
fileMetadata
=
fileMetaContext
.
get
();
ByteArrayOutputStream
byteArrayOutputStream
=
new
ByteArrayOutputStream
();
FileStorage
fileStorage
=
new
FileStorage
();
return
new
StreamObserver
<
FileUploadRequest
>()
{
@Override
//Called when client sends data
public
void
onNext
(
FileUploadRequest
fileUploadRequest
)
{
try
{
fileUploadRequest
.
getFile
().
getContent
()
.
writeTo
(
byteArrayOutputStream
);
}
catch
(
IOException
e
)
{
responseObserver
.
onError
(
io
.
grpc
.
Status
.
INTERNAL
.
withDescription
(
"a problem occur in writing data: "
+
e
.
getMessage
())
.
asRuntimeException
());
}
}
@Override
//Called when client sends error
public
void
onError
(
Throwable
throwable
)
{
System
.
out
.
println
(
throwable
.
toString
());
}
@Override
//Called when client finish sending data
public
void
onCompleted
()
{
int
allReceivedBytes
=
byteArrayOutputStream
.
size
();
try
{
if
(
allReceivedBytes
==
fileMetadata
.
getContentLength
())
{
fileStorage
.
write
(
fileMetadata
.
getFileNameWithType
(),
byteArrayOutputStream
);
byteArrayOutputStream
.
close
();
}
else
{
responseObserver
.
onError
(
Status
.
FAILED_PRECONDITION
.
withDescription
(
String
.
format
(
"there is an different between expected bytes %d and received %d "
,
fileMetadata
.
getContentLength
(),
allReceivedBytes
))
.
asRuntimeException
());
return
;
}
}
catch
(
IOException
e
)
{
responseObserver
.
onError
(
io
.
grpc
.
Status
.
INTERNAL
.
withDescription
(
"There is a problem in saving data: "
+
e
.
getMessage
())
.
asRuntimeException
());
return
;
}
responseObserver
.
onNext
(
FileUploadResponse
.
newBuilder
()
.
setFileName
(
fileMetadata
.
getFileNameWithType
())
.
setUploadStatus
(
UploadStatus
.
SUCCESS
)
.
build
()
);
responseObserver
.
onCompleted
();
}
};
}
}
UploadingFile_Server/src/main/java/Store/FileStorage.java
deleted
100644 → 0
View file @
2462e153
package
Store
;
import
java.io.BufferedOutputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
public
class
FileStorage
{
private
final
String
DEFAULT_PATH
=
"output/"
;
public
void
write
(
String
fileNameWithType
,
ByteArrayOutputStream
byteArrayOutputStream
)
throws
IOException
{
String
filePath
=
DEFAULT_PATH
+
fileNameWithType
;
try
(
BufferedOutputStream
bufferedOutputStream
=
new
BufferedOutputStream
(
new
FileOutputStream
(
filePath
)))
{
byteArrayOutputStream
.
writeTo
(
bufferedOutputStream
);
}
}
}
UploadingFile_Server/src/main/java/interceptor/UploadInterceptor.java
deleted
100644 → 0
View file @
2462e153
package
interceptor
;
import
Uploading_Service.FileMetadata
;
import
com.google.protobuf.InvalidProtocolBufferException
;
import
io.grpc.*
;
import
net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor
;
@GrpcGlobalServerInterceptor
public
class
UploadInterceptor
implements
ServerInterceptor
{
@Override
public
<
ReqT
,
RespT
>
ServerCall
.
Listener
<
ReqT
>
interceptCall
(
ServerCall
<
ReqT
,
RespT
>
serverCall
,
Metadata
metadata
,
ServerCallHandler
<
ReqT
,
RespT
>
serverCallHandler
)
{
FileMetadata
fileMetadata
=
null
;
if
(
metadata
.
containsKey
(
Metadata
.
Key
.
of
(
"file-meta-bin"
,
Metadata
.
BINARY_BYTE_MARSHALLER
)))
{
byte
[]
metaBytes
=
metadata
.
get
(
Metadata
.
Key
.
of
(
"file-meta-bin"
,
Metadata
.
BINARY_BYTE_MARSHALLER
));
try
{
fileMetadata
=
FileMetadata
.
parseFrom
(
metaBytes
);
}
catch
(
InvalidProtocolBufferException
e
)
{
Status
status
=
Status
.
INTERNAL
.
withDescription
(
"Error in creating file metadata"
);
serverCall
.
close
(
status
,
metadata
);
}
Context
.
Key
<
FileMetadata
>
fileMetaContext
=
Context
.
key
(
"file-meta"
);
Context
context
=
Context
.
current
().
withValue
(
fileMetaContext
,
fileMetadata
);
return
Contexts
.
interceptCall
(
context
,
serverCall
,
metadata
,
serverCallHandler
);
}
return
new
ServerCall
.
Listener
()
{
};
}
}
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