Search in sources :

Example 1 with I18n

use of org.xnap.commons.i18n.I18n in project candlepin by candlepin.

the class OAuth method getPrincipal.

/**
 * Attempt to pull a principal off of an oauth signed message.
 *
 * @return the principal if it can be created, null otherwise
 */
public Principal getPrincipal(HttpRequest httpRequest) {
    Principal principal = null;
    I18n i18n = i18nProvider.get();
    try {
        if (AuthUtil.getHeader(httpRequest, "Authorization").contains("oauth")) {
            OAuthMessage requestMessage = new RestEasyOAuthMessage(httpRequest);
            OAuthAccessor accessor = this.getAccessor(requestMessage);
            // TODO: This is known to be memory intensive.
            VALIDATOR.validateMessage(requestMessage, accessor);
            // If we got here, it is a valid oauth message.
            // Figure out which kind of principal we should create, based on header
            log.debug("Using OAuth");
            if (!AuthUtil.getHeader(httpRequest, TrustedUserAuth.USER_HEADER).equals("")) {
                principal = userAuth.getPrincipal(httpRequest);
            } else if (!AuthUtil.getHeader(httpRequest, TrustedConsumerAuth.CONSUMER_HEADER).equals("")) {
                principal = consumerAuth.getPrincipal(httpRequest);
            } else {
                // The external system is acting on behalf of itself
                principal = systemAuth.getPrincipal(httpRequest);
            }
        }
    } catch (OAuthProblemException e) {
        log.debug("OAuth Problem", e);
        // status code of 200. make it 401 unauthorized instead.
        if (e.getProblem().equals("signature_invalid")) {
            throw new NotAuthorizedException(i18n.tr("Invalid OAuth unit or secret"));
        }
        Response.Status returnCode = Response.Status.fromStatusCode(e.getHttpStatusCode());
        String message = i18n.tr("OAuth problem encountered. Internal message is: {0}", e.getMessage());
        throw new CandlepinException(returnCode, message);
    } catch (OAuthException e) {
        log.debug("OAuth Error", e);
        String message = i18n.tr("OAuth error encountered. Internal message is: {0}", e.getMessage());
        throw new BadRequestException(message);
    } catch (URISyntaxException e) {
        throw new IseException(e.getMessage(), e);
    } catch (IOException e) {
        throw new IseException(e.getMessage(), e);
    }
    return principal;
}
Also used : CandlepinException(org.candlepin.common.exceptions.CandlepinException) RestEasyOAuthMessage(org.candlepin.common.resteasy.auth.RestEasyOAuthMessage) OAuthMessage(net.oauth.OAuthMessage) OAuthException(net.oauth.OAuthException) NotAuthorizedException(org.candlepin.common.exceptions.NotAuthorizedException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) RestEasyOAuthMessage(org.candlepin.common.resteasy.auth.RestEasyOAuthMessage) OAuthAccessor(net.oauth.OAuthAccessor) OAuthProblemException(net.oauth.OAuthProblemException) IseException(org.candlepin.common.exceptions.IseException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) I18n(org.xnap.commons.i18n.I18n)

Example 2 with I18n

use of org.xnap.commons.i18n.I18n in project candlepin by candlepin.

the class SubscriptionResourceTest method setUp.

@Before
public void setUp() {
    I18n i18n = I18nFactory.getI18n(getClass(), Locale.US, I18nFactory.READ_PROPERTIES | I18nFactory.FALLBACK);
    this.subResource = new SubscriptionResource(subService, consumerCurator, poolManager, i18n);
}
Also used : I18n(org.xnap.commons.i18n.I18n) Before(org.junit.Before)

Example 3 with I18n

use of org.xnap.commons.i18n.I18n in project candlepin by candlepin.

the class PageRequestFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) {
    PageRequest p = null;
    MultivaluedMap<String, String> params = requestContext.getUriInfo().getQueryParameters();
    String page = params.getFirst(PageRequest.PAGE_PARAM);
    String perPage = params.getFirst(PageRequest.PER_PAGE_PARAM);
    String order = params.getFirst(PageRequest.ORDER_PARAM);
    String sortBy = params.getFirst(PageRequest.SORT_BY_PARAM);
    if (page != null || perPage != null || order != null || sortBy != null) {
        p = new PageRequest();
        if (order == null) {
            p.setOrder(PageRequest.DEFAULT_ORDER);
        } else {
            p.setOrder(readOrder(order));
        }
        /* We'll leave it to the curator layer to figure out what to sort by if
             * sortBy is null. */
        p.setSortBy(sortBy);
        try {
            if (page == null && perPage != null) {
                p.setPage(PageRequest.DEFAULT_PAGE);
                p.setPerPage(readInteger(perPage));
            } else if (page != null && perPage == null) {
                p.setPage(readInteger(page));
                p.setPerPage(PageRequest.DEFAULT_PER_PAGE);
            } else {
                p.setPage(readInteger(page));
                p.setPerPage(readInteger(perPage));
            }
        } catch (NumberFormatException nfe) {
            I18n i18n = this.i18nProvider.get();
            throw new BadRequestException(i18n.tr("offset and limit parameters" + " must be positive integers"), nfe);
        }
    }
    ResteasyProviderFactory.pushContext(PageRequest.class, p);
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) BadRequestException(org.candlepin.common.exceptions.BadRequestException) I18n(org.xnap.commons.i18n.I18n)

Example 4 with I18n

use of org.xnap.commons.i18n.I18n in project candlepin by candlepin.

the class CommonI18nProvider method get.

@Override
public I18n get() {
    Locale locale = (request.getLocale() == null) ? Locale.US : request.getLocale();
    I18n i18n;
    // share across threads!
    synchronized (cache) {
        i18n = cache.get(locale);
        if (i18n == null) {
            i18n = I18nFactory.getI18n(getClass(), getBaseName(), locale, I18nFactory.FALLBACK);
            cache.put(locale, i18n);
            log.debug("Getting i18n engine for locale {}", locale);
        }
    }
    return i18n;
}
Also used : Locale(java.util.Locale) I18n(org.xnap.commons.i18n.I18n)

Example 5 with I18n

use of org.xnap.commons.i18n.I18n in project candlepin by candlepin.

the class AtomFeedResourceTest method setUp.

@Before
public void setUp() {
    Configuration config = mock(Configuration.class);
    injector = Guice.createInjector(new TestingModules.MockJpaModule(), new TestingModules.StandardTest(config), new TestingModules.ServletEnvironmentModule());
    i18n = injector.getInstance(I18n.class);
    ec = mock(EventCurator.class);
    ea = new EventAdapterImpl(new ConfigForTesting(), i18n);
    afr = new AtomFeedResource(ec, ea);
}
Also used : MapConfiguration(org.candlepin.common.config.MapConfiguration) Configuration(org.candlepin.common.config.Configuration) EventAdapterImpl(org.candlepin.audit.EventAdapterImpl) EventCurator(org.candlepin.model.EventCurator) I18n(org.xnap.commons.i18n.I18n) Before(org.junit.Before)

Aggregations

I18n (org.xnap.commons.i18n.I18n)11 Before (org.junit.Before)6 Owner (org.candlepin.model.Owner)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Locale (java.util.Locale)2 BadRequestException (org.candlepin.common.exceptions.BadRequestException)2 Product (org.candlepin.model.Product)2 ProductCurator (org.candlepin.model.ProductCurator)2 Subscription (org.candlepin.model.dto.Subscription)2 Test (org.junit.Test)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 OAuthAccessor (net.oauth.OAuthAccessor)1 OAuthException (net.oauth.OAuthException)1 OAuthMessage (net.oauth.OAuthMessage)1 OAuthProblemException (net.oauth.OAuthProblemException)1 EventAdapterImpl (org.candlepin.audit.EventAdapterImpl)1