Spring Boot Unit Testing - java

I'm currently developing a Rest service in Spring Boot and am attempting to develop unit tests. My Rest controller takes the request and utilizes an Sftp Connector defined within another package to directly interface with our servers. I've defined my test cases to mock the behavior of the Sftp Connector rather than have it execute with the mock data being fed into the Rest endpoints. I've been getting a ton of error logs during testing showing that exceptions are being raised from the Sftp connector. Which makes sense if the mock data sent to the endpoints is still being fed into the connector class. I'm guessing that I'm not mocking the connector properly. I've been unsuccessful so far in locating a method to mock the connector. Is this possible? Or can I only mock the classes defined within the same package as the test class?
Controller:
package RestAPI;
import JSch.SFTP.SftpConnector;
import JSch.SFTP.fileInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
#RestController
#Api( value = "Test Rest Controller", description = "Basic test for SFTP
connector with very basic operations.")
public class SftpRestController {
private Properties getProperties(){ //used to get connection values from
external file
InputStream input = null;
Properties prop = new Properties(); //new property
try{
if(System.getProperty("os.name").contains("Windows")){
input = new FileInputStream("C:\\tmp\\application.properties"); //get resources stream
}
else{
input = new FileInputStream("application.properties");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
while((line = reader.readLine()) != null){
String[] split = line.split("=");
prop.put(split[0], split[1]);
}
}catch(IOException IO){
IO.printStackTrace();
}finally{
if(input != null )
try{
input.close(); //close the stream
}catch(IOException IO2){
IO2.printStackTrace();
}
}
System.out.println(prop.getProperty("sftp.port"));
return prop;
}
#ApiOperation(value = "Check that service is up and running.", response = String.class)
#RequestMapping("/")
public #ResponseBody String index(){ return "Rest Test Service is up and running";}
#ApiOperation(value = "Upload a file", response = String.class)
#PostMapping(value="/Upload") //uploads files so long as the files are defined in one structure in the form body
public #ResponseBody String multipartUpload(#RequestBody MultipartFile[] files, #RequestParam("dest") String dest){
String fileMessage = ""; //need something to send back to the caller
SftpConnector connector = new SftpConnector(this.getProperties()); //plugs properties into the constructor and opens a connection
for( int i = 0; i < files.length; i++ ){ //for each file uploaded
connector.uploadFile(files[i], dest); //call the upload method with the file reference and where it's going
fileMessage = fileMessage + files[i].getOriginalFilename() + " has been uploaded to remote directory \n";
}
connector.closeSFTPConnection(); //manually close the connection
return fileMessage;
}
#ApiOperation(value = "Downloads a file over SFTP and writes it to local file system specified", response = String.class)
#GetMapping(value="/Download")
public #ResponseBody String downloadFile(#RequestParam("fileName") String fName, #RequestParam("dest") String dest){
SftpConnector connector = new SftpConnector(this.getProperties()); //new connector
connector.downloadFile(fName, dest); //pull the specified file down
connector.closeSFTPConnection(); //close the connection
return fName + " has been downloaded to the specified local directory";
}
#ApiOperation(value = "Moves all files on a remote server from one directory to another based on file type specified", response = String.class)
#PutMapping(value="/moveFiles")
public #ResponseBody String moveFiles(#RequestParam("dir") String origin, #RequestParam("fileType") String type,#RequestParam("dest") String dest){
SftpConnector connector = new SftpConnector(this.getProperties());
connector.moveFiles(origin, type, dest); //moves a group of files based file type from one directory to another
connector.closeSFTPConnection();
return "All files have been moved.";
}
#ApiOperation(value = "Moves a single specific file", response = String.class)
#GetMapping(value="/moveFile")
public #ResponseBody String moveFile(#RequestParam("dir") String origFile, #RequestParam("dest") String dest){
SftpConnector connector = new SftpConnector(this.getProperties());
connector.moveFile(origFile, dest); //moves a single file. file name must be specified.
connector.closeSFTPConnection();
return FilenameUtils.getName(origFile) + " has been moved to " + dest;
}
#ApiOperation(value = "Gets a specified file stream and returns it as a string", response = String.class)
#GetMapping(value="/readFile")
public #ResponseBody String readFile(#RequestParam("path") String path){
String fileContents;
try{
SftpConnector connector = new SftpConnector(this.getProperties());
InputStream fis = connector.readFile(path); //gets an open file stream
fileContents = IOUtils.toString(fis,"UTF-8"); //takes a file stream, reads into variable in specified encoding
fis.close(); //closes the stream
connector.closeSFTPConnection();
}catch(IOException IO){
fileContents = IO.toString();
}
return fileContents;
}
#ApiOperation(value="Returns a list of file names as a string", response = String.class)
#GetMapping(value="/listFiles")
public #ResponseBody String fileNames(#RequestParam("dir") String origin, #RequestParam("fileType") String type){
String base = "";
try{
SftpConnector connector = new SftpConnector(this.getProperties());
Collection<fileInfo> files = connector.listFiles(origin, type); //gets a list of files with name, type, and an input stream
Iterator<fileInfo> iterator = files.iterator(); //iterator to roll over collection
while(iterator.hasNext()){ //while not empty
fileInfo thisFile = iterator.next(); //get the next fileInfo
base = base + thisFile.getName() + '\n' ; //write the name to the base string
thisFile.getFileStream().close(); //close the file stream
}
connector.closeSFTPConnection();
}catch(Exception ex){
ex.printStackTrace();
}
return base;
}
#ApiOperation(value = "Moves a file by the connectors readFile and writeFile methods rather than SFTP rename", response = String.class)
#GetMapping(value="/test")
public #ResponseBody String StreamTest(#RequestParam("file") String file, #RequestParam("dest") String dest){
SftpConnector connector = new SftpConnector(this.getProperties());
connector.writeFile(dest, FilenameUtils.getName(file), connector.readFile(file));
connector.closeSFTPConnection();
return file + " has been successfully read, and written to destination";
}
}
Sftp Connector:
package JSch.SFTP;
import com.jcraft.jsch.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.*;
#Component
public class SftpConnector {
private String sftpHost;
private String sftpUser;
private int sftpPort;
private String sftpPass;
private Session session = null;
private ChannelSftp channelSftp = null;
public String getSftpHost() {
return sftpHost;
}
public void setSftpHost(String sftpHost) {
this.sftpHost = sftpHost;
}
public String getSftpUser() {
return sftpUser;
}
public void setSftpUser(String sftpUser) {
this.sftpUser = sftpUser;
}
public int getSftpPort() {
return sftpPort;
}
public void setSftpPort(int sftpPort) {
this.sftpPort = sftpPort;
}
public String getSftpPass() {
return sftpPass;
}
public void setSftpPass(String sftpPass) {
this.sftpPass = sftpPass;
}
public void setConnectionVars(String host, String user, String password, int port){
this.setSftpHost(host);
this.setSftpPort(port);
this.setSftpUser(user);
this.setSftpPass(password);
}
public SftpConnector(Properties prop){
this.setConnectionVars(prop.getProperty("sftp.host"), prop.getProperty("sftp.user"), prop.getProperty("sftp.pass"), Integer.parseInt(prop.getProperty("sftp.port"))); //set the connection variables
this.openSFTPConnection(); //open the sftp connection
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public ChannelSftp getChannelSftp() {
return channelSftp;
}
public void setChannelSftp(ChannelSftp channelSftp) {
this.channelSftp = channelSftp;
}
public void openSFTPConnection(){
try{
JSch jsch = new JSch(); //create the new JSch var
Session baseSession = jsch.getSession(this.getSftpUser(), this.getSftpHost(),this.getSftpPort()); //establish the ssh session info
baseSession.setPassword(this.getSftpPass()); //auth over password
baseSession.setConfig("StrictHostKeyChecking","no"); // don't require hosting key check will need to change after testing
baseSession.setConfig("PreferredAuthentications","password"); //used to omit kerberos authentication
this.setSession(baseSession); //set the session to private variable
this.getSession().connect(); //open the session
ChannelSftp tempChannel = (ChannelSftp) this.getSession().openChannel("sftp"); //create an SFTP channel
this.setChannelSftp(tempChannel); //set the channel to the private variable
this.getChannelSftp().connect(); //open the SFTP connection
}catch(JSchException e){
e.printStackTrace();
}
}
public void closeSFTPConnection(){
try{
if(this.getChannelSftp().isConnected()){ //if the channel is open so must be the session close both
this.getChannelSftp().disconnect();
this.getSession().disconnect();
}else{ //if the channel is closed check to see if the session is still open
if(this.getSession().isConnected()){
this.getSession().disconnect();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public Collection<fileInfo> listFiles(String dir, String filter){
Collection<fileInfo> files = new ArrayList<fileInfo>();
try{
if(this.getChannelSftp() == null || !this.getChannelSftp().isConnected()) //make sure that the JSch sessions been opened.
this.openSFTPConnection();
this.getChannelSftp().cd(dir); //set the working directory
Vector<ChannelSftp.LsEntry> fileList = this.getChannelSftp().ls(filter); //Get a listing of the files in the directory depending on a filter
Iterator<ChannelSftp.LsEntry> iterator = fileList.iterator(); //iterate over the collection
while(iterator.hasNext()) {
ChannelSftp.LsEntry entry = iterator.next();
fileInfo thisFile = new fileInfo(entry.getFilename(), entry.getAttrs().getAtimeString(), FilenameUtils.getExtension(entry.getFilename()), this.getChannelSftp().get(entry.getFilename())); //get a buffered input stream for each file.
files.add(thisFile);
}
}catch(SftpException e){
e.printStackTrace();
}
return files;
}
public InputStream readFile(String filePath){ //built to read get a file stream from a given file path
InputStream inputStream = null;
try{
if(this.getChannelSftp() == null || !this.getChannelSftp().isConnected()) //if the channel isn't open, open it.
this.openSFTPConnection(); //open connection
inputStream = this.getChannelSftp().get(filePath); //get the stream
}catch(SftpException ex){
ex.printStackTrace();
}
return inputStream;
}
public String uploadFile(MultipartFile file, String dest){
try{
System.out.println(this.getSession().getConfig("FileUploadBaseSizeLimit"));
if(this.getChannelSftp() == null || !this.getChannelSftp().isConnected()) //if not connected open the connection
this.openSFTPConnection();
this.getChannelSftp().cd(dest); //set working dir
this.getChannelSftp().put(file.getInputStream(), dest + '/' + file.getOriginalFilename()); //get the input stream from the multipart file and put it in the destination
}catch(IOException IO){
IO.printStackTrace();
}catch(SftpException sftp){
sftp.printStackTrace();
}
return file.getOriginalFilename() + " has been uploaded";
}
public String downloadFile(String orig, String dest){
try{
if(this.getChannelSftp() == null || !this.getChannelSftp().isConnected()) //if not connected
this.openSFTPConnection();
File download = new File(dest + '/' + FilenameUtils.getName(orig)); //create a new local file
OutputStream outputStream = new FileOutputStream(download); //generate an output stream to the file
byte[] buffer = new byte[10000000]; //10MB buffer instance
BufferedInputStream bufferedInputStream = new BufferedInputStream(this.getChannelSftp().get(orig)); //create a buffered input stream off the input stream from the ssh get
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); // and a buffered output stream off the output stream we created
int readCount; //integer link to the buffer
while((readCount = bufferedInputStream.read(buffer)) > 0){ //read the buffered input stream into the buffer. while it's not empty...
bufferedOutputStream.write(buffer, 0, readCount); //write the buffer to the output stream
}
bufferedInputStream.close(); //close the streams
bufferedOutputStream.close();
outputStream.close();
}catch(IOException IO){
IO.printStackTrace();
}catch(SftpException sftp){
sftp.printStackTrace();
}
return "File " + FilenameUtils.getName(orig) + " has been downloaded."; //return that we've successfully uploaded the file
}
public void writeFile(String dir, String fileName, InputStream fileIS) { //writes a file given the destination directory, fileName, and an input stream
try{
if(this.getChannelSftp() == null || !this.getChannelSftp().isConnected() ) //if the connection isn't already open, open it
this.openSFTPConnection();
this.getChannelSftp().cd(dir); //set the working dir for better shorthand
this.getChannelSftp().put(fileIS, fileName); //put the file.
fileIS.close(); //close the file stream
}catch(SftpException e){
e.printStackTrace();
}catch(IOException IO){
IO.printStackTrace();
}
}
public void moveFile(String path, String dest){
try{
if(this.getChannelSftp() == null || !this.getChannelSftp().isConnected()){ //open connection if not done already
this.openSFTPConnection();
}
this.getChannelSftp().rename(path, dest + '/' + FilenameUtils.getName(path)); //ssh command to move the file.
}catch(SftpException e){
e.printStackTrace();
}
}
public String moveFiles(String dir, String filter, String dest){
try{
if(this.getChannelSftp() == null || !this.getChannelSftp().isConnected()){ //open if not done already
this.openSFTPConnection();
}
Collection<fileInfo> files = this.listFiles(dir, filter); //invoke the listFiles method and convert to an array
Iterator<fileInfo> iterator = files.iterator(); //iterator
while( iterator.hasNext()){ //while there is another value in the collection
fileInfo thisFile = iterator.next(); // have to store the fileInfo somewhere. can't just keep calling next.
this.getChannelSftp().rename(dir + '/' + thisFile.getName(), dest + '/' + thisFile.getName());
}
this.closeSFTPConnection(); //close the connection
}catch(Exception ex){
ex.printStackTrace();
}
return "all files moved";
}
}
Test Class:
package RestAPI;
import JSch.SFTP.SftpConnector;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.io.FileInputStream;
import java.io.InputStream;
#RunWith(SpringRunner.class)
#WebMvcTest(value = SftpRestController.class, secure = false)
public class SftpRestUnitTest {
#Autowired
private MockMvc mockMvc;
#MockBean
SftpConnector connector ;
#Test
public void testFileUpload() throws Exception{
MockMultipartFile testFile1 = new MockMultipartFile("data", "WSUID1.csv", "application/csv", "some xml".getBytes());
Mockito.when(
connector.uploadFile(Mockito.any(),Mockito.anyString())
).thenReturn("WSUID1.csv has been uploaded");
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.multipart("/Upload").file(testFile1).param("dest", "/tmp/test")).andReturn();
System.out.println(result.getResponse().getContentAsString());
assert result.getResponse().getContentAsString().equals("WSUID1.csv has been uploaded to remote directory \n");
}
#Test
public void testDownload() throws Exception{
String expected = "TestFile.csv has been downloaded to the specified local directory";
Mockito.when(
connector.downloadFile(Mockito.anyString(), Mockito.anyString())
).thenReturn("File TestFile.csv has been downloaded.");
RequestBuilder request = MockMvcRequestBuilders.get("/Download").param("fileName","/tmp/TestFile.csv").param("dest", "/tmp");
MvcResult result = mockMvc.perform(request).andReturn();
System.out.println(result.getResponse().getContentAsString());
assert result.getResponse().getContentAsString().equals(expected);
}
#Test
public void testMoveFiles() throws Exception{
Mockito.when(
connector.moveFiles(Mockito.anyString(),Mockito.anyString(),Mockito.anyString())
).thenReturn("all files moved");
RequestBuilder request = MockMvcRequestBuilders.put("/moveFiles").param("dir","/IB_Test").param("fileType", "*.csv").param("dest", "/InternatlPgms/INTO/Archive/Receipt");
MvcResult result = mockMvc.perform(request).andReturn();
System.out.println(result.getResponse().getContentAsString());
assert result.getResponse().getStatus() == 200;
}
#Test
public void testReadFile() throws Exception{
String expected = "greetings from java test";
InputStream mockStream = Mockito.mock(FileInputStream.class);
Mockito.when(
connector.readFile(Mockito.anyString())
).thenReturn(mockStream);
RequestBuilder request = MockMvcRequestBuilders.get("/readFile").param("path", "IB_Test");
MvcResult result = mockMvc.perform(request).andReturn();
System.out.println(result.getResponse().getContentAsString());
assert result.equals(expected);
}
}
Error Logs:
The specified file is a directory.
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2873)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1337)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1290)
at JSch.SFTP.SftpConnector.readFile(SftpConnector.java:133)
at RestAPI.SftpRestController.readFile(SftpRestController.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:166)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Any help is greatly appreciated.

You create a new instance of SftpConnector in every method of your controller. It is not an injected bean so it won't be replaced with your mock.
For an example have a look at this article (section 5):
https://www.baeldung.com/spring-boot-testing

Related

How to mock S3AsyncClient in Junit

I have one method that uploads files to Amazon S3. I am trying to write JUnit for this method but get NullPointerException on the S3AsyncClient:
my class:
public class S3Client<T> {
private static final Logger log = LoggerFactory.getLogger(S3Client.class);
S3AsyncClient client;
/**
*
* #param s3Configuration
*/
public S3Client(AWSS3Configuration s3Configuration) {
this.client = s3Configuration.getAsyncClient();
}
/**
* Uploads a file s3 bucket and returns etag
* #param uploadData
* #return
* #throws S3Exception
*/
public CompletableFuture<String> uploadFile(S3UploadData<T> uploadData) throws S3Exception {
int contentLength;
AsyncRequestBody asyncRequestBody;
if(uploadData.getContent() instanceof String) {
String content = (String) uploadData.getContent();
contentLength = content.length();
asyncRequestBody = AsyncRequestBody.fromString(content);
}
else if(uploadData.getContent() instanceof byte[]){
byte[] bytes = (byte[]) uploadData.getContent();
contentLength = bytes.length;
asyncRequestBody = AsyncRequestBody.fromBytes(bytes);
}
else{
throw new IllegalArgumentException("Unsupported upload content type");
}
PutObjectRequest putObjRequest = PutObjectRequest.builder()
.bucket(uploadData.getBucketName())
.key(uploadData.getFileName())
.metadata(uploadData.getMetaData())
.contentLength((long) contentLength).build();
CompletableFuture<String> response = client.putObject(putObjRequest, asyncRequestBody).thenApply(
getPutObjectResponse -> {
log.info("Got response from S3 upload={}", getPutObjectResponse.eTag());
return getPutObjectResponse.eTag();
});
response.exceptionally(throwable -> {
log.error("Exception occurred while uploading a file intuit_tid={} file={}",uploadData.getTransactionId(),uploadData.getFileName());
throw new S3Exception(throwable.getMessage());
});
return response;
}
input for this class object of S3UploadData:
`
#Getter
#AllArgsConstructor
public class InputData<T> {
T content;
String fileName;
String bucketName;
String transactionId;
Map<String, String> metaData;
}`
can u help please with writing Junit for uploadFile method?
You have no JUNIT test code. You should have code that uses org.junit.jupiter.api.*
Instead of using a MOCK, call the actual S3 Async code in a #TestInstance integration test to make sure it works. For example, here is my test in IntelliJ.
As you can see, my test passed and I Know my code works -- which is the point of this AWS integration test.
If my code failed or threw an exception for some reason, my test would fail. For example, if I passed a bucket name that does not exist, I would get:
Here is my Java Amazon S3 Async code:
package com.example.s3.async;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
import java.nio.file.Paths;
import java.util.concurrent.CompletableFuture;
// snippet-end:[s3.java2.async_ops.import]
// snippet-start:[s3.java2.async_ops.main]
/**
* To run this AWS code example, ensure that you have setup your development environment, including your AWS credentials.
*
* For information, see this documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class S3AsyncOps {
public static void main(String[] args) {
final String USAGE = "\n" +
"Usage:\n" +
" S3AsyncOps <bucketName> <key> <path>\n\n" +
"Where:\n" +
" bucketName - the name of the Amazon S3 bucket (for example, bucket1). \n\n" +
" key - the name of the object (for example, book.pdf). \n" +
" path - the local path to the file (for example, C:/AWS/book.pdf). \n";
if (args.length != 3) {
System.out.println(USAGE);
System.exit(1);
}
String bucketName = args[0];
String key = args[1];
String path = args[2];
Region region = Region.US_WEST_2;
S3AsyncClient client = S3AsyncClient.builder()
.region(region)
.build();
putObjectAsync(client, bucketName, key, path);
}
public static void putObjectAsync(S3AsyncClient client,String bucketName, String key, String path) {
PutObjectRequest objectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.key(key)
.build();
// Put the object into the bucket
CompletableFuture<PutObjectResponse> future = client.putObject(objectRequest,
AsyncRequestBody.fromFile(Paths.get(path))
);
future.whenComplete((resp, err) -> {
try {
if (resp != null) {
System.out.println("Object uploaded. Details: " + resp);
} else {
// Handle error
err.printStackTrace();
}
} finally {
// Only close the client when you are completely done with it
client.close();
}
});
future.join();
}
}
Now for my test, i want to call this code, not MOCK it. I have setup my test in IntelliJ like this,
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import software.amazon.awssdk.regions.Region;
import java.io.*;
import java.util.*;
import com.example.s3.async.*;
import software.amazon.awssdk.services.s3.S3AsyncClient;
#TestInstance(TestInstance.Lifecycle.PER_METHOD)
#TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class AmazonS3AsyncTest {
private static S3AsyncClient s3AsyncClient;
// Define the data members required for the tests
private static String bucketName = "";
private static String objectKey = "";
private static String objectPath = "";
private static String toBucket = "";
#BeforeAll
public static void setUp() throws IOException {
// Run tests on Real AWS Resources
s3AsyncClient = S3AsyncClient.builder()
.region(Region.US_EAST_1)
.build();
try (InputStream input = AmazonS3Test.class.getClassLoader().getResourceAsStream("config.properties")) {
Properties prop = new Properties();
if (input == null) {
System.out.println("Sorry, unable to find config.properties");
return;
}
//load a properties file from class path, inside static method
prop.load(input);
// Populate the data members required for all tests
bucketName = prop.getProperty("bucketName");
objectKey = prop.getProperty("objectKey");
objectPath= prop.getProperty("objectPath");
toBucket = prop.getProperty("toBucket");
} catch (IOException ex) {
ex.printStackTrace();
}
}
#Test
#Order(1)
public void whenInitializingAWSS3Service_thenNotNull() {
assertNotNull(s3AsyncClient);
System.out.println("Test 1 passed");
}
#Test
#Order(2)
public void putObject() {
S3AsyncOps.putObjectAsync(s3AsyncClient, bucketName, objectKey, objectPath);
System.out.println("Test 2 passed");
}
}
You could use Mockito to mock the S3AsyncClient operations.
#Mock
private S3AsyncClient s3AsyncClient;
Below is the unit test case for my upload file implementation. It will surely give you insights how it is done and it can be done.
#Nested
class UploadFile {
#Captor
ArgumentCaptor<PutObjectRequest> putObjectRequestCaptor;
#Captor
ArgumentCaptor<AsyncRequestBody> requestBodyCaptor;
#Test
void testSuccessfulUpload() {
Flux<ByteBuffer> body = Flux.just();
var expectedResponse = PutObjectResponse.builder().build();
when(s3AsyncClient.putObject(putObjectRequestCaptor.capture(), requestBodyCaptor.capture())) .thenReturn(CompletableFuture.completedFuture(expectedResponse));
fileUploadService.upload("TEST_PREFIX", "test.zip", body);
assertThat(putObjectRequestCaptor.getValue().bucket()).isEqualTo(TEST_BUCKET);
assertThat(putObjectRequestCaptor.getValue().key()).isEqualTo("TEST_PREFIX/test.zip");
assertThat(requestBodyCaptor.getValue()).isNotNull();
}
}

Web server hanging. I have no idea why

I'm writing a very tiny, very sh*tty web server just for fun. It was working fine with GET requests or returning a 404. And worked very briefly working with POST requests. And now it just hangs on any POST request.
Here is the relevant bit of code. As you can see I put in some logging to both System.out and to a file. The logging to System.out works, but the logging to file never happens. If I remove the logging to file, it still hangs on the line after System.out log. I included the few lines previous so you can see that it is the exact same code as returning a 404. 404 works, but POST doesn't. This is using a ServerSocket. I'm at a complete loss at this point. Would appreciate any insight.
Edit: Have included the main and sendResponse methods in case there is anything in there that might be causing this.
Edit #2: Ima just post the whole thing.
package beerio;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Beerio {
public static void main( String[] args ) throws Exception {
try (ServerSocket serverSocket = new ServerSocket(80)) {
while (true) {
try (Socket client = serverSocket.accept()) {
handleClient(client);
}
catch(Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
BufferedWriter writer = new BufferedWriter(new FileWriter("errorlog.txt", true));
writer.append(System.lineSeparator());
writer.append(sw.toString());
writer.append(System.lineSeparator());
writer.close();
continue;
}
}
}
}
private static void handleClient(Socket client) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
//make request into string. this only parses until the first blank line
StringBuilder requestBuilder = new StringBuilder();
String line;
while (!(line = br.readLine()).isBlank()) {
requestBuilder.append(line + "\r\n");
}
//split string and parse into request info
String request = requestBuilder.toString();
String[] requestsLines = request.split("\r\n");
String[] requestLine = requestsLines[0].split(" ");
String method = requestLine[0];
String path = requestLine[1];
String version = requestLine[2];
String host = requestsLines[1].split(" ")[1];
List<String> headers = new ArrayList<>();
for (int h = 2; h < requestsLines.length; h++) {
String header = requestsLines[h];
headers.add(header);
}
//rest of request contains post info. parse that here
if(method.equals("POST")) {
String parameters;
parameters = br.readLine();
String[] temp = parameters.split("&");
String[][] params = new String[temp.length][2];
for(int i=0; i<temp.length; i++) {
params[i][0] = temp[i].substring(0,temp[i].indexOf("="));
params[i][1] = temp[i].substring(temp[i].indexOf("=")+1);
}
}
Path filePath = getFilePath(path);
if (method.equals("GET")) {
System.out.println("doGet");
if (Files.exists(filePath)) {
// file exist
String contentType = guessContentType(filePath);
sendResponse(client, "200 OK", contentType, Files.readAllBytes(filePath));
} else {
// 404
byte[] notFoundContent = "<h1>Not found :(</h1>".getBytes();
sendResponse(client, "404 Not Found", "text/html", notFoundContent);
}
} else if(method.equals("POST")){
byte[] postContent = "<h1>POST Failed Successfully</h1>".getBytes();
sendResponse(client, "200 OK", "text/html", postContent);
}
}
private static void sendResponse(Socket client, String status, String contentType, byte[] content) throws IOException {
OutputStream clientOutput = client.getOutputStream();
clientOutput.write(("HTTP/1.1 " + status+"/r/n").getBytes());
clientOutput.write(("ContentType: " + contentType + "\r\n").getBytes());
clientOutput.write("\r\n".getBytes());
clientOutput.write(content);
clientOutput.write("\r\n\r\n".getBytes());
clientOutput.flush();
client.close();
}
private static Path getFilePath(String path) {
if ("/".equals(path)) {
path = "\\index.html";
}
return Paths.get("C:\\Users\\shawn\\beerio\\beerio\\tmp\\www", path);
}
private static String guessContentType(Path filePath) throws IOException {
return Files.probeContentType(filePath);
}
}

Selenium multiple browser testing. Only last browser receives cookies. TestNG

I have been working on automated tests and are trying to skip the login page, by logging in one time, saving the cookies on a text file and then finally reading and adding the files on a new browser instance.
It works fine when I just create one browser at a time, but if I create multiple browser parallel, only one page receives the cookies (thus skipping the login page as intended).
Here is the code:
My Setup(). This works perfect. I just login, saves the cookie data in text file and close the browser.
#BeforeClass
public void cookie_setup(){
//Firefox
System.setProperty("webdriver.gecko.driver", "C:\\SeleniumGecko/geckodriver.exe");
webdriver = new FirefoxDriver();
File file = new File("Cookies.data");
try
{
file.delete();
file.createNewFile();
FileWriter fileWrite = new FileWriter(file);
BufferedWriter Bwrite = new BufferedWriter(fileWrite);
for ( Cookie ck: webdriver.manage().getCookies())
{
Bwrite.write((ck.getName()+";"+ck.getValue()+";"+ck.getDomain()+";"+ck.getPath()+";"+ck.getExpiry()+";"+ck.isSecure()));
Bwrite.newLine();
}
Bwrite.close();
fileWrite.close();
}catch (Exception ex)
{
ex.printStackTrace();
}
cookies = webdriver.manage().getCookies();
webdriver.quit();
}
Then I create a new firefox browser
Reads the cookie data in the text file
Add the the cookies to the page with:`webdriver.manage().addCookie(ck);
#Test(threadPoolSize = 4, invocationCount = 4)
public void search() throws InterruptedException {
System.setProperty("webdriver.gecko.driver",
"C:\\SeleniumGecko/geckodriver.exe");
webdriver.navigate().to("http://pagethatrequirelogin/");
try {
File file = new File("Cookies.data");
FileReader fileReader = new FileReader(file);
BufferedReader Buffreader = new BufferedReader(fileReader);
String strline;
while ((strline = Buffreader.readLine()) != null) {
StringTokenizer token = new StringTokenizer(strline, ";");
while (token.hasMoreTokens()) {
String name = token.nextToken();
String value = token.nextToken();
String domain = token.nextToken();
String path = token.nextToken();
Date expiry = null;
String val;
if (!(val = token.nextToken()).equals("null")) {
expiry = new Date(val);
}
Boolean isSecure = Boolean.valueOf(token.nextToken());
Cookie ck = new Cookie(name, value, domain, path, expiry, isSecure);
System.out.println(ck);
webdriver.manage().addCookie(ck);
}
}
}
catch(Exception ex){
}
}
I use TestNG and with the parameters:
#Test(threadPoolSize = 4, invocationCount = 4)
I create 4 firefox browers at the same time, but only one of browser receives the cookies and skip login page.
It works perfectly fine if I just create one browser at a time multiple times.
The issue is because you are perhaps having your #Test methods overwrite the webdriver instance. Here's a sample that shows how a thread-safe version of it would look like:
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
public class CookiesSample {
private static final ThreadLocal<WebDriver> drivers = new ThreadLocal<>();
private List<Cookie> cookies = new ArrayList<>();
#BeforeClass
public void beforeClass() throws IOException {
String text = "Cookies.data";
List<String> lines = Files.readAllLines(Paths.get(text));
lines.forEach(line -> {
StringTokenizer token = new StringTokenizer(line, ";");
while (token.hasMoreTokens()) {
String name = token.nextToken();
String value = token.nextToken();
String domain = token.nextToken();
String path = token.nextToken();
Date expiry = null;
String val = token.nextToken();
if (!val.equals("null")) {
expiry = asDate(val);
}
Boolean isSecure = Boolean.valueOf(token.nextToken());
Cookie ck = new Cookie(name, value, domain, path, expiry, isSecure);
cookies.add(ck);
}
});
}
#BeforeMethod
public void beforeMethod() {
WebDriver driver = new FirefoxDriver();
cookies.forEach(driver.manage()::addCookie);
drivers.set(driver);
}
#AfterMethod
public void afterMethod() {
drivers.get().quit();
drivers.remove();
}
#Test(threadPoolSize = 4, invocationCount = 4)
public void testMethod() {
drivers.get().get("http://pagethatrequirelogin/");
}
private static Date asDate(String text) {
try {
return DateFormat.getInstance().parse(text);
} catch (ParseException e) {
return new Date();
}
}
}

Issue while downloading a file from a WEB FTP using Java?

I have been having an issue while downloading a file from a Web Ftp through a Java program using Apache Commons Net.
Let's say I want to download this file : webftp.vancouver.ca/opendata/bike_rack/BikeRackData.csv
This is not a file you will be able to download via an HTTP request. I can do it with my browser because I'm using a FTP plugin.
It appears that the traditionnal way to retrieve files from a FTP server doesn't work with that particuliar type (Web FTP). I can establish the connection, but the file retrieval doesn't work as the file is empty.
The code I used (classic way to retrieve a file using Apache Commons) is the following :
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory;
public class FTPDownload {
final private FTPClient ftp = new FTPClient();
private String input;
private String server;
private String path;
private String destination;
private String fileName;
public FTPDownload(String input, String destination) {
this.input = input;
this.destination = destination;
if (input != null && input.contains("/")) {
String[] elts = input.split("/");
server = elts[0];
fileName = elts[elts.length - 1];
path = input.substring(server.length() - 1);
} else {
server = input;
path = null;
}
}
public FTPDownload(String server, String path, String destination) {
this.server = server;
this.path = path;
this.destination = destination;
if (path != null && path.contains("/")) {
String[] elts = path.split("/");
fileName = elts[elts.length - 1];
}
}
public boolean retrieveFile() throws IOException {
boolean error = false;
ftp.connect(server);
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return false;
}
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterRemotePassiveMode();
File download = new File(destination + "/" + fileName);
OutputStream outputStream;
outputStream = new FileOutputStream(download);
ftp.changeWorkingDirectory(path.substring(0, path.length() - fileName.length()));
ftp.setParserFactory(new DefaultFTPFileEntryParserFactory());
String[] list = ftp.listNames();
ftp.retrieveFile(fileName, outputStream);
outputStream.close();
ftp.logout();
return error;
}
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
FTPDownload myFtp = new FTPDownload("webftp.vancouver.ca", "/opendata/bike_rack/BikeRackData.csv", "E:\\test");
myFtp.retrieveFile();
}
}
Hope the problem is understandable.
Thank you.

Java JAX-RS 2.0 + Jersey + Jetty Security

I am building a RESTful Web Service in which I have a set of commands that should only be used by admins. Ideally I would have two ports setup, both with Server-side SSL, however one port would require client SSL. I would like to know how I could separate resources by port. Alternatively, I could have the system setup to use #RolesAllowed for each resource, In that case I would need to know how to set the role of the user (by port would be great). So far I have a http and https port setup and working. I also have all the resources setup.
It might also be good to know that the two files shown below are the only ones used to run with webservice. there are no container files etc.
keep in mind that not all code is complete. Here is the code:
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import java.io.File;
import java.io.FileNotFoundException;
public class Core {
static String jettyKeystore = "Assets/keys/keystore";
//http://www.eclipse.org/jetty/documentation/current/configuring-ssl.html
public static void main(String args[]) throws FileNotFoundException {
String keystorePath = System.getProperty(
"example.keystore", jettyKeystore);
File keyStoreFile = new File(keystorePath);
if (!keyStoreFile.exists()) {
throw new FileNotFoundException(keyStoreFile.getAbsolutePath());
}
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
int port = 8080;
int securePort = 8084;
HttpConfiguration httpConf = new HttpConfiguration();
httpConf.setSecureScheme("https");
httpConf.setSecurePort(securePort);
httpConf.setOutputBufferSize(32768);
Server jettyServer = new Server();
ServerConnector http = new ServerConnector(jettyServer,
new HttpConnectionFactory(httpConf));
http.setPort(port);
http.setIdleTimeout(30000);
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(keyStoreFile.getAbsolutePath());
sslContextFactory.setKeyStorePassword("password");
sslContextFactory.setKeyManagerPassword("password");
//sslContextFactory.setNeedClientAuth(true);
HttpConfiguration httpsConf = new HttpConfiguration(httpConf);
httpsConf.addCustomizer(new SecureRequestCustomizer());
ServerConnector https = new ServerConnector(jettyServer,
new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
new HttpConnectionFactory(httpsConf));
https.setPort(securePort);
https.setIdleTimeout(500000);
jettyServer.setConnectors(new Connector[]{http, https});
jettyServer.setHandler(context);
ServletHolder jerseyServlet = context.addServlet(
org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter(
"jersey.config.server.provider.classnames",
RQHandler.class.getCanonicalName());
try {
jettyServer.start();
System.err.println("Server Started on port: " + port + "," + securePort);
jettyServer.join();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
jettyServer.destroy();
}
}
}
Here is the resource file:
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.ContentDisposition;
import java.io.*;
import java.net.UnknownHostException;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.websocket.server.PathParam;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
#Path("audio_archives")
public class RQHandler {
MongoClient mc;
DB db;
DBCollection audioCollection;
DBCollection videoCollection;
{
try {
mc = new MongoClient("localhost");
db = mc.getDB("ziondb");
audioCollection = db.getCollection("audio");
videoCollection = db.getCollection("video");
} catch (UnknownHostException e) {
e.printStackTrace();
System.out.println("exiting!");
System.exit(0);
}
}
#GET
#Path("getMedia")
#Produces(MediaType.TEXT_PLAIN)
public String getAllMedia(#QueryParam("type") String type){
String DBreturn = "";
if(type.toLowerCase().equals("audio")) {
DBCursor cursor = audioCollection.find();
try {
while (cursor.hasNext()) {
DBreturn += cursor.next();
}
} finally {
cursor.close();
}
} else if(type.toLowerCase().equals("video")) {
DBCursor cursor = videoCollection.find();
try {
while (cursor.hasNext()) {
DBreturn += cursor.next();
}
} finally {
cursor.close();
}
}
if (DBreturn.equals("")) {
DBreturn = "{ \"entries\" : \"null\" }";
}
return DBreturn;
}
#GET
#Path("getMediaFile")
#Produces({"video/mp4"}) //use application/type for downloadable file
public File getMediaFile(#QueryParam("type") String type,#QueryParam("resourceId") String id){
String DBreturn = "";
if(type.toLowerCase().equals("audio")) {
DBCursor cursor = audioCollection.find();
try {
while (cursor.hasNext()) {
DBreturn += cursor.next();
}
} finally {
cursor.close();
}
} else if(type.toLowerCase().equals("video")) {
DBCursor cursor = videoCollection.find();
try {
while (cursor.hasNext()) {
DBreturn += cursor.next();
}
} finally {
cursor.close();
}
}
if (DBreturn.equals("")) {
DBreturn = "{ \"entries\" : \"null\" }";
}
return (new File("/home/digitalblueeye/Downloads/SampleVideo_1280x720_50mb.mp4"));
}
#GET
#Path("getMediaStream")
#Produces({"video/mp4"}) //use application/type for downloadable file
public StreamingOutput getMediaStream(#QueryParam("type") String type, #QueryParam("resourceId") String id){
return new StreamingOutput() {
#Override
public void write(OutputStream outputStream) throws IOException, WebApplicationException {
File file = new File("/home/digitalblueeye/Downloads/SampleVideo_1280x720_50mb.mp4");
outputStream = new FileOutputStream(file);
outputStream.write(0);
}
};
}
//********************************************************
#GET
#Path("")
#Produces(MediaType.TEXT_HTML)
public static File home() {
return (new File("Assets/index.html"));
}
#GET
#Path("developers")
#Produces(MediaType.TEXT_HTML)
public static File sysInfo() {
return (new File("Assets/manual.html"));
}
#GET
#Produces(MediaType.APPLICATION_JSON)
#Path("list_by_date")
public String listByDate(#QueryParam("model") String model,
#QueryParam("from") String fromDate,
#QueryParam("to") String toDate,
#QueryParam("quota") String quota) {
return null;
}
#GET
#Produces(MediaType.APPLICATION_JSON)
#Path("list_by_param")
public String listByParam(#QueryParam("model") String model,
#QueryParam("param_type") String paramType,
#QueryParam("param") String param,
#QueryParam("quota") String quota) {
return null;
}
#GET
#Produces(MediaType.APPLICATION_JSON)
#Path("list_files")
public String listFile(#QueryParam("id") String id) {
return null;
}
#GET
#Produces("application/mp3,application/wav")
#Path("get_file")
public File getFile(#QueryParam("id") String id) {
return null;
}
#GET
#Produces("audio/mp3,audio/wav")
#Path("stream_file")
public File streamFile(#QueryParam("id") String id) {
return null;
}
private byte[] readFromStream(InputStream stream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1000];
int wasRead = 0;
do {
wasRead = stream.read(buffer);
if (wasRead > 0) {
baos.write(buffer, 0, wasRead);
}
} while (wasRead > -1);
return baos.toByteArray();
}
#PUT
#Consumes(MediaType.APPLICATION_JSON)
#Path("get_auth_secret")
public void get_auth_secret(InputStream is) throws IOException {
byte[] bytes = readFromStream(is);
String input = new String(bytes);
System.out.println(input);
}
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Path("update")//update reources
public void putstuff(InputStream is) throws IOException {
byte[] bytes = readFromStream(is);
String input = new String(bytes);
System.out.println(input);
}
#DELETE
#Consumes(MediaType.APPLICATION_JSON)//use json for batch delete
#Path("delete")//delete reources
public void deletestuff(InputStream is) throws IOException {
byte[] bytes = readFromStream(is);
String input = new String(bytes);
System.out.println(input);
}
#PUT
#Consumes("application/mp3,application/wav")
#Path("create")//create resources
public void createstuff(InputStream is) throws IOException {
byte[] bytes = readFromStream(is);
String input = new String(bytes);
System.out.println(input);
}
/*
#POST
#Path("/upload")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
#FormParam("file") InputStream fileInputStream,
#QueryParam("filename") String filename) {
String filePath = "FileStore/audio/" + filename;
// save the file to the server
saveFile(fileInputStream, filePath);
String output = "File saved to server location : " + filePath;
return Response.status(200).entity(output).build();
}
// save uploaded file to a defined location on the server
private void saveFile(InputStream uploadedInputStream,
String serverLocation) {
try {
OutputStream outpuStream = new FileOutputStream(new File(serverLocation));
int read = 0;
byte[] bytes = new byte[1024];
outpuStream = new FileOutputStream(new File(serverLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
outpuStream.write(bytes, 0, read);
}
outpuStream.flush();
outpuStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
#POST
#Path("/upload")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
#FormDataParam("file") InputStream inputStream,
#FormDataParam("file") FormDataContentDisposition formDataContentDisposition) {
String fileName = formDataContentDisposition.getFileName();
String filePath = saveFile(inputStream, fileName);
String output = "File: " + filePath;
return Response.status(Response.Status.CREATED).entity(output).build();
}
#POST
#Path("/multi")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
FormDataMultiPart form) {
FormDataBodyPart filePart = form.getField("file");
ContentDisposition headerOfFilePart = filePart.getContentDisposition();
InputStream inputStream = filePart.getValueAs(InputStream.class);
String filePath = saveFile(inputStream, headerOfFilePart.getFileName());
String output = "File: " + filePath;
return Response.status(Response.Status.CREATED).entity(output).build();
}
private String saveFile(InputStream inputStream, String fileName) {
try {
File file = File.createTempFile("temp", ".txt");
OutputStream outputStream = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.flush();
outputStream.close();
return file.getAbsolutePath();
} catch (Exception e) {
}
return "";
}
*/
//Media streaming with JAX-RS https://github.com/aruld/jersey-streaming
}
The methods I am looking to protect are PUT, POST, and DELETE. I just want to make sure that only admins can create, update, and remove resources. Since a desktop program will be the only way to use admin methods, I am not concerned about ease of implementing the admin methods into a website.

Categories