String url = context.getUrl().toString();
View view = gadget.getCurrentView();
View.ContentType type;
type = (view == null) ? View.ContentType.HTML : view.getType();
UriBuilder uri;
switch (type) {
case URL:
uri = new UriBuilder(view.getHref());
break;
case HTML:
case HTML_SANITIZED:
default:
Uri iframeBaseUri = iframeBaseUris.get(context.getContainer());
uri = iframeBaseUri != null ? new UriBuilder(iframeBaseUri) : new UriBuilder();
String host = lockedDomainService.getLockedDomainForGadget(gadget, context.getContainer());
if (host != null) {
uri.setAuthority(host);
}
break;
}
uri.addQueryParameter("container", context.getContainer());
if (context.getModuleId() != 0) {
uri.addQueryParameter("mid", Integer.toString(context.getModuleId()));
}
if (context.getIgnoreCache()) {
uri.addQueryParameter("nocache", "1");
} else {
uri.addQueryParameter("v", spec.getChecksum());
}
uri.addQueryParameter("lang", context.getLocale().getLanguage());
uri.addQueryParameter("country", context.getLocale().getCountry());
uri.addQueryParameter("view", context.getView());
UserPrefs prefs = context.getUserPrefs();
for (UserPref pref : gadget.getSpec().getUserPrefs()) {
String name = pref.getName();
String value = prefs.getPref(name);
if (value == null) {
value = pref.getDefaultValue();
}
uri.addQueryParameter("up_" + pref.getName(), value);
}
// add url last to work around browser bugs
if(type != View.ContentType.URL) {
uri.addQueryParameter("url", url);
}
return uri.toString();
}