*
* @return the response target.
*/
public Target resolve(Request request) {
RequestContext context = (RequestContext) request;
final ServletRequestContext requestContext;
if (context instanceof ServletRequestContext) {
requestContext = (ServletRequestContext) request;
} else {
requestContext = null;
}
if (embeddedRegistryService == null) {
if (requestContext != null) {
embeddedRegistryService =
(EmbeddedRegistryService) requestContext.getRequest().getSession()
.getServletContext().getAttribute("registry");
}
if (embeddedRegistryService == null) {
String msg = "Error in retrieving the embedded registry service.";
log.error(msg);
}
}
//TODO (reg-sep)
UserRegistry registry = null;
String uri = context.getUri().toString();
String loggedIn = null;
if (requestContext != null) {
loggedIn = ((ServletRequestContext) request).getRequest().getParameter("loggedIn");
}
if (loggedIn != null) {
String loggedUser =
(String) requestContext.getRequest().getSession().getServletContext()
.getAttribute("logged-user");
try {
registry = embeddedRegistryService.getRegistry(loggedUser);
uri = uri.substring(0, uri.lastIndexOf("?"));
} catch (RegistryException e) {
final StringResponseContext response =
new StringResponseContext("Unauthorized", 401);
response.setHeader("WWW-Authenticate", "Basic realm=\"WSO2-Registry\"");
return new ResponseTarget(context, response);
}
}
if (registry == null) {
// Set up secure registry instance
String authorizationString = request.getAuthorization();
if (authorizationString != null) {
// splitting the Authorization string "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
String values[] = authorizationString.split("\\ ");
if (values == null || values.length == 0) {
final StringResponseContext response =
new StringResponseContext("Unauthorized", 401);
response.setHeader("WWW-Authenticate", "Basic realm=\"WSO2-Registry\"");
return new ResponseTarget(context, response);
} else if ("Basic".equals(values[0])) {
try {
// Decode username/password
authorizationString = new String(Base64.decode(values[1]));
values = authorizationString.split("\\:");
String userName = values[0];
String password = values[1];
String tenantDomain =
(String) ((ServletRequestContext) request).getRequest().
getAttribute(MultitenantConstants.TENANT_DOMAIN);
int tenantId;
String userNameAlong;
if (tenantDomain == null) {
tenantId = getTenantId(userName);
userNameAlong = getUserName(userName);
} else {
tenantId = getTenantIdFromDomain(tenantDomain);
userNameAlong = userName;
}
registry = embeddedRegistryService.getRegistry(userNameAlong,
password, tenantId);
} catch (Exception e) {
final StringResponseContext response =
new StringResponseContext("Unauthorized", 401);
response.setHeader("WWW-Authenticate", "Basic realm=\"WSO2-Registry\"");
return new ResponseTarget(context, response);
}
} else {
// TODO - return an ExceptionTarget which contains the authentication problem
// return new ExceptionTarget(400, "Only basic authentication is supported!");
return null;
}
} else {
String tenantDomain = (String) requestContext.getRequest().
getAttribute(MultitenantConstants.TENANT_DOMAIN);
int calledTenantId = 0;
if (tenantDomain != null) {
if (RegistryContext.getBaseInstance().getRealmService() == null) {
String msg = "Error in getting the tenant manager. " +