use of er.extensions.foundation.ERXMutableURL in project wonder-slim by undur.
the class AjaxUpdateLink method onClick.
public String onClick(WOContext context, boolean generateFunctionWrapper) {
WOComponent component = context.component();
NSMutableDictionary options = createAjaxOptions(component);
StringBuilder onClickBuffer = new StringBuilder();
String onClick = (String) valueForBinding("onClick", component);
String onClickBefore = (String) valueForBinding("onClickBefore", component);
String updateContainerID = AjaxUpdateContainer.updateContainerID(this, component);
String functionName = (String) valueForBinding("functionName", component);
String function = (String) valueForBinding("function", component);
String replaceID = (String) valueForBinding("replaceID", component);
// PROTOTYPE EFFECTS
AjaxUpdateLink.addEffect(options, (String) valueForBinding("effect", component), updateContainerID, (String) valueForBinding("effectDuration", component));
String afterEffectID = (String) valueForBinding("afterEffectID", component);
if (afterEffectID == null) {
afterEffectID = AjaxUpdateContainer.currentUpdateContainerID();
if (afterEffectID == null) {
afterEffectID = updateContainerID;
}
}
// PROTOTYPE EFFECTS
AjaxUpdateLink.addEffect(options, (String) valueForBinding("afterEffect", component), afterEffectID, (String) valueForBinding("afterEffectDuration", component));
// PROTOTYPE EFFECTS
String beforeEffect = (String) valueForBinding("beforeEffect", component);
WOAssociation directActionNameAssociation = (WOAssociation) associations().valueForKey("directActionName");
if (beforeEffect == null && updateContainerID != null && directActionNameAssociation == null && replaceID == null && function == null && onClick == null && onClickBefore == null) {
NSDictionary nonDefaultOptions = AjaxUpdateContainer.removeDefaultOptions(options);
onClickBuffer.append("AUL.");
if (generateFunctionWrapper) {
onClickBuffer.append("updateFunc");
} else {
onClickBuffer.append("update");
}
onClickBuffer.append("('");
onClickBuffer.append(updateContainerID);
onClickBuffer.append("', ");
AjaxOptions.appendToBuffer(nonDefaultOptions, onClickBuffer, context);
onClickBuffer.append(", '");
onClickBuffer.append(context.contextID());
onClickBuffer.append('.');
onClickBuffer.append(context.elementID());
onClickBuffer.append('\'');
// if (generateFunctionWrapper) {
// onClickBuffer.append(", additionalParams");
// }
onClickBuffer.append(')');
onClickBuffer.append(';');
} else {
if (generateFunctionWrapper) {
onClickBuffer.append("function(additionalParams) {");
}
if (onClickBefore != null) {
onClickBuffer.append("if (");
onClickBuffer.append(onClickBefore);
onClickBuffer.append(") {");
}
// PROTOTYPE EFFECTS
if (beforeEffect != null) {
onClickBuffer.append("new ");
onClickBuffer.append(AjaxUpdateLink.fullEffectName(beforeEffect));
onClickBuffer.append("('");
String beforeEffectID = (String) valueForBinding("beforeEffectID", component);
if (beforeEffectID == null) {
beforeEffectID = AjaxUpdateContainer.currentUpdateContainerID();
if (beforeEffectID == null) {
beforeEffectID = updateContainerID;
}
}
onClickBuffer.append(beforeEffectID);
onClickBuffer.append("', { ");
String beforeEffectDuration = (String) valueForBinding("beforeEffectDuration", component);
if (beforeEffectDuration != null) {
onClickBuffer.append("duration: ");
onClickBuffer.append(beforeEffectDuration);
onClickBuffer.append(", ");
}
onClickBuffer.append("queue:'end', afterFinish: function() {");
}
String actionUrl = null;
if (directActionNameAssociation != null) {
actionUrl = context._directActionURL((String) directActionNameAssociation.valueInComponent(component), ERXComponentUtilities.queryParametersInComponent(associations(), component), ERXRequest.isRequestSecure(context.request()), 0, false).replaceAll("&", "&");
} else {
actionUrl = AjaxUtils.ajaxComponentActionUrl(context);
}
if (replaceID != null) {
try {
ERXMutableURL tempActionUrl = new ERXMutableURL(actionUrl);
tempActionUrl.addQueryParameter(ERXAjaxApplication.KEY_REPLACED, "true");
actionUrl = tempActionUrl.toExternalForm();
} catch (MalformedURLException e) {
throw NSForwardException._runtimeExceptionForThrowable(e);
}
}
actionUrl = "'" + actionUrl + "'";
if (functionName != null) {
actionUrl = actionUrl + ".addQueryParameters(additionalParams)";
}
if (function != null) {
onClickBuffer.append("return " + function + "(" + actionUrl + ")");
} else {
// PROTOTYPE FUNCTIONS
if (replaceID == null) {
if (updateContainerID == null) {
onClickBuffer.append("new Ajax.Request(" + actionUrl + ", ");
AjaxOptions.appendToBuffer(options, onClickBuffer, context);
onClickBuffer.append(')');
} else {
onClickBuffer.append("new Ajax.Updater('" + updateContainerID + "', " + actionUrl + ", ");
AjaxOptions.appendToBuffer(options, onClickBuffer, context);
onClickBuffer.append(')');
}
} else {
onClickBuffer.append("new Ajax.Updater('" + replaceID + "', " + actionUrl + ", ");
AjaxOptions.appendToBuffer(options, onClickBuffer, context);
onClickBuffer.append(')');
}
}
if (onClick != null) {
onClickBuffer.append(';');
onClickBuffer.append(onClick);
}
if (beforeEffect != null) {
onClickBuffer.append("}});");
}
if (onClickBefore != null) {
onClickBuffer.append('}');
}
if (generateFunctionWrapper) {
onClickBuffer.append('}');
}
}
return onClickBuffer.toString();
}
use of er.extensions.foundation.ERXMutableURL in project wonder-slim by undur.
the class AjaxSocialNetwork method _submissionUrl.
protected String _submissionUrl(String baseUrl, String urlKey, String targetUrl, String titleKey, String title, NSDictionary<String, String> additionalParams) {
try {
ERXMutableURL url = new ERXMutableURL(baseUrl);
url.setQueryParameter(urlKey, targetUrl);
if (titleKey != null && title != null) {
url.setQueryParameter(titleKey, title);
}
if (additionalParams != null) {
url.addQueryParameters(additionalParams);
}
return url.toString();
} catch (Throwable t) {
throw new RuntimeException("Failed to create a URL for '" + baseUrl + "' with the targetUrl '" + targetUrl + "'");
}
}
use of er.extensions.foundation.ERXMutableURL in project wonder-slim by undur.
the class ERXApplication method stopPreviousDevInstance.
/**
* Terminates a different instance of the same application that may already be running, only in dev mode.
* Set the property "er.extensions.ERXApplication.allowMultipleDevInstances" to "true" if you need to run multiple dev instances.
*
* @return true if a previously running instance was stopped.
*/
private static boolean stopPreviousDevInstance() {
if (!isDevelopmentModeSafe() || ERXProperties.booleanForKeyWithDefault("er.extensions.ERXApplication.allowMultipleDevInstances", false)) {
return false;
}
if (!(application().wasMainInvoked())) {
return false;
}
try {
ERXMutableURL adapterUrl = new ERXMutableURL(application().cgiAdaptorURL());
if (application().host() == null) {
adapterUrl.setHost("localhost");
}
adapterUrl.appendPath(application().name() + application().applicationExtension());
if (application().isDirectConnectEnabled()) {
adapterUrl.setPort((Integer) application().port());
} else {
adapterUrl.appendPath("-" + application().port());
}
adapterUrl.appendPath(application().directActionRequestHandlerKey() + "/stop");
URL url = adapterUrl.toURL();
log.debug("Stopping previously running instance of " + application().name());
URLConnection connection = url.openConnection();
connection.getContent();
Thread.sleep(2000);
return true;
} catch (Throwable e) {
e.printStackTrace();
}
return false;
}
use of er.extensions.foundation.ERXMutableURL in project wonder-slim by undur.
the class JSONRequestHandler method handleRequest.
@SuppressWarnings("unchecked")
@Override
public WOResponse handleRequest(WORequest request) {
WOApplication application = WOApplication.application();
application.awake();
try {
WOContext context = application.createContextForRequest(request);
WOResponse response = application.createResponseInContext(context);
Object output;
try {
String inputString = request.contentString();
JSONObject input = new JSONObject(inputString);
String sessionIdKey = WOApplication.application().sessionIdKey();
String sessionId = request.cookieValueForKey(sessionIdKey);
if (sessionId == null) {
ERXMutableURL url = new ERXMutableURL();
url.setQueryParameters(request.queryString());
sessionId = url.queryParameter(sessionIdKey);
if (sessionId == null && input.has(sessionIdKey)) {
sessionId = input.getString(sessionIdKey);
}
}
context._setRequestSessionID(sessionId);
WOSession session = null;
if (context._requestSessionID() != null) {
session = WOApplication.application().restoreSessionWithID(sessionId, context);
}
if (session != null) {
session.awake();
}
try {
JSONComponentCallback componentCallback = null;
WODynamicURL url = request._uriDecomposed();
String requestHandlerPath = url.requestHandlerPath();
JSONRPCBridge jsonBridge;
if (requestHandlerPath != null && requestHandlerPath.length() > 0) {
String componentNameAndInstance = requestHandlerPath;
String componentInstance;
String componentName;
int slashIndex = componentNameAndInstance.indexOf('/');
if (slashIndex == -1) {
componentName = componentNameAndInstance;
componentInstance = null;
} else {
componentName = componentNameAndInstance.substring(0, slashIndex);
componentInstance = componentNameAndInstance.substring(slashIndex + 1);
}
if (session == null) {
session = context.session();
}
String bridgesKey = (componentInstance == null) ? "_JSONGlobalBridges" : "_JSONInstanceBridges";
Map<String, JSONRPCBridge> componentBridges = (Map<String, JSONRPCBridge>) session.objectForKey(bridgesKey);
if (componentBridges == null) {
int limit = ERXProperties.intForKeyWithDefault((componentInstance == null) ? "er.ajax.json.globalBacktrackCacheSize" : "er.ajax.json.backtrackCacheSize", WOApplication.application().pageCacheSize());
componentBridges = new LRUMap<>(limit);
session.setObjectForKey(componentBridges, bridgesKey);
}
jsonBridge = componentBridges.get(componentNameAndInstance);
if (jsonBridge == null) {
Class componentClass = _NSUtilities.classWithName(componentName);
JSONComponent component;
if (JSONComponent.class.isAssignableFrom(componentClass)) {
component = (JSONComponent) _NSUtilities.instantiateObject(componentClass, new Class[] { WOContext.class }, new Object[] { context }, true, false);
} else {
throw new SecurityException("There is no JSON component named '" + componentName + "'.");
}
jsonBridge = createBridgeForComponent(component, componentName, componentInstance, componentBridges);
}
componentCallback = new JSONComponentCallback(context);
jsonBridge.registerCallback(componentCallback, WOContext.class);
} else {
jsonBridge = _sharedBridge;
}
try {
output = jsonBridge.call(new Object[] { request, response, context }, input);
} finally {
if (componentCallback != null) {
jsonBridge.unregisterCallback(componentCallback, WOContext.class);
}
}
if (context._session() != null) {
WOSession contextSession = context._session();
// If this is a new session, then we have to force it to be a cookie session
if (sessionId == null) {
boolean storesIDsInCookies = contextSession.storesIDsInCookies();
try {
contextSession.setStoresIDsInCookies(true);
contextSession._appendCookieToResponse(response);
} finally {
contextSession.setStoresIDsInCookies(storesIDsInCookies);
}
} else {
contextSession._appendCookieToResponse(response);
}
}
response.appendContentString(output.toString());
response._finalizeInContext(context);
response.disableClientCaching();
} finally {
try {
if (session != null) {
session.sleep();
}
} finally {
if (context._session() != null) {
WOApplication.application().saveSessionForContext(context);
}
}
}
} catch (NoSuchElementException e) {
e.printStackTrace();
output = new JSONRPCResult(JSONRPCResult.CODE_ERR_NOMETHOD, null, JSONRPCResult.MSG_ERR_NOMETHOD);
} catch (JSONException e) {
e.printStackTrace();
output = new JSONRPCResult(JSONRPCResult.CODE_ERR_PARSE, null, JSONRPCResult.MSG_ERR_PARSE);
} catch (Throwable t) {
t.printStackTrace();
output = new JSONRPCResult(JSONRPCResult.CODE_ERR_PARSE, null, t.getMessage());
}
return response;
} finally {
application.sleep();
}
}
use of er.extensions.foundation.ERXMutableURL in project wonder-slim by undur.
the class AjaxModalContainer method appendToResponse.
@Override
public void appendToResponse(WOResponse response, WOContext context) {
WOComponent component = context.component();
String linkID = (String) valueForBinding("id", component);
if (linkID == null) {
linkID = ERXWOContext.safeIdentifierName(context, false);
}
String containerID = (String) valueForBinding("containerID", linkID + "Container", component);
response.appendContentString("<a");
String href = (String) valueForBinding("href", component);
if (href == null) {
String directActionName = stringValueForBinding("directActionName", component);
if (directActionName != null) {
NSDictionary queryDictionary = (NSDictionary) valueForBinding("queryDictionary", component);
boolean secure = booleanValueForBinding("secure", ERXRequest.isRequestSecure(context.request()), component);
if (secure) {
boolean generatingCompleteURLs = context.doesGenerateCompleteURLs();
if (!generatingCompleteURLs) {
context.generateCompleteURLs();
}
try {
href = context._directActionURL(directActionName, queryDictionary, secure, 0, false);
ERXMutableURL u = new ERXMutableURL(href);
u.addQueryParameter(String.valueOf(System.currentTimeMillis()), null);
href = u.toExternalForm();
} catch (MalformedURLException e) {
throw new NSForwardException(e);
} finally {
if (!generatingCompleteURLs) {
context.generateRelativeURLs();
}
}
} else {
href = context.directActionURLForActionNamed(directActionName, queryDictionary);
}
}
}
boolean isAjax = booleanValueForBinding("ajax", false, component);
if (href == null) {
if (isAjax) {
if (valueForBinding("id", component) == null) {
throw new IllegalArgumentException("If ajax = 'true', you must also bind 'id'.");
}
href = AjaxUtils.ajaxComponentActionUrl(context);
} else if (associations().objectForKey("action") != null) {
// don't use ajax request handler here
href = context.componentActionURL();
}
if (href == null) {
href = "#" + containerID;
}
}
appendTagAttributeToResponse(response, "href", href);
String relAttributeValue = "ibox";
Object height = valueForBinding("height", component);
Object width = valueForBinding("width", component);
Object closeLabel = valueForBinding("closeLabel", component);
if (height != null) {
relAttributeValue += "&height=" + URLEncoder.encode(height.toString(), Charset.forName(WOMessage.defaultURLEncoding()));
}
if (width != null) {
relAttributeValue += "&width=" + URLEncoder.encode(width.toString(), Charset.forName(WOMessage.defaultURLEncoding()));
}
if (closeLabel != null) {
relAttributeValue += "&closeLabel=" + URLEncoder.encode(closeLabel.toString(), Charset.forName(WOMessage.defaultURLEncoding()));
}
if (booleanValueForBinding("locked", false, component)) {
relAttributeValue += "&locked=true";
}
// don't escape the ampersands
response._appendTagAttributeAndValue("rel", relAttributeValue, false);
appendTagAttributeToResponse(response, "title", valueForBinding("title", component));
appendTagAttributeToResponse(response, "value", valueForBinding("value", component));
appendTagAttributeToResponse(response, "class", valueForBinding("class", component));
appendTagAttributeToResponse(response, "style", valueForBinding("style", component));
appendTagAttributeToResponse(response, "id", linkID);
response.appendContentString(">");
if (!href.startsWith("#") && !isAjax && childrenElements() != null && childrenElements().count() > 0) {
appendChildrenToResponse(response, context);
} else {
Object label = valueForBinding("label", "", component);
response.appendContentString(label.toString());
}
response.appendContentString("</a>");
if (AjaxUtils.isAjaxRequest(context.request())) {
NSMutableDictionary userInfo = ERXWOContext.contextDictionary();
if (!userInfo.containsKey("er.ajax.AjaxModalContainer.init")) {
AjaxUtils.appendScriptHeader(response);
response.appendContentString("iBox.init()");
AjaxUtils.appendScriptFooter(response);
userInfo.setObjectForKey(Boolean.TRUE, "er.ajax.AjaxModalContainer.init");
}
}
if (booleanValueForBinding("open", false, component)) {
if (AjaxUtils.isAjaxRequest(context.request())) {
// PROTOTYPE FUNCTIONS
response.appendContentString("<script>iBox.handleTag.bind($wi('" + linkID + "'))()</script>");
} else {
// PROTOTYPE FUNCTIONS
response.appendContentString("<script>Event.observe(window, 'load', iBox.handleTag.bind($wi('" + linkID + "')))</script>");
}
}
if (href.startsWith("#")) {
response.appendContentString("<div");
appendTagAttributeToResponse(response, "id", containerID);
appendTagAttributeToResponse(response, "style", "display:none;");
response.appendContentString(">");
appendChildrenToResponse(response, context);
response.appendContentString("</div>");
}
super.appendToResponse(response, context);
}
Aggregations