javax.portlet.PortletPreferences Java Examples

The following examples show how to use javax.portlet.PortletPreferences. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PreferenceCommonTest.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
/**
 * Private method that checks if a preference is not defined or has no
 * value in <code>portlet.xml</code>, the default values are returned.
 * @param request  the portlet request.
 * @param preferenceName  the preference name which is not defined or has no
 *        value in <code>portlet.xml</code>.
 * @return the test result.
 */
private TestResult doCheckDefaultPreference(PortletRequest request,
                                            String preferenceName) {
	TestResult result = new TestResult();
	result.setDescription("Ensure proper default is returned when "
			+ "a non-existing/value-undefined preference is requested.");
	result.setSpecPLT("14.1");

	PortletPreferences preferences = request.getPreferences();
	String value =  preferences.getValue(preferenceName, DEF_VALUE);
	String[] values = preferences.getValues(preferenceName,
	                                        new String[] { DEF_VALUE });
	if (DEF_VALUE.equals(value)
			&& values != null && values.length == 1
			&& DEF_VALUE.equals(values[0])) {
		result.setReturnCode(TestResult.PASSED);
	} else if (!DEF_VALUE.equals(value)) {
		TestUtils.failOnAssertion("preference value", value, DEF_VALUE, result);
	} else {
		TestUtils.failOnAssertion("preference values",
		                          values,
		                          new String[] { DEF_VALUE },
		                          result);
	}
	return result;
}
 
Example #2
Source File: IMSBLTIPortlet.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public void processActionReset(String action,ActionRequest request, ActionResponse response)
throws PortletException, IOException {

	// TODO: Check Role
	log.debug("Removing preferences....");
	clearSession(request);
	PortletSession pSession = request.getPortletSession(true);
	PortletPreferences prefs = request.getPreferences();
	try {
		prefs.reset("sakai.descriptor");
		for (String element : fieldList) {
			prefs.reset("imsti."+element);
			prefs.reset("sakai:imsti."+element);
		}
		log.debug("Preference removed");
	} catch (ReadOnlyException e) {
		setErrorMessage(request, rb.getString("error.modify.prefs")) ;
		return;
	}
	prefs.store();

	// Go back to the main edit page
	pSession.setAttribute("sakai.view", "edit");
}
 
Example #3
Source File: PreferenceCommonTest.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
protected TestResult checkSetPreferenceSingleValue(PortletRequest request) {
    TestResult result = new TestResult();
    result.setDescription("Ensure a single preference value can be set.");
    result.setSpecPLT("14.1");

    PortletPreferences preferences = request.getPreferences();
    try {
        preferences.setValue("TEST", "TEST_VALUE");
    } catch (ReadOnlyException ex) {
    	TestUtils.failOnException("Unable to set preference value.", ex, result);
        return result;
    }

    String value = preferences.getValue("TEST", DEF_VALUE);
    if (value != null && value.equals("TEST_VALUE")) {
    	result.setReturnCode(TestResult.PASSED);
    } else {
    	TestUtils.failOnAssertion("preference value", value, "TEST_VALUE", result);
    }
    return result;
}
 
Example #4
Source File: PreferenceCommonTest.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
protected TestResult checkSetPreferenceNull(PortletRequest request) {
    TestResult result = new TestResult();
    result.setDescription("Ensure a preference value can be set to null.");
    result.setSpecPLT("14.1");

    PortletPreferences preferences = request.getPreferences();
    try {
        preferences.setValue("TEST", null);
    } catch (ReadOnlyException ex) {
    	TestUtils.failOnException("Unable to set preference value.", ex, result);
        return result;
    }

    String value = preferences.getValue("TEST", DEF_VALUE);
    // see PLUTO-609: behavioral change!
    if (null == value) {
        result.setReturnCode(TestResult.PASSED);
    } else {
        TestUtils.failOnAssertion("preference value", value, null, result);
    }
    return result;
}
 
Example #5
Source File: PreferenceCommonTest.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
protected TestResult checkSetPreferencesReturnsFirst(PortletRequest request) {
    TestResult result = new TestResult();
    result.setDescription("Ensure the first value set to a given "
    		+ "preference is returned first by PortletPreferences.getValue().");
    result.setSpecPLT("14.1");

    PortletPreferences preferences = request.getPreferences();
    try {
        preferences.setValues("TEST", new String[] { "FIRST", "SECOND" });
    } catch (ReadOnlyException ex) {
    	TestUtils.failOnException("Unable to set preference values.", ex, result);
    	return result;
    }

    String value = preferences.getValue("TEST", DEF_VALUE);
    if (value != null && value.equals("FIRST")) {
    	result.setReturnCode(TestResult.PASSED);
    } else {
    	TestUtils.failOnAssertion("preference value", value, "FIRST", result);
    }
    return result;
}
 
Example #6
Source File: IMSBLTIPortlet.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public void processActionReset(String action,ActionRequest request, ActionResponse response)
throws PortletException, IOException {

	// TODO: Check Role
	log.debug("Removing preferences....");
	clearSession(request);
	PortletSession pSession = request.getPortletSession(true);
	PortletPreferences prefs = request.getPreferences();
	try {
		prefs.reset("sakai.descriptor");
		for (String element : fieldList) {
			prefs.reset("imsti."+element);
			prefs.reset("sakai:imsti."+element);
		}
		log.debug("Preference removed");
	} catch (ReadOnlyException e) {
		setErrorMessage(request, rb.getString("error.modify.prefs")) ;
		return;
	}
	prefs.store();

	// Go back to the main edit page
	pSession.setAttribute("sakai.view", "edit");
}
 
Example #7
Source File: PreferenceCommonTest.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
protected TestResult checkGetPreferenceNames(PortletRequest request) {
    TestResult result = new TestResult();
    result.setDescription("Ensure returned enumeration is valid.");
    result.setSpecPLT("14.1");

    PortletPreferences preferences = request.getPreferences();
    Map<String, String[]> prefMap = preferences.getMap();
    boolean hasAll = true;
    for (Enumeration<String> en = preferences.getNames(); 
    		en.hasMoreElements(); ) {
        if (!prefMap.containsKey(en.nextElement())) {
            hasAll = false;
            break;
        }
    }

    if (hasAll) {
    	result.setReturnCode(TestResult.PASSED);
    } else {
    	result.setReturnCode(TestResult.FAILED);
    	result.setResultMessage("At least one name is not found "
    			+ "in the preference map.");
    }
    return result;
}
 
Example #8
Source File: PreferenceCommonTest.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
/**
 * Logs out the portlet preferences.
 * @param preferences  PortletPreferences to log.
 */
protected void logPreferences(PortletPreferences preferences) {
	StringBuffer buffer = new StringBuffer();
	Map<String, String[]> map = preferences.getMap();
	for (String key : map.keySet()) {
		String[] values = (String[]) map.get(key);
		buffer.append(key).append("=");
		if (values != null) {
			buffer.append("{");
			for (int i = 0; i < values.length; i++) {
				buffer.append(values[i]);
				if (i < values.length - 1) {
					buffer.append(",");
				}
			}
			buffer.append("}");
		} else {
			// Spec allows null values.
			buffer.append("NULL");
		}
		buffer.append(";");
	}
	LOG.debug("PortletPreferences: " + buffer.toString());
}
 
Example #9
Source File: V2EnvironmentTests_PortletPreferences_Validator.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(PortletPreferences preferences) throws ValidatorException {

  EnvironmentTests_PortletPreferences_ApiAction.tr32_success = true;
  EnvironmentTests_PortletPreferences_ApiEvent_event.tr32_success = true;
  EnvironmentTests_PortletPreferences_ApiResource.tr32_success = true;
  
  if (preferences.getValue("tr33", "true").equals("false")) {
    ArrayList<String> al = new ArrayList<String>();
    al.add("tr33");
    al.add("tr34");
    throw new ValidatorException("ValidatorException for tr33 and tr34", al);
  }
}
 
Example #10
Source File: PreferenceCommonTest.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
protected TestResult checkGetPreferences(PortletRequest request) {
    TestResult result = new TestResult();
    result.setDescription("Ensure that preferences defined "
    		+ "in the deployment descriptor may be retrieved.");
    result.setSpecPLT("14.1");

    PortletPreferences preferences = request.getPreferences();
    String value = preferences.getValue(PREF_NAME, DEF_VALUE);
    if (value != null && value.equals(PREF_VALUE)) {
    	result.setReturnCode(TestResult.PASSED);
    } else {
    	TestUtils.failOnAssertion("preference value", value, PREF_VALUE, result);
    }
    return result;
}
 
Example #11
Source File: PreferenceCommonTest.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
protected TestResult checkSetPreferenceMultiValues(PortletRequest request) {
    TestResult result = new TestResult();
    result.setDescription("Ensure multiple preference values can be set.");
    result.setSpecPLT("14.1");

    PortletPreferences preferences = request.getPreferences();
    try {
        preferences.setValues("TEST", new String[] {"ONE", "ANOTHER"});
    } catch (ReadOnlyException ex) {
    	TestUtils.failOnException("Unable to set preference values.", ex, result);
    	return result;
    }

    String[] values = preferences.getValues("TEST", new String[] { DEF_VALUE });
    if (values != null && values.length == 2
    		&& values[0].equals("ONE") && values[1].equals("ANOTHER")) {
    	result.setReturnCode(TestResult.PASSED);
    } else if (values == null) {
    	TestUtils.failOnAssertion("preference values",
    	                          values,
    	                          new String[] { "ONE", "ANOTHER" },
    	                          result);
    } else if (values.length != 2) {
    	TestUtils.failOnAssertion("length of preference values",
    	                          String.valueOf(values.length),
    	                          String.valueOf(2),
    	                          result);
    } else {
    	TestUtils.failOnAssertion("preference values",
    	                          values,
    	                          new String[] { "ONE", "ANOTHER" },
    	                          result);
    }
    return result;
}
 
Example #12
Source File: PreferenceCommonTest.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
/**
 * Check (xci) SPEC 91, PLT 14.1: Preferences values are not modified
 * if the values in the Map are altered.
 */
protected TestResult checkPreferenceValueNotModified(PortletRequest request) {
	TestResult result = new TestResult();
	result.setDescription("Preferences values are not modified if "
			+ "the values in the returned preference Map are altered.");
	result.setSpecPLT("14.1");

	PortletPreferences preferences = request.getPreferences();
    if (LOG.isDebugEnabled()) {
    	LOG.debug("Original Preferences:");
    	logPreferences(preferences);
    }

    // Modify the returned preference map.
	Map<String, String[]> prefMap = preferences.getMap();
	String[] values = (String[]) prefMap.get(PREF_NAME);
	String originalValue = null;
	String modifiedValue = "Value modified in preferences map.";
	if (values != null && values.length == 1) {
		originalValue = values[0];
		values[0] = modifiedValue;
	}

	// Check if the value held by portlet preferences is modified.
    if (LOG.isDebugEnabled()) {
    	LOG.debug("Modified Preferences:");
    	logPreferences(preferences);
    }
	String newValue = preferences.getValue(PREF_NAME, DEF_VALUE);
	if (newValue != null && newValue.equals(originalValue)) {
		result.setReturnCode(TestResult.PASSED);
	} else {
		result.setReturnCode(TestResult.FAILED);
		result.setResultMessage("Preference value modified according to "
				+ "the preference map.");
	}
	return result;
}
 
Example #13
Source File: Utils.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
public boolean checkEqualPreferences(PortletPreferences injectedPortletPreferences, PortletPreferences requestPreferences){
   if (injectedPortletPreferences.equals(requestPreferences)) {
      return true;
   }
   if(checkEqualEnumeration(injectedPortletPreferences.getNames(), requestPreferences.getNames())
         && injectedPortletPreferences.getMap().equals(requestPreferences.getMap())){
      return true;
   } else {
      return false;
   }
}
 
Example #14
Source File: SaveConfigurationModule.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Service.
 * 
 * @param request the request
 * @param response the response
 * 
 * @throws Exception the exception
 * 
 * @see it.eng.spago.dispatching.action.AbstractHttpAction#service(it.eng.spago.base.SourceBean, it.eng.spago.base.SourceBean)
 */

public void service(SourceBean request, SourceBean response) throws Exception {
       
	List attributes = request.getContainedAttributes();
	PortletRequest portReq = PortletUtilities.getPortletRequest();
	PortletPreferences pref = portReq.getPreferences();
	String prefPrefix = "PORTLET_PREF_";
	Iterator it = attributes.iterator();
	while (it.hasNext()) {
		SourceBeanAttribute attribute = (SourceBeanAttribute) it.next();
		String key = attribute.getKey();
		if (key != null && key.startsWith(prefPrefix)) {
			String prefName = key.substring(prefPrefix.length());
			String prefValues = (String) attribute.getValue();
			String[] values = prefValues.split(",");
			pref.setValues(prefName, values);
		}
	}
	pref.store();
	/*
	SessionContainer session = getRequestContainer().getSessionContainer();
	RequestContainer requestContainer = this.getRequestContainer();
	PortletRequest portReq = PortletUtilities.getPortletRequest();
	PortletPreferences pref = portReq.getPreferences();
	String lang = (String)request.getAttribute("language");
	String[] codesLang = lang.split(",");
	pref.setValues("language", codesLang);
	pref.store();
	SessionContainer permanentContainer = session.getPermanentContainer();
	permanentContainer.setAttribute("AF_LANGUAGE", codesLang[0]);
	permanentContainer.setAttribute("AF_COUNTRY", codesLang[1]);
	*/
}
 
Example #15
Source File: ResourceRequestWrapperChecker.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@Override
public PortletPreferences getPreferences() {
   String meth = "getPreferences";
   Object[] args = {};
   PortletPreferences ret = prefs;
   retVal = ret;
   checkArgs(meth, args);
   return ret;
}
 
Example #16
Source File: PortletRequestWrapperChecker.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@Override
public PortletPreferences getPreferences() {
   String meth = "getPreferences";
   Object[] args = {};
   PortletPreferences ret = prefs;
   retVal = ret;
   checkArgs(meth, args);
   return ret;
}
 
Example #17
Source File: RenderRequestWrapperChecker.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@Override
public PortletPreferences getPreferences() {
   String meth = "getPreferences";
   Object[] args = {};
   PortletPreferences ret = prefs;
   retVal = ret;
   checkArgs(meth, args);
   return ret;
}
 
Example #18
Source File: ActionRequestWrapperChecker.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@Override
public PortletPreferences getPreferences() {
   String meth = "getPreferences";
   Object[] args = {};
   PortletPreferences ret = prefs;
   retVal = ret;
   checkArgs(meth, args);
   return ret;
}
 
Example #19
Source File: EventRequestWrapperChecker.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@Override
public PortletPreferences getPreferences() {
   String meth = "getPreferences";
   Object[] args = {};
   PortletPreferences ret = prefs;
   retVal = ret;
   checkArgs(meth, args);
   return ret;
}
 
Example #20
Source File: PortletRequestImpl.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@Override
public PortletPreferences getPreferences() {
   if (portletPreferences == null) {
      portletPreferences = new PortletPreferencesImpl(getPortletContainer(), getPortletWindow(), this);
   }
   return portletPreferences;
}
 
Example #21
Source File: PortletArtifactProducer.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
/**
 * Producer method for the portlet preferences. 
 */
@Produces @PortletRequestScoped @Named("portletPreferences")
public static PortletPreferences producePortletPreferences() {
   PortletArtifactProducer pap = producers.get();
   assert pap != null;
   return pap.req.getPreferences();
}
 
Example #22
Source File: IMSBLTIPortlet.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public String getCorrectProperty(PortletRequest request, String propName, String defaultValue)
{
	Placement placement = ToolManager.getCurrentPlacement();
	String propertyName = placement.getToolId() + "." + propName;
	String propValue = ServerConfigurationService.getString(propertyName,null);
	if ( propValue != null && propValue.trim().length() > 0 ) {
		log.debug("Sakai.home {}={}", propName, propValue);
		return propValue;
	}

	Properties config = placement.getConfig();
	propValue = getSakaiProperty(config, "imsti."+propName);
	if ( propValue != null && "true".equals(config.getProperty("final."+propName)) )
	{
		log.debug("Frozen {} ={}", propName, propValue);
		return propValue;
	}

	PortletPreferences prefs = request.getPreferences();
	propValue = prefs.getValue("imsti."+propName, null);
	if ( propValue != null ) {
		log.debug("Portlet {} ={}", propName, propValue);
		return propValue;
	}

	propValue = getSakaiProperty(config, "imsti."+propName);
	if ( propValue != null ) {
		log.debug("Tool {} ={}", propName, propValue);
		return propValue;
	}

	if ( defaultValue != null ) {
		log.debug("Default {} ={}", propName, defaultValue);
		return defaultValue;
	}
	log.debug("Fell through {}", propName);
	return null;
}
 
Example #23
Source File: SimpleRSSPortlet.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Helper to check if a preference is locked (ie readonly). This may be set by a channel config to restrict access.
 * 
 * @param request
 * @return
 */
private boolean isPrefLocked(PortletRequest request, String prefName) {
	PortletPreferences prefs = request.getPreferences();
	try {
		return prefs.isReadOnly(prefName);
	} catch (IllegalArgumentException e){
		log.debug("Preference does not exist: " + prefName);
		return false;
	}
}
 
Example #24
Source File: SimpleRSSPortlet.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Helper to check if a preference is locked (ie readonly). This may be set by a channel config to restrict access.
 * 
 * @param request
 * @return
 */
private boolean isPrefLocked(PortletRequest request, String prefName) {
	PortletPreferences prefs = request.getPreferences();
	try {
		return prefs.isReadOnly(prefName);
	} catch (IllegalArgumentException e){
		log.debug("Preference does not exist: " + prefName);
		return false;
	}
}
 
Example #25
Source File: ChannelUtilities.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Gets the preference value.
 *
 * @param requestContainer
 *            the request container
 * @param preferenceName
 *            the preference name
 * @param defaultValue
 *            the default value
 *
 * @return the preference value
 */
public static String getPreferenceValue(RequestContainer requestContainer, String preferenceName, String defaultValue) {
	String prefValue = defaultValue;
	try {
		// get mode of execution
		String channelType = requestContainer.getChannelType();
		String sbiMode = null;
		if ("PORTLET".equalsIgnoreCase(channelType))
			sbiMode = "PORTLET";
		else
			sbiMode = "WEB";
		// based on mode get spago object and url builder
		if (sbiMode.equalsIgnoreCase("WEB")) {
			SourceBean request = requestContainer.getServiceRequest();
			Object prefValueObj = request.getAttribute(preferenceName);
			if (prefValueObj != null)
				prefValue = prefValueObj.toString();
			else
				prefValue = defaultValue;
		} else if (sbiMode.equalsIgnoreCase("PORTLET")) {
			PortletRequest portReq = getPortletRequest();
			PortletPreferences prefs = portReq.getPreferences();
			prefValue = prefs.getValue(preferenceName, defaultValue);
		}
	} catch (Exception e) {
		SpagoBITracer.major(SpagoBIConstants.NAME_MODULE, ChannelUtilities.class.getName(), "getPreferenceValue", "Error while recovering preference value",
				e);
		prefValue = defaultValue;
	}
	return prefValue;
}
 
Example #26
Source File: PetsController.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Registers a PropertyEditor with the data binder for handling Dates
 * using the format as currently specified in the PortletPreferences.
 */
@InitBinder
public void initBinder(PortletRequestDataBinder binder, PortletPreferences preferences) {
	String formatString = preferences.getValue("dateFormat", PetService.DEFAULT_DATE_FORMAT);
	SimpleDateFormat dateFormat = new SimpleDateFormat(formatString);
	binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	binder.setAllowedFields(new String[] {"species", "breed", "name", "birthdate"});
}
 
Example #27
Source File: DateFormatController.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * In the render phase, the current format and available formats will be
 * exposed to the 'dateFormat' view via the model.
 */
@RequestMapping
public String showPreferences(PortletPreferences preferences, Model model) {
	model.addAttribute("currentFormat", preferences.getValue("dateFormat", PetService.DEFAULT_DATE_FORMAT));
	model.addAttribute("availableFormats", this.availableFormats);
	return "dateFormat";
}
 
Example #28
Source File: DateFormatController.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * In the action phase, the dateFormat preference is modified. To persist any
 * modifications, the PortletPreferences must be stored.
 */
@RequestMapping
public void changePreference(PortletPreferences preferences, @RequestParam("dateFormat") String dateFormat)
		throws Exception {

	preferences.setValue("dateFormat", dateFormat);
	preferences.store();
}
 
Example #29
Source File: IMSBLTIPortlet.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public String getCorrectProperty(PortletRequest request, String propName, String defaultValue)
{
	Placement placement = ToolManager.getCurrentPlacement();
	String propertyName = placement.getToolId() + "." + propName;
	String propValue = ServerConfigurationService.getString(propertyName,null);
	if ( propValue != null && propValue.trim().length() > 0 ) {
		log.debug("Sakai.home {}={}", propName, propValue);
		return propValue;
	}

	Properties config = placement.getConfig();
	propValue = getSakaiProperty(config, "imsti."+propName);
	if ( propValue != null && "true".equals(config.getProperty("final."+propName)) )
	{
		log.debug("Frozen {} ={}", propName, propValue);
		return propValue;
	}

	PortletPreferences prefs = request.getPreferences();
	propValue = prefs.getValue("imsti."+propName, null);
	if ( propValue != null ) {
		log.debug("Portlet {} ={}", propName, propValue);
		return propValue;
	}

	propValue = getSakaiProperty(config, "imsti."+propName);
	if ( propValue != null ) {
		log.debug("Tool {} ={}", propName, propValue);
		return propValue;
	}

	if ( defaultValue != null ) {
		log.debug("Default {} ={}", propName, defaultValue);
		return defaultValue;
	}
	log.debug("Fell through {}", propName);
	return null;
}
 
Example #30
Source File: RegisterAction.java    From journaldev with MIT License 4 votes vote down vote up
public PortletPreferences getPreferences() {
	return preferences;
}