Commit 690643b0 authored by abdullh.alsoleman's avatar abdullh.alsoleman

REST_gRPC

parent 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();
}
};
}
}
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);
}
}
}
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() {
};
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment