Avatar of Rohit Bajaj
Rohit Bajaj
Flag for India asked on

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
JavaJava EE

Avatar of undefined
Last Comment
mccarl

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
mccarl

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Am P

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Rohit Bajaj

ASKER
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 ?
SOLUTION
mccarl

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes