I have a mule program that I want to read a file from my PC and display the content in the browser on localhost.
I have it working for one file hard coded as seen below.
public class ReadFile extends AbstractMessageTransformer {
/**
* loads the content of the file specified in the parameter
*
* @param filename
* the name of the file
* @return the content of the file
*/
public String readFile(String filename) {
File file;
file = new File("O:\\test.txt");
StringBuilder builder = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null)
builder.append(line);
} catch (IOException e) {
e.printStackTrace();
} finally {
closeQuietly(reader);
}
return builder.toString();
}
public String getFileName(MuleMessage message) {
Path p = Paths.get("O:\\test.txt");
String file = p.getFileName().toString();
return file;
}
public String setPayload(MuleMessage message, String outputEncoding) {
message.setPayload(outputEncoding);
return null;
}
private void closeQuietly(Closeable c) {
if (c != null) {
try {
c.close();
} catch (IOException ignored) {
}
}
}
@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
String filename = getFileName(message);
String content = readFile(filename);
setPayload(message, content);
return message;
}
}
How can i specify it to read any file I input through the URL and not just my hard coded file?