Search in sources :

Example 1 with StringUtils.substringAfter

use of org.apache.commons.lang3.StringUtils.substringAfter in project kylo by Teradata.

the class DebugController method queryJcr.

/**
 * Prints the nodes of the JCR path given, for debugging.
 *
 * @param query the jcr query
 * @return a printout of the JCR tree
 */
@GET
@Path("jcr-sql")
@Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
public JcrQueryResult queryJcr(@QueryParam("query") final String query) {
    return metadata.read(() -> {
        this.accessController.checkPermission(AccessController.SERVICES, MetadataAccessControl.ADMIN_METADATA);
        List<List<String>> rows = new ArrayList<>();
        Long startTime = System.currentTimeMillis();
        JcrQueryResult jcrQueryResult = new JcrQueryResult();
        try {
            Session session = JcrMetadataAccess.getActiveSession();
            Workspace workspace = (Workspace) session.getWorkspace();
            String explainPlain = JcrQueryUtil.explainPlain(session, query);
            // start the timer now:
            startTime = System.currentTimeMillis();
            QueryResult result = JcrQueryUtil.query(session, query);
            jcrQueryResult.setExplainPlan(explainPlain);
            RowIterator rowItr = result.getRows();
            List<JcrQueryResultColumn> columns = new ArrayList<>();
            String colsStr = StringUtils.substringAfter(query.toLowerCase(), "select");
            colsStr = StringUtils.substringBefore(colsStr, "from");
            if (StringUtils.isNotBlank(colsStr)) {
                colsStr = colsStr.trim();
                columns = Arrays.asList(colsStr.split(",")).stream().map(c -> {
                    String columnName = c;
                    if (c.contains("as ")) {
                        columnName = StringUtils.substringAfter(c, "as ");
                    } else if (c.contains(" ")) {
                        columnName = StringUtils.substringAfter(c, " ");
                    }
                    return new JcrQueryResultColumn(columnName);
                }).collect(Collectors.toList());
            }
            jcrQueryResult.setColumns(columns);
            while (rowItr.hasNext()) {
                Row row = rowItr.nextRow();
                Value[] rowValues = row.getValues();
                if (rowValues != null) {
                    if (rowValues.length != columns.size()) {
                        columns = IntStream.range(0, rowValues.length).mapToObj(i -> new JcrQueryResultColumn("Column " + i)).collect(Collectors.toList());
                        jcrQueryResult.setColumns(columns);
                    }
                    JcrQueryResultRow jcrQueryResultRow = new JcrQueryResultRow();
                    jcrQueryResult.addRow(jcrQueryResultRow);
                    List<JcrQueryResultColumnValue> jcrQueryResultColumnValues = Arrays.asList(rowValues).stream().map(v -> {
                        try {
                            String value = v.getString();
                            return new JcrQueryResultColumnValue(value);
                        } catch (Exception e) {
                            return new JcrQueryResultColumnValue("ERROR: " + e.getMessage());
                        }
                    }).collect(Collectors.toList());
                    jcrQueryResultRow.setColumnValues(jcrQueryResultColumnValues);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        long totalTime = System.currentTimeMillis() - startTime;
        jcrQueryResult.setQueryTime(totalTime);
        return jcrQueryResult;
    });
}
Also used : Arrays(java.util.Arrays) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Produces(javax.ws.rs.Produces) JcrQueryResultRow(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultRow) Metric(com.thinkbiganalytics.metadata.sla.api.Metric) JcrTools(org.modeshape.jcr.api.JcrTools) JcrQueryResult(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResult) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) QueryResult(javax.jcr.query.QueryResult) FeedPrecondition(com.thinkbiganalytics.metadata.rest.model.feed.FeedPrecondition) Property(javax.jcr.Property) StringUtils(org.apache.commons.lang3.StringUtils) JcrTool(com.thinkbiganalytics.metadata.modeshape.support.JcrTool) ApiOperation(io.swagger.annotations.ApiOperation) VersionException(javax.jcr.version.VersionException) MediaType(javax.ws.rs.core.MediaType) JcrQueryResultColumn(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultColumn) QueryParam(javax.ws.rs.QueryParam) JcrPath(com.thinkbiganalytics.metadata.modeshape.support.JcrPath) JcrQueryUtil(com.thinkbiganalytics.metadata.modeshape.support.JcrQueryUtil) IndexDefinition(org.modeshape.jcr.api.index.IndexDefinition) Consumes(javax.ws.rs.Consumes) WithinSchedule(com.thinkbiganalytics.metadata.api.sla.WithinSchedule) JcrQueryResultColumnValue(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultColumnValue) Map(java.util.Map) AccessController(com.thinkbiganalytics.security.AccessController) DefaultValue(javax.ws.rs.DefaultValue) ModeshapeIndexUtil(com.thinkbiganalytics.metadata.modeshape.support.ModeshapeIndexUtil) Collector(java.util.stream.Collector) MetadataAccess(com.thinkbiganalytics.metadata.api.MetadataAccess) ParseException(java.text.ParseException) DELETE(javax.ws.rs.DELETE) PrintWriter(java.io.PrintWriter) JcrPropertyUtil(com.thinkbiganalytics.metadata.modeshape.support.JcrPropertyUtil) Context(javax.ws.rs.core.Context) Session(javax.jcr.Session) MetadataAccessControl(com.thinkbiganalytics.metadata.api.security.MetadataAccessControl) Set(java.util.Set) PropertyType(javax.jcr.PropertyType) MetadataEventService(com.thinkbiganalytics.metadata.api.event.MetadataEventService) Collectors(java.util.stream.Collectors) JcrIndexDefinition(com.thinkbiganalytics.metadata.rest.model.jcr.JcrIndexDefinition) List(java.util.List) Stream(java.util.stream.Stream) Item(javax.jcr.Item) Row(javax.jcr.query.Row) UriInfo(javax.ws.rs.core.UriInfo) JcrMetadataAccess(com.thinkbiganalytics.metadata.modeshape.JcrMetadataAccess) IntStream(java.util.stream.IntStream) PathParam(javax.ws.rs.PathParam) FeedOperationStatusEvent(com.thinkbiganalytics.metadata.api.event.feed.FeedOperationStatusEvent) ValueFactory(javax.jcr.ValueFactory) GET(javax.ws.rs.GET) ApiResponses(io.swagger.annotations.ApiResponses) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) MetadataAccessException(com.thinkbiganalytics.metadata.api.MetadataAccessException) PathNotFoundException(javax.jcr.PathNotFoundException) RestResponseStatus(com.thinkbiganalytics.rest.model.RestResponseStatus) RepositoryException(javax.jcr.RepositoryException) RowIterator(javax.jcr.query.RowIterator) Node(javax.jcr.Node) FeedExecutedSinceFeed(com.thinkbiganalytics.metadata.api.sla.FeedExecutedSinceFeed) LockException(javax.jcr.lock.LockException) Api(io.swagger.annotations.Api) Tag(io.swagger.annotations.Tag) OperationStatus(com.thinkbiganalytics.metadata.api.event.feed.OperationStatus) FeedOperation(com.thinkbiganalytics.metadata.api.op.FeedOperation) FeedExecutedSinceSchedule(com.thinkbiganalytics.metadata.api.sla.FeedExecutedSinceSchedule) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) Datasource(com.thinkbiganalytics.metadata.rest.model.data.Datasource) StringWriter(java.io.StringWriter) ValueFormatException(javax.jcr.ValueFormatException) MetadataExecutionException(com.thinkbiganalytics.metadata.api.MetadataExecutionException) HiveTableDatasource(com.thinkbiganalytics.metadata.rest.model.data.HiveTableDatasource) Component(org.springframework.stereotype.Component) Value(javax.jcr.Value) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition) ApiResponse(io.swagger.annotations.ApiResponse) Workspace(org.modeshape.jcr.api.Workspace) PUT(javax.ws.rs.PUT) Collections(java.util.Collections) JcrQueryResultRow(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultRow) ArrayList(java.util.ArrayList) JcrQueryResultColumn(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultColumn) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) VersionException(javax.jcr.version.VersionException) ParseException(java.text.ParseException) MetadataAccessException(com.thinkbiganalytics.metadata.api.MetadataAccessException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) LockException(javax.jcr.lock.LockException) ValueFormatException(javax.jcr.ValueFormatException) MetadataExecutionException(com.thinkbiganalytics.metadata.api.MetadataExecutionException) JcrQueryResult(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResult) QueryResult(javax.jcr.query.QueryResult) JcrQueryResult(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResult) RowIterator(javax.jcr.query.RowIterator) JcrQueryResultColumnValue(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultColumnValue) DefaultValue(javax.ws.rs.DefaultValue) Value(javax.jcr.Value) JcrQueryResultColumnValue(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultColumnValue) List(java.util.List) ArrayList(java.util.ArrayList) JcrQueryResultRow(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultRow) Row(javax.jcr.query.Row) Session(javax.jcr.Session) Workspace(org.modeshape.jcr.api.Workspace) Path(javax.ws.rs.Path) JcrPath(com.thinkbiganalytics.metadata.modeshape.support.JcrPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with StringUtils.substringAfter

use of org.apache.commons.lang3.StringUtils.substringAfter in project openEHR_SDK by ehrbase.

the class DvCodedTextRMUnmarshaller method handle.

/**
 * {@inheritDoc}
 */
@Override
public void handle(String currentTerm, DvCodedText rmObject, Map<FlatPathDto, String> currentValues, Context<Map<FlatPathDto, String>> context, Set<String> consumedPaths) {
    setValue(currentTerm, "value", currentValues, rmObject::setValue, String.class, consumedPaths);
    rmObject.setDefiningCode(new CodePhrase());
    setValue(currentTerm, "code", currentValues, c -> rmObject.getDefiningCode().setCodeString(c), String.class, consumedPaths);
    rmObject.getDefiningCode().setTerminologyId(new TerminologyId());
    setValue(currentTerm, "terminology", currentValues, t -> rmObject.getDefiningCode().getTerminologyId().setValue(t), String.class, consumedPaths);
    if (rmObject.getDefiningCode().getCodeString() == null && rmObject.getValue() == null) {
        setValue(currentTerm, null, currentValues, c -> {
            if (c != null) {
                // try to interpret as code
                setFromNode(rmObject, context, v -> Objects.equals(v.getValue(), c));
                if (rmObject.getValue() == null) {
                    // try to interpret as value
                    setFromNode(rmObject, context, v -> Objects.equals(v.getLabel(), c));
                }
            }
        }, String.class, consumedPaths);
    } else if (rmObject.getDefiningCode().getCodeString() == null) {
        setValue(currentTerm, null, currentValues, // It is the code
        c -> rmObject.getDefiningCode().setCodeString(c), String.class, consumedPaths);
    }
    // Set terminology from Node
    Optional.of(context.getNodeDeque().peek().getInputs()).stream().flatMap(List::stream).filter(i -> "code".equals(i.getSuffix())).findAny().map(WebTemplateInput::getTerminology).ifPresent(t -> rmObject.getDefiningCode().getTerminologyId().setValue(t));
    // Set value from Node
    setFromNode(rmObject, context, v -> Objects.equals(v.getValue(), rmObject.getDefiningCode().getCodeString()));
    // set code from value if not set
    if (rmObject.getDefiningCode().getCodeString() == null && rmObject.getValue() != null) {
        setFromNode(rmObject, context, v -> Objects.equals(v.getLabel(), rmObject.getValue()));
    }
    setFromNode(rmObject, context, v -> Objects.equals(v.getValue(), rmObject.getDefiningCode().getCodeString()));
    // consume strange legacy paths
    if (rmObject.getDefiningCode() != null && rmObject.getDefiningCode().getCodeString() != null) {
        currentValues.keySet().stream().map(FlatPathDto::format).filter(k -> StringUtils.substringAfter(k, "|").equals(rmObject.getDefiningCode().getCodeString())).forEach(consumedPaths::add);
    }
    setValue(currentTerm, "formatting", currentValues, rmObject::setFormatting, String.class, consumedPaths);
}
Also used : Context(org.ehrbase.serialisation.walker.Context) WebTemplateInputValue(org.ehrbase.webtemplate.model.WebTemplateInputValue) java.util(java.util) TerminologyId(com.nedap.archie.rm.support.identification.TerminologyId) Predicate(java.util.function.Predicate) WebTemplateInput(org.ehrbase.webtemplate.model.WebTemplateInput) FlatPathDto(org.ehrbase.webtemplate.path.flat.FlatPathDto) DvCodedText(com.nedap.archie.rm.datavalues.DvCodedText) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) StringUtils(org.apache.commons.lang3.StringUtils) TerminologyId(com.nedap.archie.rm.support.identification.TerminologyId) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) FlatPathDto(org.ehrbase.webtemplate.path.flat.FlatPathDto)

