try
{
String componentEditorUrl = CmsPropertyHandler.getComponentEditorUrl();
InfoGluePrincipal principal = templateController.getPrincipal();
String cmsUserName = (String)templateController.getHttpServletRequest().getSession().getAttribute("cmsUserName");
if(cmsUserName != null && !CmsPropertyHandler.getAnonymousUser().equalsIgnoreCase(cmsUserName))
principal = templateController.getPrincipal(cmsUserName);
//Locale locale = templateController.getLocale();
Locale locale = templateController.getLocaleAvailableInTool(principal);
boolean hasAccessToAccessRights = AccessRightController.getController().getIsPrincipalAuthorized(templateController.getDatabase(), principal, "ComponentEditor.ChangeSlotAccess", "");
boolean hasAccessToAddComponent = AccessRightController.getController().getIsPrincipalAuthorized(templateController.getDatabase(), principal, "ComponentEditor.AddComponent", "" + component.getContentId() + "_" + component.getSlotName());
boolean hasAccessToDeleteComponent = AccessRightController.getController().getIsPrincipalAuthorized(templateController.getDatabase(), principal, "ComponentEditor.DeleteComponent", "" + component.getContentId() + "_" + component.getSlotName());
boolean hasAccessToChangeComponent = AccessRightController.getController().getIsPrincipalAuthorized(templateController.getDatabase(), principal, "ComponentEditor.ChangeComponent", "" + component.getContentId() + "_" + component.getSlotName());
boolean hasSaveTemplateAccess = AccessRightController.getController().getIsPrincipalAuthorized(templateController.getDatabase(), principal, "StructureTool.SaveTemplate", "");
boolean hasSubmitToPublishAccess = AccessRightController.getController().getIsPrincipalAuthorized(templateController.getDatabase(), principal, "ComponentEditor.SubmitToPublish", "");
boolean hasPageStructureAccess = AccessRightController.getController().getIsPrincipalAuthorized(templateController.getDatabase(), principal, "ComponentEditor.PageStructure", "");
boolean hasOpenInNewWindowAccess = AccessRightController.getController().getIsPrincipalAuthorized(templateController.getDatabase(), principal, "ComponentEditor.OpenInNewWindow", "");
boolean hasViewSourceAccess = AccessRightController.getController().getIsPrincipalAuthorized(templateController.getDatabase(), principal, "ComponentEditor.ViewSource", "");
String extraHeader = FileHelper.getFileAsString(new File(CmsPropertyHandler.getContextRootPath() + (CmsPropertyHandler.getContextDiskPath().endsWith("/") ? "" : "/") + "preview/ajax/pageComponentEditorHeader.vm"), "iso-8859-1");
//String extraBody = FileHelper.getFileAsString(new File(CmsPropertyHandler.getContextRootPath() + (CmsPropertyHandler.getContextDiskPath().endsWith("/") ? "" : "/") + "preview/ajax/pageComponentEditorBody.vm"), "iso-8859-1");
String extraBody = "";
boolean oldUseFullUrl = this.getTemplateController().getDeliveryContext().getUseFullUrl();
this.getTemplateController().getDeliveryContext().setUseFullUrl(true);
String parameters = "repositoryId=" + templateController.getSiteNode().getRepositoryId() + "&siteNodeId=" + templateController.getSiteNodeId() + "&languageId=" + templateController.getLanguageId() + "&contentId=" + templateController.getContentId() + "&componentId=" + this.getRequest().getParameter("activatedComponentId") + "&componentContentId=" + this.getRequest().getParameter("componentContentId") + "&showSimple=false&showLegend=false&originalUrl=" + URLEncoder.encode(this.getTemplateController().getCurrentPageUrl(), "UTF-8");
String WYSIWYGEditorFile = "ckeditor/ckeditor.js";
if(!CmsPropertyHandler.getPrefferedWYSIWYG().equals("ckeditor4"))
WYSIWYGEditorFile = "FCKEditor/fckeditor.js";
StringBuffer path = getPagePathAsCommaseparatedIds(templateController);
extraHeader = extraHeader.replaceAll("\\$\\{focusElementId\\}", "" + this.getRequest().getParameter("focusElementId"));
extraHeader = extraHeader.replaceAll("\\$\\{contextName\\}", this.getRequest().getContextPath());
extraHeader = extraHeader.replaceAll("\\$\\{componentEditorUrl\\}", componentEditorUrl);
if(principal.getName().equalsIgnoreCase(CmsPropertyHandler.getAnonymousUser()))
extraHeader = extraHeader.replaceAll("\\$\\{limitedUserWarning\\}", "alert('Your session must have expired as you are now in decorated mode as " + principal.getName() + ". Please close browser and login again.');");
else
extraHeader = extraHeader.replaceAll("\\$\\{limitedUserWarning\\}", "");
//extraHeader = extraHeader.replaceAll("\\$\\{currentUrl\\}", URLEncoder.encode(this.getTemplateController().getCurrentPageUrl(), "UTF-8"));
extraHeader = extraHeader.replaceAll("\\$\\{currentUrl\\}", URLEncoder.encode(this.getTemplateController().getOriginalFullURL(), "UTF-8"));
extraHeader = extraHeader.replaceAll("\\$\\{activatedComponentId\\}", "" + this.getRequest().getParameter("activatedComponentId"));
extraHeader = extraHeader.replaceAll("\\$\\{componentContentId\\}", "" + this.getRequest().getParameter("componentContentId"));
extraHeader = extraHeader.replaceAll("\\$\\{parameters\\}", parameters);
extraHeader = extraHeader.replaceAll("\\$\\{siteNodeId\\}", "" + templateController.getSiteNodeId());
extraHeader = extraHeader.replaceAll("\\$\\{languageId\\}", "" + templateController.getLanguageId());
extraHeader = extraHeader.replaceAll("\\$\\{contentId\\}", "" + templateController.getContentId());
extraHeader = extraHeader.replaceAll("\\$\\{metaInfoContentId\\}", "" + templateController.getMetaInformationContentId());
extraHeader = extraHeader.replaceAll("\\$\\{parentSiteNodeId\\}", "" + templateController.getSiteNode().getParentSiteNodeId());
extraHeader = extraHeader.replaceAll("\\$\\{repositoryId\\}", "" + templateController.getSiteNode().getRepositoryId());
extraHeader = extraHeader.replaceAll("\\$\\{path\\}", "" + path.substring(1));
extraHeader = extraHeader.replaceAll("\\$\\{userPrefferredLanguageCode\\}", "" + CmsPropertyHandler.getPreferredLanguageCode(principal.getName()));
extraHeader = extraHeader.replaceAll("\\$\\{userPrefferredWYSIWYG\\}", "" + CmsPropertyHandler.getPrefferedWYSIWYG());
extraHeader = extraHeader.replaceAll("\\$\\{WYSIWYGEditorJS\\}", WYSIWYGEditorFile);
extraHeader = extraHeader.replaceAll("\\$\\{publishedLabel\\}", getLocalizedString(locale, "tool.contenttool.state.published"));
StringBuffer skinCSS = new StringBuffer();
String theme = CmsPropertyHandler.getTheme(principal.getName());
if(!theme.equalsIgnoreCase("Default"))
{
skinCSS.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + this.getRequest().getContextPath() + "/css/skins/" + theme + "/componentEditor.css\" />");
String themeFileJQueryUI = CmsPropertyHandler.getThemeFile(theme, "jquery-ui.css");
if(themeFileJQueryUI != null)