1 13 20 package org.jahia.engines.shared; 21 22 import java.net.MalformedURLException ; 23 import java.util.ArrayList ; 24 import java.util.Date ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.Set ; 29 import java.util.Vector ; 30 31 import org.apache.log4j.Logger; 32 import org.jahia.content.ContentFieldKey; 33 import org.jahia.content.ContentPageKey; 34 import org.jahia.content.ObjectLink; 35 import org.jahia.data.containers.JahiaContainer; 36 import org.jahia.data.fields.JahiaField; 37 import org.jahia.data.fields.JahiaFieldDefinitionProperties; 38 import org.jahia.engines.EngineMessages; 39 import org.jahia.engines.JahiaEngine; 40 import org.jahia.engines.JahiaEngineTools; 41 import org.jahia.exceptions.JahiaException; 42 import org.jahia.params.ParamBean; 43 import org.jahia.registries.ServicesRegistry; 44 import org.jahia.services.acl.JahiaBaseACL; 45 import org.jahia.services.htmlparser.ActiveXServerURLCleanupDOMVisitor; 46 import org.jahia.services.htmlparser.ExtractLinksDOMVisitor; 47 import org.jahia.services.htmlparser.JahiaTextContentTidy; 48 import org.jahia.services.pages.ContentPage; 49 import org.jahia.services.sites.JahiaSite; 50 import org.jahia.urls.ContentServletURL; 51 import org.jahia.utils.I18n; 52 import org.jahia.utils.JahiaString; 53 import org.jahia.utils.JahiaTools; 54 55 public class BigText_Field 56 { 57 58 private static Logger logger = Logger.getLogger(BigText_Field.class); 59 60 private static BigText_Field theObject = null; 61 private static final String JSP_FILE = "/jsp/jahia/engines/shared/bigtext_field.jsp"; 62 63 public static final String HTMLEDITOR_VIEW_HELPER_ATTRIBUTE = 64 "org.jahia.engines.shared.BigText_Field.HTMLEditorsViewHelper"; 65 66 71 public static synchronized BigText_Field getInstance() 72 { 73 if (theObject == null) { 74 theObject = new BigText_Field(); 75 } 76 return theObject; 77 } 79 80 81 90 public boolean handleField( ParamBean jParams, Integer modeInt, HashMap engineMap ) 91 throws JahiaException 92 { 93 int mode = modeInt.intValue(); 94 JahiaField theField = (JahiaField) engineMap.get( "theField" ); 95 initHtmlEditorsHelpers(engineMap,jParams); 96 97 logger.debug("handling mode : " + mode + " for field " + theField.getID() + " value=" + theField.getValue()); 98 99 switch (mode) 100 { 101 case (JahiaEngine.LOAD_MODE) : return composeEngineMap( jParams, engineMap, theField ); 102 case (JahiaEngine.UPDATE_MODE) : return getFormData( jParams, engineMap, theField ); 103 case (JahiaEngine.SAVE_MODE) : return saveData( jParams, engineMap, theField ); 104 } 105 return false; 106 } 108 109 110 119 private boolean getFormData( ParamBean jParams, HashMap engineMap, JahiaField theField ) 120 throws JahiaException 121 { 122 boolean out = true; 123 124 String fieldValueChanged = jParams.getRequest().getParameter( 126 "_" + theField.getID() + "_isDirty"); 127 if (fieldValueChanged != null && "false".equals(fieldValueChanged)) 128 return true; 129 130 135 136 String fieldValue = jParams.getRequest().getParameter( "_" + theField.getID()); 137 138 if (fieldValue != null) { 139 140 fieldValue = JahiaTools.replacePattern(fieldValue,"|","|"); 141 if ("ISO-8859-1".equalsIgnoreCase(jParams.settings(). 142 getDefaultResponseBodyEncoding())) { 143 fieldValue = I18n.windows1252ToISO(fieldValue); 152 } 153 String oriFieldValue = fieldValue; 154 155 JahiaSite site = ServicesRegistry.getInstance().getJahiaSitesService() 156 .getSite(theField.getJahiaID()); 157 158 if ( site.getHtmlCleanup()==1 ){ 159 Vector DOMVisitors = ServicesRegistry.getInstance() 160 .getHtmlParserService() 161 .getHtmlDOMVisitors(theField.getJahiaID()); 162 163 164 ExtractLinksDOMVisitor linkExtractionVisitor = null; 165 if (site.getHtmlMarkupFiltering() == 1) { 166 linkExtractionVisitor = new ExtractLinksDOMVisitor(); 167 linkExtractionVisitor.init(site.getID()); 168 DOMVisitors.add(linkExtractionVisitor); 169 } 170 171 String serverURL = jParams.getRequest().getParameter("serverURL"); 172 173 if ( serverURL != null ){ 174 ActiveXServerURLCleanupDOMVisitor rsnfURLVisitor = 175 new ActiveXServerURLCleanupDOMVisitor(serverURL); 176 DOMVisitors.add(rsnfURLVisitor); 177 } 178 179 fieldValue = JahiaTextContentTidy.tidyPreProcessing(fieldValue); 180 fieldValue = ServicesRegistry.getInstance().getHtmlParserService() 181 .parse(fieldValue,DOMVisitors,theField.getSiteID()); 182 fieldValue = JahiaTextContentTidy.tidyPostProcessing(fieldValue); 183 184 if (linkExtractionVisitor != null) { 185 findAndStoreCrossLinks(linkExtractionVisitor, jParams, engineMap); 186 } 187 } 188 189 EngineMessages resultMessages = ServicesRegistry.getInstance().getHtmlParserService() 190 .checkTextParsingErrors(fieldValue); 191 if ( !resultMessages.isEmpty() ){ 192 resultMessages.saveMessages(String.valueOf(theField.getID()) + ".", jParams.getRequest()); 193 fieldValue = oriFieldValue; 194 out = false; 195 } 196 197 if (!fieldValue.toLowerCase().startsWith("<html>")) { 198 fieldValue = "<html>" + fieldValue + "</html>"; 199 } 200 201 } else { 202 fieldValue = theField.getValue(); 203 } 204 fieldValue = JahiaString.getProperStr(fieldValue, jParams.getLocale()); 205 theField.setValue( fieldValue ); 206 theField.setObject(null); 207 208 return out; 209 } 211 private void findAndStoreCrossLinks (ExtractLinksDOMVisitor 212 linkExtractionVisitor, ParamBean paramBean, HashMap engineMap) { 213 Set pageXRefs = new HashSet (); 214 Iterator linkIter = linkExtractionVisitor.getDocumentLinks().iterator(); 215 while (linkIter.hasNext()) { 216 String curLink = (String ) linkIter.next(); 217 if (curLink == null || curLink.length() == 0) 218 continue; 219 logger.debug("Found link [" + curLink + "] in bigtext field."); 220 221 String originalURL = ServicesRegistry.getInstance().getURLRewritingService().getOriginalFromRewritten(curLink); 223 if (originalURL == null) { 224 originalURL = curLink; 225 } 226 try { 242 if (ContentServletURL.isContentServletURL(paramBean.getRequest(), originalURL, true)) { 243 ContentServletURL contentServletURL = new ContentServletURL(paramBean.getRequest(), originalURL, true); 247 String siteKey = contentServletURL.getPathInfoParameter("site"); 248 String pageIDStr = contentServletURL.getPathInfoParameter("pid"); 249 String hostName = contentServletURL.getHostName(); 250 if (logger.isDebugEnabled()) 251 logger.debug("Found Jahia URL : site=[" + 252 siteKey + 253 "] pid=[" + 254 pageIDStr + 255 "] hostName=[" + 256 hostName + "]"); 257 JahiaSite site = null; 258 try { 259 if ( (siteKey != null) && (!"".equals(siteKey))) { 260 site = ServicesRegistry.getInstance(). 261 getJahiaSitesService().getSiteByKey(siteKey); 262 } else if ( (hostName != null) && (!"".equals(hostName))) { 263 site = ServicesRegistry.getInstance(). 264 getJahiaSitesService().getSiteByServerName( 265 hostName); 266 } 267 if (site == null) { 268 site = ParamBean.getDefaultSite(); 269 } 270 if (pageIDStr != null) { 271 int pageID = Integer.parseInt(pageIDStr); 273 try { 274 ContentPage contentPage = ContentPage.getPage( 275 pageID); 276 if (contentPage != null) { 277 pageXRefs.add(new Integer (pageID)); 280 } 281 } catch (JahiaException ex) { 282 logger.debug("Page " + pageID + 283 "not found, ignoring in references"); 284 } 285 } 286 } catch (JahiaException je) { 287 logger.warn( 288 "Exception raised while trying to find site from URL : " + 289 contentServletURL + 290 ", we will ignore this URL instead of processing it.", 291 je); 292 } catch (NumberFormatException nfe) { 293 logger.warn("Invalid page number " + pageIDStr + ", ignoring URL " + contentServletURL, nfe); 294 } 295 } 296 } catch (MalformedURLException mue) { 297 logger.warn("Error in parsed URL, ignoring", mue); 298 } 299 } 300 engineMap.put("pageXRefs", pageXRefs); 301 } 302 303 304 305 314 private boolean saveData( ParamBean jParams, HashMap engineMap, JahiaField theField ) 315 throws JahiaException 316 { 317 318 Set pageXRefs = (Set ) engineMap.get("pageXRefs"); 322 if ((pageXRefs != null) && (pageXRefs.size() > 0)) { 323 324 ContentFieldKey fieldKey = new ContentFieldKey(theField.getID()); 325 326 ArrayList references = ObjectLink.findByTypeAndLeftObjectKey("hardcodedLink", fieldKey); 328 Iterator refIterator = references.iterator(); 329 while (refIterator.hasNext()) { 330 ObjectLink curLink = (ObjectLink) refIterator.next(); 331 curLink.remove(); 332 } 333 334 Iterator pageXRefIter = pageXRefs.iterator(); 335 while (pageXRefIter.hasNext()) { 336 Integer curPageIDInt = (Integer ) pageXRefIter.next(); 337 logger.debug("Found reference to page " + curPageIDInt + " in this bigtext"); 338 ObjectLink.createLink(fieldKey, 339 new ContentPageKey(curPageIDInt.intValue()), "hardcodedLink", 1, 340 new Date (), 341 "root:0", new Date (), "root:0", new HashMap (), 342 new HashMap (), new HashMap ()); 343 } 344 } 345 346 return theField.save(jParams); 347 348 } 350 351 352 361 private boolean composeEngineMap( ParamBean jParams, HashMap engineMap, JahiaField theField ) 362 throws JahiaException 363 { 364 365 boolean editable = false; 366 JahiaContainer theContainer = (JahiaContainer)engineMap.get("theContainer"); 367 if ( theContainer == null ){ 368 editable = true; 370 } else { 371 HashMap ctnListFieldAcls = (HashMap ) engineMap 372 .get("ctnListFieldAcls"); 373 374 if (theContainer.getListID() != 0 && ctnListFieldAcls != null 375 && ctnListFieldAcls.size() > 0) { 376 JahiaBaseACL acl = JahiaEngineTools.getCtnListFieldACL( 377 ctnListFieldAcls, theField.getID()); 378 if (acl != null) { 379 editable = acl.getPermission(jParams.getUser(), 380 JahiaBaseACL.WRITE_RIGHTS, true); 381 } 382 } else { 383 editable = true; 384 } 385 } 386 387 String output = ""; 388 String forward = theField.getDefinition() 389 .getProperty(JahiaFieldDefinitionProperties.FIELD_UPDATE_JSP_FILE_PROP); 390 if ( forward == null ){ 391 forward = JSP_FILE; 392 if ( !editable ){ 393 forward = JahiaEngine.NOT_EDITABLE_FIELD; 394 } 395 } 396 397 initHtmlEditorsHelpers(engineMap,jParams); 398 399 if ( editable ){ 400 String fieldValue = theField.getValue(); 401 if ( fieldValue == null ){ 402 fieldValue = ""; 403 } 404 405 430 435 theField.setValue(fieldValue); 436 output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, forward ); 437 } else { 438 output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, forward ); 439 } 440 engineMap.put( "fieldForm", output ); 441 442 return true; 443 } 445 446 private void initHtmlEditorsHelpers(HashMap engineMap, ParamBean jParams) 447 throws JahiaException { 448 449 HtmlEditorsViewHelper heViewHelper = (HtmlEditorsViewHelper) 450 engineMap.get(BigText_Field.HTMLEDITOR_VIEW_HELPER_ATTRIBUTE); 451 if ( heViewHelper == null ){ 452 heViewHelper = new HtmlEditorsViewHelper(); 453 heViewHelper.loadHtmlEditors(jParams.getSiteID(),jParams.getRequest()); 454 engineMap.put(BigText_Field.HTMLEDITOR_VIEW_HELPER_ATTRIBUTE, 455 heViewHelper); 456 } 457 String selectedHtmlEditor = 459 jParams.getRequest().getParameter("htmlEditor"); 460 if ( selectedHtmlEditor != null ){ 461 heViewHelper.setDefaultEditorID(selectedHtmlEditor); 462 } 463 464 String selectedCSS = 466 jParams.getRequest().getParameter("htmlEditorCSS"); 467 if ( selectedCSS != null ){ 468 heViewHelper.setDefaultCSSID(selectedCSS); 469 } 470 471 } 472 473 474 } | Popular Tags |