Example 3 with StringUtils.substringAfter

use of org.apache.commons.lang3.StringUtils.substringAfter in project tumo-boot by Tumo-Team.

the class AuthUtil method getRoleNames.

/**
 * 获取用户角色Alias集合
 */
public static List<String> getRoleNames() {
    List<String> roleNames = new ArrayList<>();
    Authentication authentication = getAuthentication();
    if (authentication == null) {
        return roleNames;
    }
    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
    authorities.stream().filter(granted -> StringUtils.startsWith(granted.getAuthority(), AuthConstant.ROLE_PREFIX)).forEach(granted -> {
        String name = StringUtils.substringAfter(granted.getAuthority(), AuthConstant.ROLE_SUFFIX);
        roleNames.add(name);
    });
    return roleNames;
}
Also used : BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) StringUtils(org.apache.commons.lang3.StringUtils) AuthConstant(cn.tycoding.boot.common.core.constant.AuthConstant) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) Objects(java.util.Objects) RequestContextHolder(org.springframework.web.context.request.RequestContextHolder) HttpServletRequest(javax.servlet.http.HttpServletRequest) List(java.util.List) TumoUser(cn.tycoding.boot.modules.auth.dto.TumoUser) Authentication(org.springframework.security.core.Authentication) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) Authentication(org.springframework.security.core.Authentication) ArrayList(java.util.ArrayList)

