Refactoring java code

Rohit Bajaj
Rohit Bajaj used Ask the Experts™
on
HI,
I have the following code in my spring mvc controller i want to refactor it .
 private String getBase64EncodedIframeSrc(String body) {
        String contentCss = "<link href=\"https://cdn.ckeditor.com/4.5.6/full/contents.css\" 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;
    }

Open in new window


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 :
 @ExceptionHandler(EmptyResultDataAccessException.class)
    public ResponseEntity<String> handleServerException(EmptyResultDataAccessException e) {
        String message = "Page not found";
        logger.error(message, e);
        return new ResponseEntity<String>(message, HttpStatus.NOT_FOUND);
    }

Open in new window


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

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
So UTF-8 is a character encoding that Java has specified that ALL implementations MUST support, so one option is to just catch the exception and ignore it, because it will never occur. If you would rather some cleaner code than that, try the below...

 private String getBase64EncodedIframeSrc(String body) {
        String contentCss = "<link href=\"https://cdn.ckeditor.com/4.5.6/full/contents.css\" 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;
        return Base64.getEncoder().encodeToString(StandardCharsets.UTF_8.encode(iframeSrc).array());
    }

Open in new window

Commented:
If you are using html code in your controller, then I would suggest to have a constants for very common tags or words and refer it in your java file.

Alternately, you can have static methods that will get you the html code based on values passed. For example, getContentCSS(href) or getHTMLBody(body).

About the exception, I dont see any issues as far as you have handled UnsupportedEncodingException in youe base controller. If not, throw a new one which is properly handled in your base controller.

To move out exceptions from a controller, you can use filters that will take care of handling the application. Controller will just need to throw an exception and filter will handle any exception thrown by Controller.

Hope above helps you.

Author

Commented:
Hi mccarl,
As you mentioned :
So UTF-8 is a character encoding that Java has specified that ALL implementations MUST support
Where can i see this on java docs or official site ?
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial