Example usage for java.util StringTokenizer StringTokenizer

List of usage examples for java.util StringTokenizer StringTokenizer

Introduction

In this page you can find the example usage for java.util StringTokenizer StringTokenizer.

Prototype

public StringTokenizer(String str, String delim) 

Source Link

Document

Constructs a string tokenizer for the specified string.

Usage

From source file:com.josue.jsf.jaxrs.base64.GenericResource.java

@GET
@Produces("text/plain")
public String getText(@QueryParam("query") String query) {
    byte[] decodedBytes = Base64.decodeBase64(query.getBytes());
    StringTokenizer token = new StringTokenizer(new String(decodedBytes), "&");
    Map<String, Object> params = new HashMap<>();
    while (token.hasMoreElements()) {
        String keyVal = token.nextToken();
        params.put(keyVal.split("=")[0], keyVal.split("=")[1]);
    }/*from w w  w.  j  av  a2 s.  c o m*/

    for (Object o : params.values()) {
        LOG.info(o.toString());
    }

    return new String(decodedBytes);
}

From source file:com.fusesource.examples.horo.rssReader.StarSignParser.java

public StarSign parse(@Header("title") String title) {
    Validate.notEmpty(title, "title is empty");

    StarSign starSign = null;/*from ww  w  . java2  s .  co m*/
    StringTokenizer tokenizer = new StringTokenizer(title, " ");
    while (tokenizer.hasMoreTokens()) {
        String token = tokenizer.nextToken();
        starSign = StarSign.getInstance(token);
        if (starSign != null) {
            break;
        }
    }
    return starSign;
}

From source file:de.mpg.imeji.presentation.util.LoginHelper.java

/**
 * Get handle of System administrator of eSciDoc instance.
 * /*from   w  w  w .  j  a v a  2s  . co m*/
 * @return
 */
//    public static String loginSystemAdmin()
//    {
//        String handle = null;
//        try
//        {
//            handle = login(PropertyReader.getProperty("framework.admin.username"),
//                    PropertyReader.getProperty("framework.admin.password"));
//        }
//        catch (Exception e)
//        {
//            sessionBean = (SessionBean)BeanHelper.getSessionBean(SessionBean.class);
//            BeanHelper
//                    .info(sessionBean.getLabel("error") + ", wrong administrator user. Check config file or FW: " + e);
//            logger.error("Error escidoc admin login", e);
//        }
//        return handle;
//    }

public static String login(String userName, String password) throws Exception {
    String frameworkUrl = PropertyReader.getProperty("escidoc.framework_access.framework.url");
    StringTokenizer tokens = new StringTokenizer(frameworkUrl, "//");
    tokens.nextToken();
    StringTokenizer hostPort = new StringTokenizer(tokens.nextToken(), ":");
    String host = hostPort.nextToken();
    int port = 80;
    if (hostPort.hasMoreTokens()) {
        port = Integer.parseInt(hostPort.nextToken());
    }
    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().closeIdleConnections(1000);
    client.getHostConfiguration().setHost(host, port, "http");
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    PostMethod login = new PostMethod(frameworkUrl + "/aa/j_spring_security_check");
    login.addParameter("j_username", userName);
    login.addParameter("j_password", password);
    try {
        client.executeMethod(login);
    } catch (Exception e) {
        throw new RuntimeException("Error login in " + frameworkUrl + "  status: " + login.getStatusCode()
                + " - " + login.getStatusText());
    }
    login.releaseConnection();
    CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
    Cookie[] logoncookies = cookiespec.match(host, port, "/", false, client.getState().getCookies());
    Cookie sessionCookie = logoncookies[0];
    PostMethod postMethod = new PostMethod("/aa/login");
    postMethod.addParameter("target", frameworkUrl);
    client.getState().addCookie(sessionCookie);
    client.executeMethod(postMethod);
    if (HttpServletResponse.SC_SEE_OTHER != postMethod.getStatusCode()) {
        throw new HttpException("Wrong status code: " + postMethod.getStatusCode());
    }
    String userHandle = null;
    Header headers[] = postMethod.getResponseHeaders();
    for (int i = 0; i < headers.length; ++i) {
        if ("Location".equals(headers[i].getName())) {
            String location = headers[i].getValue();
            int index = location.indexOf('=');
            userHandle = new String(Base64.decode(location.substring(index + 1, location.length())));
        }
    }
    if (userHandle == null) {
        throw new ServiceException("User not logged in.");
    }
    return userHandle;
}

From source file:IPFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    String ip = request.getRemoteAddr();

    HttpServletResponse httpResp = null;

    if (response instanceof HttpServletResponse)
        httpResp = (HttpServletResponse) response;

    StringTokenizer toke = new StringTokenizer(ip, ".");
    int dots = 0;
    String byte1 = "";
    String byte2 = "";
    String client = "";

    while (toke.hasMoreTokens()) {

        ++dots;//  w w  w . j a va 2  s  .c o  m

        //if we've reached the second dot, break and check out the indx
        // value
        if (dots == 1) {

            byte1 = toke.nextToken();

        } else {

            byte2 = toke.nextToken();
            break;
        }
    } //while

    //Piece together half of the client IP address so it can be compared
    // with
    //the forbidden range represented by IPFilter.IP_RANGE
    client = byte1 + "." + byte2;

    if (IP_RANGE.equals(client)) {

        httpResp.sendError(HttpServletResponse.SC_FORBIDDEN, "That means goodbye forever!");

    } else {

        chain.doFilter(request, response);
    }

}

