We help IT Professionals succeed at work.
Get Started

Refactoring java code

Rohit Bajaj
Rohit Bajaj asked
on
151 Views
Last Modified: 2016-02-23
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
IT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
Unlock 3 Answers and 4 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE