Example usage for io.netty.handler.codec.http QueryStringDecoder uri

List of usage examples for io.netty.handler.codec.http QueryStringDecoder uri

Introduction

In this page you can find the example usage for io.netty.handler.codec.http QueryStringDecoder uri.

Prototype

String uri

To view the source code for io.netty.handler.codec.http QueryStringDecoder uri.

Click Source Link

Usage

From source file:com.jetbrains.edu.learning.stepik.builtInServer.StepikRestService.java

License:Apache License

@Nullable
@Override/*from  www. j  a  v  a  2s.c  om*/
public String execute(@NotNull QueryStringDecoder urlDecoder, @NotNull FullHttpRequest request,
        @NotNull ChannelHandlerContext context) throws IOException {
    String uri = urlDecoder.uri();
    LOG.info("Request: " + uri);

    Matcher matcher = OPEN_COURSE_PATTERN.matcher(uri);
    if (matcher.matches()) {
        int courseId;
        int stepId;
        String link = getStringParameter(StepikNames.LINK, urlDecoder);

        if (link == null) {
            return log("The link parameter was not found");
        }

        LOG.info("Try to open a course: " + link);

        QueryStringDecoder linkDecoder = new QueryStringDecoder(link);

        matcher = COURSE_PATTERN.matcher(linkDecoder.path());

        if (!matcher.matches()) {
            return log("Unrecognized the link parameter");
        }

        int lessonId;
        int stepIndex;
        try {
            lessonId = Integer.parseInt(matcher.group(1));
            stepIndex = Integer.parseInt(matcher.group(2));
        } catch (NumberFormatException e) {
            return log("Unrecognized the link");
        }

        int unitId = getIntParameter("unit", linkDecoder);

        if (unitId == -1) {
            return log("Unrecognized the Unit id");
        }

        StepikWrappers.Unit unit = StepikConnector.getUnit(unitId);
        if (unit.getId() == 0) {
            return log("Unrecognized the Unit id");
        }

        Section section = StepikConnector.getSection(unit.getSection());
        courseId = section.getCourseId();
        if (courseId == 0) {
            return log("Unrecognized the course id");
        }
        Lesson lesson = StepikConnector.getLesson(lessonId);
        List<Integer> stepIds = lesson.steps;

        if (stepIds.isEmpty()) {
            return log("Unrecognized the step id");
        }
        stepId = stepIds.get(stepIndex - 1);

        LOG.info(String.format("Try to open a course: courseId=%s, stepId=%s", courseId, stepId));

        if (focusOpenProject(courseId, stepId) || openRecentProject(courseId, stepId)
                || createProject(courseId, stepId)) {
            RestService.sendOk(request, context);
            LOG.info("Course opened: " + courseId);
            return null;
        }

        RestService.sendStatus(HttpResponseStatus.NOT_FOUND, false, context.channel());
        String message = "A project didn't found or created";
        LOG.info(message);
        return message;
    }

    Matcher codeMatcher = OAUTH_CODE_PATTERN.matcher(uri);
    if (codeMatcher.matches()) {
        String code = getStringParameter("code", urlDecoder);
        if (code != null) {
            StepicUser user = StepikAuthorizedClient.login(code, StepikConnector.getOAuthRedirectUrl());
            if (user != null) {
                EduSettings.getInstance().setUser(user);
                showOkPage(request, context);
                showStepikNotification(NotificationType.INFORMATION,
                        "Logged in as " + user.getFirstName() + " " + user.getLastName());
                focusOnApplicationWindow();
                return null;
            }
        }

        showStepikNotification(NotificationType.ERROR, "Failed to log in");
        return sendErrorResponse(request, context, "Couldn't find code parameter for Stepik OAuth");
    }

    RestService.sendStatus(HttpResponseStatus.BAD_REQUEST, false, context.channel());
    String message = "Unknown command: " + uri;
    LOG.info(message);
    return message;
}

From source file:org.jpos.qrest.participant.Q2Info.java

License:Open Source License

@Override
public int prepare(long id, Serializable context) {
    Context ctx = (Context) context;
    FullHttpRequest request = (FullHttpRequest) ctx.get(REQUEST);
    QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
    Map<String, Object> response = null;
    Optional<Route<Map<String, Object>>> route = routes.stream().filter(rr -> rr.matches(decoder.uri()))
            .findFirst();/*w w  w. j  a  va  2 s .  co m*/
    String path = URI.create(decoder.uri()).getPath();

    HttpResponseStatus status = HttpResponseStatus.NOT_FOUND;
    if (route.isPresent()) {
        if (route.get().isValid(path)) {
            response = new LinkedHashMap<>();
            response.putAll(route.get().apply(route.get(), path));
            status = HttpResponseStatus.OK;
        }
    } else {
        response = new LinkedHashMap<>();
        for (Route<Map<String, Object>> r : routes) {
            if (!r.hasPathParams())
                response.putAll(r.apply(r, path));
        }
        status = HttpResponseStatus.OK;
    }
    if (response != null && response.containsKey("error")) {
        status = HttpResponseStatus.NOT_ACCEPTABLE;
    }
    ctx.put(RESPONSE, response != null ? new Response(status, response)
            : new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status));
    return PREPARED | NO_JOIN | READONLY;
}

From source file:org.jpos.qrest.participant.Router.java

License:Open Source License

@Override
public String select(long id, Serializable context) {
    Context ctx = (Context) context;
    FullHttpRequest request = ctx.get(REQUEST);
    ctx.log("Method: " + request.method().name());
    ctx.log("Routes: " + routes);
    List<Route<String>> routesByMethod = routes.get(request.method().name());
    QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
    if (!decoder.parameters().isEmpty())
        ctx.put(QUERYPARAMS, decoder.parameters());

    if (routesByMethod != null) {
        Optional<Route<String>> route = routesByMethod.stream().filter(r -> r.matches(decoder.uri()))
                .findFirst();/* w w  w  .j av  a2  s.  co  m*/
        String path = URI.create(decoder.uri()).getPath();
        if (route.isPresent()) {
            Map m = route.get().parameters(path);
            if (m != null)
                ctx.put(PATHPARAMS, m);
            return route.get().apply(route.get(), path);
        }
    }
    return null;
}