Example 4 with StringUtils.substringAfter

use of org.apache.commons.lang3.StringUtils.substringAfter in project kylo by Teradata.

the class RefreshableDataSourceTest method testCreateDataSourceAndGetProperties.

private Map<String, String> testCreateDataSourceAndGetProperties() throws Exception {
    DataSource ds = Whitebox.invokeMethod(hiveDs, "create", true, principal);
    Map<String, String> props = Arrays.stream(ds.toString().split("; ")).collect(Collectors.toMap(s -> StringUtils.substringBefore(s, "="), s -> StringUtils.substringAfter(s, "=")));
    return props;
}
Also used : Whitebox(org.powermock.reflect.Whitebox) Arrays(java.util.Arrays) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) KerberosUtil(com.thinkbiganalytics.kerberos.KerberosUtil) Mockito(org.mockito.Mockito) UsernameCaseStrategyUtil(com.thinkbiganalytics.UsernameCaseStrategyUtil) SecurityContext(org.springframework.security.core.context.SecurityContext) Environment(org.springframework.core.env.Environment) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Map(java.util.Map) DataSource(javax.sql.DataSource) KerberosTicketConfiguration(com.thinkbiganalytics.kerberos.KerberosTicketConfiguration) Assert(org.junit.Assert) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) Authentication(org.springframework.security.core.Authentication) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) Before(org.junit.Before) DataSource(javax.sql.DataSource)

Example 5 with StringUtils.substringAfter

use of org.apache.commons.lang3.StringUtils.substringAfter in project Piped-Backend by TeamPiped.

the class DownloaderImpl method executeRequest.

/**
 * Executes a request with HTTP/2.
 */
public Response executeRequest(Request request) throws IOException, ReCaptchaException {
    // TODO: HTTP/3 aka QUIC
    var bytes = request.dataToSend();
    RequestBody body = null;
    if (bytes != null)
        body = RequestBody.create(bytes);
    var builder = new okhttp3.Request.Builder().url(request.url()).method(request.httpMethod(), body).header("User-Agent", Constants.USER_AGENT);
    if (saved_cookie != null && !saved_cookie.hasExpired())
        builder.header("Cookie", saved_cookie.getName() + "=" + saved_cookie.getValue());
    request.headers().forEach((name, values) -> values.forEach(value -> builder.header(name, value)));
    var resp = Constants.h2client.newCall(builder.build()).execute();
    if (resp.code() == 429) {
        synchronized (cookie_lock) {
            if (saved_cookie != null && saved_cookie.hasExpired() || (System.currentTimeMillis() - cookie_received > TimeUnit.MINUTES.toMillis(30)))
                saved_cookie = null;
            String redir_url = String.valueOf(resp.request().url());
            if (saved_cookie == null && redir_url.startsWith("https://www.google.com/sorry")) {
                var formBuilder = new FormBody.Builder();
                String sitekey = null, data_s = null;
                for (Element el : Jsoup.parse(resp.body().string()).selectFirst("form").children()) {
                    String name;
                    if (!(name = el.tagName()).equals("script")) {
                        if (name.equals("input"))
                            formBuilder.add(el.attr("name"), el.attr("value"));
                        else if (name.equals("div") && el.attr("id").equals("recaptcha")) {
                            sitekey = el.attr("data-sitekey");
                            data_s = el.attr("data-s");
                        }
                    }
                }
                if (StringUtils.isEmpty(sitekey) || StringUtils.isEmpty(data_s))
                    throw new ReCaptchaException("Could not get recaptcha", redir_url);
                SolvedCaptcha solved = null;
                try {
                    solved = CaptchaSolver.solve(redir_url, sitekey, data_s);
                } catch (JsonParserException | InterruptedException e) {
                    e.printStackTrace();
                }
                formBuilder.add("g-recaptcha-response", solved.getRecaptchaResponse());
                var formReqBuilder = new okhttp3.Request.Builder().url("https://www.google.com/sorry/index").header("User-Agent", Constants.USER_AGENT).post(formBuilder.build());
                var formResponse = Constants.h2_no_redir_client.newCall(formReqBuilder.build()).execute();
                saved_cookie = HttpCookie.parse(URLUtils.silentDecode(StringUtils.substringAfter(formResponse.headers().get("Location"), "google_abuse="))).get(0);
                cookie_received = System.currentTimeMillis();
            }
            if (// call again as captcha has been solved or cookie has not expired.
            saved_cookie != null)
                execute(request);
        }
    }
    var response = new Response(resp.code(), resp.message(), resp.headers().toMultimap(), resp.body().string(), String.valueOf(resp.request().url()));
    resp.close();
    return response;
}
Also used : Caffeine(com.github.benmanes.caffeine.cache.Caffeine) ReCaptchaException(org.schabi.newpipe.extractor.exceptions.ReCaptchaException) LoadingCache(com.github.benmanes.caffeine.cache.LoadingCache) Constants(me.kavin.piped.consts.Constants) Downloader(org.schabi.newpipe.extractor.downloader.Downloader) IOException(java.io.IOException) SolvedCaptcha(me.kavin.piped.utils.obj.SolvedCaptcha) Scheduler(com.github.benmanes.caffeine.cache.Scheduler) StringUtils(org.apache.commons.lang3.StringUtils) RequestBody(okhttp3.RequestBody) TimeUnit(java.util.concurrent.TimeUnit) HttpCookie(java.net.HttpCookie) FormBody(okhttp3.FormBody) JsonParserException(com.grack.nanojson.JsonParserException) Element(org.jsoup.nodes.Element) Request(org.schabi.newpipe.extractor.downloader.Request) Jsoup(org.jsoup.Jsoup) NotNull(org.jetbrains.annotations.NotNull) Response(org.schabi.newpipe.extractor.downloader.Response) SolvedCaptcha(me.kavin.piped.utils.obj.SolvedCaptcha) Element(org.jsoup.nodes.Element) JsonParserException(com.grack.nanojson.JsonParserException) Request(org.schabi.newpipe.extractor.downloader.Request) ReCaptchaException(org.schabi.newpipe.extractor.exceptions.ReCaptchaException) Response(org.schabi.newpipe.extractor.downloader.Response) RequestBody(okhttp3.RequestBody)

Aggregations

StringUtils (org.apache.commons.lang3.StringUtils)14 List (java.util.List)8 Map (java.util.Map)8 Collectors (java.util.stream.Collectors)8 Arrays (java.util.Arrays)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 Optional (java.util.Optional)3 FileUtils (org.apache.commons.io.FileUtils)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2