class MultiPartBoundaryBuilder {

    public String getMultiPartContent(HttpServletRequest request, String xmlSct, String jsonPart) {
        String contentTypeHeader = request.getHeader(HttpHeaders.CONTENT_TYPE);

        String boundary = DEFAULT_BOUNDARY;
        if (contentTypeHeader != null
                && contentTypeHeader.contains(MediaType.MULTIPART_FORM_DATA_VALUE)
                && contentTypeHeader.contains(BOUNDARY)) {
            String boundaryValue = contentTypeHeader.substring(contentTypeHeader.indexOf(BOUNDARY) + BOUNDARY.length());
            boundary = boundaryValue.startsWith(BOUNDARY_PREFIX) ? boundaryValue : BOUNDARY_PREFIX + boundaryValue;
        }
        return contentTemplate
                   .replaceAll(BOUNDARY_PLACEHOLDER, boundary)
                   .replaceAll(XMLPART_PLACEHOLDER, xmlSct)
                   .replaceAll(JSONPART_PLACEHOLDER, jsonPart)
                   .trim();
    }

}