@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (!(request instanceof HttpServletRequest)) { throw new RuntimeException("Expecting a HTTP request"); } HttpServletRequest httpRequest = (HttpServletRequest) request; String authToken = httpRequest.getHeader("X-Auth-Token"); String userName = TokenUtils.getUserNameFromToken(authToken); if (userName != null) { UserDetails userDetails = this.userService.loadUserByUsername(userName); if (TokenUtils.validateToken(authToken, userDetails)) { UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( userDetails, null, userDetails.getAuthorities()); authentication.setDetails( new WebAuthenticationDetailsSource().buildDetails((HttpServletRequest) request)); SecurityContextHolder.getContext().setAuthentication(authentication); } } chain.doFilter(request, response); }
/** * Check that there is exactly one space between a control structure keyword and a opening * parenthesis or curly brace. */ private void checkSpaceBetweenKeywordAndNextNode(SyntaxToken keyword, SyntaxToken nextToken) { if (TokenUtils.isType(nextToken, PHPPunctuator.LCURLYBRACE, PHPPunctuator.LPARENTHESIS) && TokenUtils.isOnSameLine(keyword, nextToken)) { int nbSpace = TokenUtils.getNbSpaceBetween(keyword, nextToken); if (nbSpace != 1) { String endMessage = String.format( CONTROL_STRUCTURES_KEYWORD_MESSAGE, keyword.text(), TokenUtils.isType(nextToken, PHPPunctuator.LPARENTHESIS) ? "parenthesis." : "curly brace."); check.reportIssue(TokenUtils.buildIssueMsg(nbSpace, endMessage), keyword); } } }
/** Check there is exactly one space after each ";" in for statement. */ private void checkSpaceForStatement(Tree tree) { Iterator<Tree> iterator = ((PHPTree) tree).childrenIterator(); Tree next; Tree previous = null; while (iterator.hasNext()) { next = iterator.next(); if (isSemicolon(previous)) { SyntaxToken semicolonToken = (SyntaxToken) previous; SyntaxToken nextToken = ((PHPTree) next).getFirstToken(); int nbSpace = TokenUtils.getNbSpaceBetween(semicolonToken, nextToken); if (nbSpace != 1 && TokenUtils.isOnSameLine(semicolonToken, nextToken)) { check.reportIssue(FOR_SEMICOLON_MESSAGE, tree); break; } } previous = next; } }
TextAnnotation readTextAnnotation(String string) throws Exception { JsonObject json = (JsonObject) new JsonParser().parse(string); String corpusId = readString("corpusId", json); String id = readString("id", json); String text = readString("text", json); String[] tokens = readStringArray("tokens", json); Pair<Pair<String, Double>, int[]> sentences = readSentences(json); IntPair[] offsets = TokenUtils.getTokenOffsets(text, tokens); TextAnnotation ta = new TextAnnotation(corpusId, id, text, offsets, tokens, sentences.getSecond()); JsonArray views = json.getAsJsonArray("views"); for (int i = 0; i < views.size(); i++) { JsonObject view = (JsonObject) views.get(i); String viewName = readString("viewName", view); JsonArray viewData = view.getAsJsonArray("viewData"); List<View> topKViews = new ArrayList<>(); for (int k = 0; k < viewData.size(); k++) { JsonObject kView = (JsonObject) viewData.get(k); topKViews.add(readView(kView, ta)); } ta.addTopKView(viewName, topKViews); } readAttributes(ta, json); return ta; }
private static boolean isSemicolon(@Nullable Tree tree) { return tree != null && tree.is(Kind.TOKEN) && TokenUtils.isType((SyntaxToken) tree, PHPPunctuator.SEMICOLON); }
private static boolean isExactlyOneSpaceAround(TokenVisitor tokenVisitor, SyntaxToken token) { return TokenUtils.getNbSpaceBetween(tokenVisitor.prevToken(token), token) == 1 && TokenUtils.getNbSpaceBetween(token, tokenVisitor.nextToken(token)) == 1; }
/** * @return the "normalized" version of the given source, which is built form tokens, so ignores * all comments and spaces. */ private static String getNormalizedSource(String s) { List<Token> selectionTokens = TokenUtils.getTokens(s); return StringUtils.join(selectionTokens, TOKEN_SEPARATOR); }