All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xipki.http.servlet.ServletURI Maven / Gradle / Ivy

The newest version!
/*
 *
 * Copyright (c) 2013 - 2018 Lijun Liao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xipki.http.servlet;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

/**
 * TODO.
 * @author Lijun Liao
 * @since 2.2.0
 */

// CHECKSTYLE:SKIP
public class ServletURI {

  private String path;

  private String fragement;

  private String query;

  private Map parameters;

  public ServletURI(String rawPath) throws URISyntaxException {
    if (rawPath == null || rawPath.isEmpty() || "/".equals(rawPath)) {
      path = "/";
    } else {
      URI uri = new URI(rawPath);
      path = uri.getPath();
      if (path == null || path.isEmpty()) {
        path = "/";
      } else if (path.charAt(0) != '/') {
        path += "/" + path;
      }

      fragement = uri.getFragment();
      query = uri.getQuery();
    }
  }

  public void setPath(String path) {
    this.path = (path == null || path.isEmpty()) ? "/" : path;
  }

  public String getPath() {
    return path;
  }

  public String getQuery() {
    return query;
  }

  public String getFragement() {
    return fragement;
  }

  public Set getParameterNames() {
    initParameters();
    return parameters.keySet();
  }

  public String getParameter(String name) {
    initParameters();
    return parameters.get(name);
  }

  public Map getParameters() {
    initParameters();
    return parameters;
  }

  private void initParameters() {
    if (parameters == null) {
      parameters = new HashMap<>();
      if (query != null) {
        StringTokenizer st = new StringTokenizer(query, "&");
        while (st.hasMoreTokens()) {
          String token = st.nextToken();
          int idx = token.indexOf('=');
          if (idx != -1 && idx != token.length()) {
            String pn = token.substring(0, idx);
            String pv = token.substring(idx + 1);
            parameters.put(pn, pv);
          }
        }
      }
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy