I have try to insert user-defined css file into my template.
Model:
@Entity
public class MyModel extends Model {
// Some code stuff...
/** User-defined css file. */
public Blob stylesheet;
}
Controller
public class MyController extends Controller {
// Some code stuff...
/**
* Displays user-defined css file for specified instance of MyModel.
*/
public static void displayStylesheet(Long id) {
final MyModel myModel = MyModel.findById(id);
notFoundIfNull(myModel);
response.setContentTypeIfNotSet(myModel.stylesheet.type());
if (myModel.stylesheet.exists()) {
renderBinary(myModel.stylesheet.getFile());
}
}
/**
* Displays page, that contains user-defined css file
* for specified instance of MyModel.
*/
public static void index(Long id) {
render(id);
}
}
View
#{extends 'main.html' /}
#{set 'styles'}
<link rel="stylesheet" href="@{MyController.displayStylesheet(id)}" />
#{/set}
<p>This page contains user-defined stylesheet.</p>
When I try to display stylesheet by GET request all working fine:
http://localhost:9000/mycontroller/displaystylesheet?id=1188
But FireBug or Google Chrome developer`s panel are not displays this style like sourced.
Updated:
I found the solution, but it is not so beautyfull. Code in the controller:
/**
* Displays page, that contains user-defined css file
* for specified instance of MyModel.
*/
public static void index(Long id) {
final MyModel myModel = MyModel.findById(id);
String styles = "";
File stylesheet = myModel.stylesheet.getFile();
try {
FileInputStream stream = new FileInputStream(stylesheet);
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
styles = Charset.defaultCharset().decode(bb).toString();
}
finally {
stream.close();
}
}
catch (IOException ioe){
throw new RuntimeException(ioe);
}
render(id, styles);
}
It will put all styles into the one string styles. Then we can render it in the template:
<style type="text/css">
${styles}
</style>
May be someone can suggest more beautyfull solution?