troubleshooting Question

Refactoring java code

Avatar of Rohit Bajaj
Rohit BajajFlag for India asked on
JavaJava EE
4 Comments3 Solutions155 ViewsLast Modified:
I have the following code in my spring mvc controller i want to refactor it .
 private String getBase64EncodedIframeSrc(String body) {
        String contentCss = "<link href=\"\" rel=\"stylesheet\" type=\"text/css\"/>";
        String htmlBody = "<body><div id=\"textArea\" class=\"cke_editable cke_editable_themed cke_contents_ltr cke_show_borders\">" + body + "</div><body>";
        String iframeSrc = contentCss + htmlBody;
        String base64encodedString= null;
        try {
            base64encodedString = Base64.getEncoder().encodeToString(iframeSrc.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            logger.error("Exception while encoding iframe source in base64",e.getMessage());
        return base64encodedString;

Please help me write it in a better way.
Things that i want to avoid :
1) I have manually put \" at many places in both the strings contentCss and htmlBody
2) Handling of exception. Is it possible to move it out of controller?

I am extending my controller with a basecontroller where i am handling other exceptions like :
    public ResponseEntity<String> handleServerException(EmptyResultDataAccessException e) {
        String message = "Page not found";
        logger.error(message, e);
        return new ResponseEntity<String>(message, HttpStatus.NOT_FOUND);

But this approach will not work here...
Any other possible suggestions for refactoring and making it better ?

IT Business Systems Analyst / Software Developer
Join our community to see this answer!
Unlock 3 Answers and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros