Refactoring java code

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
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Amitkumar PSr. ConsultantCommented:
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.
Rohit BajajAuthor 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 ?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.