Exception after reading values from jsp page - java

I am inserting values into database but I am getting UIException here is my code sample,
public void createTeacherInfo(HttpServletRequest request) {
try{
TeacherInfo teacherInfo= new TeacherInfo();
request.getParameter("flowName");
DateFormat df = new SimpleDateFormat("dd-mm-yyyy");
String tId= request.getParameter("teacherId");
teacherInfo.setTeahcerId(Integer.parseInt(tId));
//teacherInfo.setTeahcerId(Integer.parseInt(request.getParameter("teacherId")));
teacherInfo.setTeacherName(request.getParameter("teacherName"));
/*teacherInfo.setDob(df.parse(request.getParameter("dob")));
teacherInfo.setDoj(df.parse(request.getParameter("doj")));*/
teacherInfo.setTeacherEducation(request.getParameter("education"));
teacherInfo.setPreviousEmployeDetails(request.getParameter("prevdetails"));
//teacherInfo.setYearOfExper(Integer.parseInt(request.getParameter("experience")));
teacherInfo.setTeahcherPhoto(request.getParameter("photo"));
teacherInfo.setTeacherEmail(request.getParameter("email"));
System.out.println(tId);
System.out.println("TeacherId");
pupilInfoManagementBusinessService.createTeacherInfo(teacherInfo);
}catch (BusinessException e) {
webLayerLogger.error(CommonUtils.getStackTrace(e));
throw new UIException(e,UIMessageHelper.getLocalValue("exception while Inserting data"));
}
}
In this method after reading all values it will go to service method , Here is service class method,
#Override
public void createTeacherInfo(TeacherInfo teacherInfo) throws BusinessException {
try {
pupilInfoManagementDataService.createTeacherInfo(teacherInfo);
}catch (Exception e) {
businessServiceLogger.error(CommonUtils.getStackTrace(e));
throw new BusinessException(this.getClass(), e, e.getMessage());
}
}
My problem is values are reading but not inserting to database. Please help me in this .

I'll bet the html form for this includes a file upload for the photo. When a form includes a file upload, then request.getParameter will not work any longer (the values will always be null). When you do a file upload you have to use Apache Commons File Upload library to retrieve the parameters from the request, or you can use request.getPart (if you are on the latest version of your servlet container).

Related

How to output a temporary file in API service in Java, and delete it in the end?

I am writing an API service that fetches data from a stream, and outputs it in a file. I can't output it as a stream because I use Swagger (now OpenAPI) 2.0, which doesn't support output streams (Swagger 3.0 does, but i can't use it).
What would be the cleanest way to make a file, output it via the service, and then make sure it gets deleted?
I initially thought I might use a temp file and delete in finally clause. However, there is no guarantee that the file finished downloading on the client side before that clause is reached and file is deleted.
Am I right? Wrong? Is there a better way to do this?
I was talking about using a closeable in the comments. This is it.
Usage:
try (TempFile file = new TempFile("tempfile", ".txt")) {
// do stuff with file
} catch (IOException e) {
// error handling.
// file should be automatically deleted.
}
TempFile:
public class TempFile implements AutoCloseable {
private final File file;
public TempFile(String prefix, String suffix) {
this.file = File.createTempFile(prefix, suffix);
}
public File getFile() {
return this.file;
}
#Override
public void close() throws IOException {
this.file.delete();
}
}

Nanohttpd serve multiple files

Using nanohttpd I can select a chosen file and start a server to serve that one file.
Is it possible to serve a list of lot of files?
That is, I have lot of files in sd card and I want to serve the selected ones. So how to give an array of file paths of those files and generate and return URL for them, so that I can access them from network.
Not an HTML page which lists all those files and folders.
I have gone through this, this is not what I am referring to. In this it it just lists the root folder and lists them all in a HTML page, for a user to view/select. Not what I am after.
Just an array of server URLs for a selected, chosen list of files in sdcard, which I can then use programmatically.
As of now I have this
protected void onCreate(Bundle savedInstanceState) {
...
server = new Mp3Server();
try {
server.start();
} catch(IOException ioe) {
Log.w("Httpd", "The server could not start.");
}
Log.w("Httpd", "Web server initialized.");
}
...
...
public class Mp3Server extends NanoHTTPD {
public Mp3Server() {
super(8089);
}
#Override
public Response serve(String uri, Method method,
Map<String, String> header, Map<String, String> parameters,
Map<String, String> files) {
String answer = "";
FileInputStream fis = null;
try {
fis = new FileInputStream("/storage/C67A-18F7/Music/music.mp3");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newChunkedResponse(Status.OK, "audio/mpeg", fis);
}
}
Or do I have to pass the chosen file and start/stop server each time for each file? But this sounds inefficient.

Is my file saving logic correct?

My java app calls a rest endpoint and in the response body is a 10GB XML file. Before I send the rest quest, I ask the service how many records will be in the file. I then retrieve the file. When I run my app, the file is saved successfully but only roughly 50% of the expected records. There are 2 reasons the file doesn't have all the records:
The file sent from the rest endpoint only has 50% of the expected records
My app is falling over when before it has finished downloading
My question is, if in scenario 2 and my app falls over, would I see an exception stating so? I do not see an exception, in fact, I see my log statement after the save is saved saying 'File successfully saved'.
EDIT: I have downloaded the file outside of my app, via a curl request and the same thing happened - only 50% of the expected population was downloaded. This proves the issue isn't with my file-saving logic.
public void saveFile() {
try {
downloadAndSaveFile();
} catch (Exception e) {
LOGGER.error("A error has occurred processing all content, caused by {}", e.getMessage(), e);
throw new RuntimeException(e);
}
}
private void downloadAndSaveFile() throws Exception {
long recordCount = countRecords();
LOGGER.info("Number of records to process is {}", recordCount);
if (recordCount > 0 ) {
InputStream dataToSave = getAllContent();
saveStream(dataToSave);
LOGGER.info("File successfully saved.");
} else {
LOGGER.error("No content to retrieve");
throw new RuntimeException("There are no records to process");
}
}
public InputStream getAllContent() throws Exception {
return callRestEndpoint(webTarget).readEntity(InputStream.class);
}
private Response callRestEndpoint(WebTarget target) throws InterruptedException {
Response response = null;
for (int numberOfTries = 0; numberOfTries < reconnectRetries; numberOfTries++) {
try {
response = makeGetRequest(target);
if (OK.getStatusCode() == response.getStatus()) {
break;
}
} catch (Exception ex) {
retryRequest(numberOfTries, ex);
}
}
return response;
}
public void saveStream(InputStream inputStream) throws IOException {
File fileToCreate = new File(fileName);
if (!fileToCreate.exists()) {
fileToCreate.mkdirs();
}
Files.copy(
inputStream,
fileToCreate.toPath(),
StandardCopyOption.REPLACE_EXISTING
);
closeQuietly(inputStream);
}
Is my file saving logic correct?
No.
if (!fileToCreate.exists()) {
fileToCreate.mkdirs();
}
Here you are creating every element in fileToCreate as a directory, including the final element. So trying to open it later as a file will fail. And the exists() test is pointless. It should be:
fileToCreate.getParentFile().mkdirs();
if in scenario 2 and my app falls over, would I see an exception stating so
Yes, provided you print or log it somewhere. The method will definitely throw one.

write to properties file on specific line

Please let me know if this question has been asked before.
The Goal
In my android application when a user launches the App it loads the Login.class first. This class checks to see if a local file (in the included file path of the App) called app_prefs.prop exists (which it does) and then it checks the following fields structured like so:
username=
user_hash=
saved_email=
email_hash=
Those fields are blank by default just like so. I am reading them using the following code:
public static String getConfigValue(Context context, String name) {
Resources resources = context.getResources();
String TAG = "Retrieve";
try {
InputStream rawResource = resources.openRawResource(R.raw.app_prefs);
Properties properties = new Properties();
properties.load(rawResource);
return properties.getProperty(name);
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Unable to find the config file: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "Failed to open config file.");
}
return null;
}
If they return empty values, which by default they will, then the login screen is showed. If they do not, the login is attempted by default and if successful it will continue to the App, if not, login is shown again of course.
The Issue
When they sign in, I want to write the data into those fields. Currently its being sent to and from the server using JSON and works awesome. I am able to extract this data as well to a string variable which I am then passing to my save to config file after logging the user in but before continuing to the next App screen. This is where the problem lies, I have enabled the permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I have also passed all the values to it, but they are not being written to the lines I want them too. Here is the code I am using to write the file with:
private void commitUserInfotoFile(Context context, String username, String passhash, String rmemail, String rmemailhash) {
Resources resources = context.getResources();
String TAG = "Store";
try {
InputStream rawResource = resources.openRawResource(R.raw.app_prefs);
Properties properties = new Properties();
properties.load(rawResource);
//tried using setProperty as well as put but neither works
properties.setProperty("username", username);
properties.put("username", username);
properties.setProperty("user_hash", passhash);
properties.setProperty("saved_email", rmemail);
properties.setProperty("email_hash", rmemailhash);
Log.e(TAG, "Wrote the values to the stored file");
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Unable to find the config file: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IO Exception loading file.");
}
}
Yet its not storing those values to the file even tho I get the message Wrote the values to the stored file in my console. I am a little confused as to the writing of properties using this method so any help would be appreciated. Thanks
You never store the result of your edits back into the resource. setProperty() just updates some internal key-value pair in the Properties object, it does not update it's source. You need to call Properties.store(OutputStream, String) when you are done with your edits. See here:
https://developer.android.com/reference/java/util/Properties.html#store(java.io.OutputStream, java.lang.String)

How to watch file for new content and retrieve that content

I have a file with name foo.txt. This file contains some text. I want to achieve following functionality:
I launch program
write something to the file (for example add one row: new string in foo.txt)
I want to get ONLY NEW content of this file.
Can you clarify the best solution of this problem? Also I want resolve related issues: in case if I modify foo.txt I want to see diff.
The closest tool which I found in Java is WatchService but if I understood right this tool can only detect type of event happened on filesystem (create file or delete or modify).
Java Diff Utils is designed for that purpose.
final List<String> originalFileContents = new ArrayList<String>();
final String filePath = "C:/Users/BackSlash/Desktop/asd.txt";
FileListener fileListener = new FileListener() {
#Override
public void fileDeleted(FileChangeEvent paramFileChangeEvent)
throws Exception {
// use this to handle file deletion event
}
#Override
public void fileCreated(FileChangeEvent paramFileChangeEvent)
throws Exception {
// use this to handle file creation event
}
#Override
public void fileChanged(FileChangeEvent paramFileChangeEvent)
throws Exception {
System.out.println("File Changed");
//get new contents
List<String> newFileContents = new ArrayList<String> ();
getFileContents(filePath, newFileContents);
//get the diff between the two files
Patch patch = DiffUtils.diff(originalFileContents, newFileContents);
//get single changes in a list
List<Delta> deltas = patch.getDeltas();
//print the changes
for (Delta delta : deltas) {
System.out.println(delta);
}
}
};
DefaultFileMonitor monitor = new DefaultFileMonitor(fileListener);
try {
FileObject fileObject = VFS.getManager().resolveFile(filePath);
getFileContents(filePath, originalFileContents);
monitor.addFile(fileObject);
monitor.start();
} catch (InterruptedException ex) {
ex.printStackTrace();
} catch (FileNotFoundException e) {
//handle
e.printStackTrace();
} catch (IOException e) {
//handle
e.printStackTrace();
}
Where getFileContents is :
void getFileContents(String path, List<String> contents) throws FileNotFoundException, IOException {
contents.clear();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
contents.add(line);
}
}
What I did:
I loaded the original file contents in a List<String>.
I used Apache Commons VFS to listen for file changes, using FileMonitor. You may ask, why? Because WatchService is only available starting from Java 7, while FileMonitor works with at least Java 5 (personal preference, if you prefer WatchService you can use it). note: Apache Commons VFS depends on Apache Commons Logging, you'll have to add both to your build path in order to make it work.
I created a FileListener, then I implemented the fileChanged method.
That method load new contents form the file, and uses Patch.diff to retrieve all differences, then prints them
I created a DefaultFileMonitor, which basically listens for changes to a file, and I added my file to it.
I started the monitor.
After the monitor is started, it will begin listening for file changes.

Categories

Resources