From source file:cn.edu.zjnu.acm.judge.security.password.MultiPasswordSupport.java

@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
    for (StringTokenizer tokenizer = new StringTokenizer(encodedPassword, ","); tokenizer.hasMoreElements();) {
        if (super.matches(rawPassword, tokenizer.nextToken())) {
            return true;
        }/*from w  ww. j av a2  s  .  co m*/
    }
    return false;
}

From source file:com.karki.spring.dao.impl.PaymentDaoImpl.java

@Override
public void loadData(String path) throws IOException, ClassNotFoundException, SQLException {
    Payment payment = new Payment();
    String line = "";
    BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
    while ((line = reader.readLine()) != null) {
        StringTokenizer tokenizer = new StringTokenizer(line, ",");
        payment.setPaymentId(Integer.parseInt(tokenizer.nextToken()));
        payment.setPaymentType(tokenizer.nextToken());

        insert(payment);//from  ww  w . ja  v  a2  s .  com
    }
    reader.close();

}

From source file:com.karki.spring.dao.impl.FacilitiesDaoImpl.java

@Override
public void loadData(String path) throws IOException, ClassNotFoundException, SQLException {
    Facilities fa = new Facilities();
    String line = "";
    BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
    while ((line = reader.readLine()) != null) {
        StringTokenizer tokenizer = new StringTokenizer(line, ",");
        fa.setFacilityId(Integer.parseInt(tokenizer.nextToken()));
        fa.setName(tokenizer.nextToken());
        fa.setFee(Double.parseDouble(tokenizer.nextToken()));
        insert(fa);// w ww  .  j  a v  a2 s .c  om
    }
    reader.close();
}

From source file:com.mycompany.securerest.auth.AuthenticationService.java

public boolean authenticate(String authCredentials) {

    if (null == authCredentials)
        return false;
    // header value format will be "Basic encodedstring" for Basic
    // authentication. Example "Basic YWRtaW46YWRtaW4="
    final String encodedUserPassword = authCredentials.replaceFirst("Basic" + " ", "");
    String usernameAndPassword = null;
    try {//from ww  w .  j  a  v  a 2s  .co  m
        byte[] decodedBytes = Base64.decodeBase64(encodedUserPassword);
        usernameAndPassword = new String(decodedBytes, "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
    }
    final StringTokenizer tokenizer = new StringTokenizer(usernameAndPassword, ":");
    final String username = tokenizer.nextToken();
    final String password = tokenizer.nextToken();

    // we have fixed the userid and password as admin
    // call some UserService/LDAP here
    boolean authenticationStatus = "admin".equals(username) && "admin".equals(password);
    return authenticationStatus;
}

From source file:com.pushinginertia.commons.net.util.HttpServletRequestUtils.java

/**
 * Parses the host name from the request.
 * @param req request received from the user agent
 * @return null if unable to parse/* w  w w .j ava 2s  .  c  o  m*/
 */
public static String getRequestHostName(final HttpServletRequest req) {
    // maybe we are behind a proxy
    String header = req.getHeader(X_FORWARDED_HOST);
    if (header != null) {
        // we are only interested in the first header entry
        header = new StringTokenizer(header, ",").nextToken().trim();
    }
    if (header == null)
        header = req.getHeader(HOST);

    return header;
}

From source file:Main.java

private static String convertStringToTime(String oldTime) {
    if (oldTime == null || oldTime.length() == 0) {
        return "00h00m00s";
    }//from  w w  w . jav  a2 s .c  om

    boolean hasHours = (oldTime.indexOf('h') != -1);
    boolean hasMinutes = (oldTime.indexOf('m') != -1);
    StringBuffer rtn = new StringBuffer(oldTime);

    // Delete the seconds fields and replace other chars with colons
    rtn.deleteCharAt(oldTime.indexOf('s'));

    if (hasMinutes) {
        rtn.setCharAt(oldTime.indexOf('m'), ':');
    } else {
        rtn.insert(0, "00:");
    }

    if (hasHours) {
        rtn.setCharAt(oldTime.indexOf('h'), ':');
    } else {
        rtn.insert(0, "00:");
    }

    StringTokenizer st = new StringTokenizer(rtn.toString(), ":");
    int index = 1;
    String hh = "00";
    String mm = "00";
    String ss = "00";

    while (st.hasMoreTokens()) {
        String toke = st.nextToken();

        switch (index) {
        case 1:
            if (toke.length() < 2) {
                hh = "0" + toke;
            } else {
                hh = toke;
            }
            break;

        case 2:
            if (toke.length() < 2) {
                mm = "0" + toke;
            } else {
                mm = toke;
            }
            break;

        case 3:
            if (toke.length() < 2) {
                ss = "0" + toke;
            } else {
                ss = toke;
            }
            break;
        }

        index++;
    }

    return hh + "h" + mm + "m" + ss + "s";
}