Package org.olat.core.dispatcher.mapper

Source Code of org.olat.core.dispatcher.mapper.MapperRegistry

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) 1999-2006 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.core.dispatcher.mapper;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.olat.core.dispatcher.Dispatcher;
import org.olat.core.dispatcher.DispatcherAction;
import org.olat.core.gui.media.MediaResource;
import org.olat.core.gui.media.NotFoundMediaResource;
import org.olat.core.gui.media.ServletUtil;
import org.olat.core.logging.OLATSecurityException;
import org.olat.core.logging.Tracing;
import org.olat.core.util.UserSession;
import org.olat.core.util.WebappHelper;

/**
* Description:<br>
* Allows to register so called mappers, which map a certain session-unique url
* to a mapper which can dispatch requests to that url.
* <P>
* Initial Date: 10.06.2005 <br>
*
* @author Felix Jost
*/
public class MapperRegistry implements Dispatcher {
  private static final String CNAME = MapperRegistry.class.getName();
  private static final MediaResource NOTFOUND = new NotFoundMediaResource("not found");

  private Map<String, Mapper> map = new HashMap<String, Mapper>();
  private int mapid;

  /**
   *
   */
  private MapperRegistry() {
    // to avoid browser caching if logging in again using the same browser (css
    // cache seems not to be emptied even if browser is closed...)
    mapid = ((int) (System.currentTimeMillis() % 1000l)) * 100;
  }

  /**
   * @param us
   * @return MapperRegistry
   */
  public static MapperRegistry getInstanceFor(UserSession us) {
    MapperRegistry mreg;
      mreg = (MapperRegistry) us.getEntry(CNAME);
      if (mreg == null) {
        synchronized (us) {
          // need to sync since a browser may request many e.g. pictures in parallel
          mreg = (MapperRegistry) us.getEntry(CNAME);
          if(mreg == null){
            mreg = new MapperRegistry();
            us.putEntry(CNAME, mreg);           
          }
        }
      }
    //}
    return mreg;
  }

  /**
   * @param mapper
   * @return the path e.g. /olat/m/1001 without / at the end
   */
  public String register(Mapper mapper) {
    int cur = mapid;
    map.put(String.valueOf(cur), mapper);
    mapid++; // do not sync instance var inc since mappers of a user will not
              // be allocated concurrently (should be done in constructors of
              // controllers only / = one gui thread).
    return WebappHelper.getServletContextPath() + DispatcherAction.PATH_MAPPED + cur;
  }

  /**
   * @param hreq
   * @param hres
   */
  public void execute(HttpServletRequest hreq, HttpServletResponse hres, String pathInfo) {
    final boolean isDebugLog = Tracing.isDebugEnabled(MapperRegistry.class);
    StringBuilder debugMsg = null;
    long debug_start = 0;
    if (isDebugLog) {
      debug_start = System.currentTimeMillis();
      debugMsg = new StringBuilder("::mprex:");
    }
   
    // e.g. 23423/bla/blu.html
    String subInfo = pathInfo.substring(DispatcherAction.PATH_MAPPED.length());
    int slashPos = subInfo.indexOf('/');
    if (slashPos == -1) {
      DispatcherAction.sendNotFound("not found", hres);
      return;
    }

    // e.g. 23423
    String smappath = subInfo.substring(0, slashPos);
    Mapper m = map.get(smappath);
    MediaResource mr;
    if (m == null) { // not mapped
      mr = NOTFOUND;
    } else {
      String mod = subInfo.substring(slashPos);
      if (mod.indexOf("..") != -1) throw new OLATSecurityException("mapper path contained '..' : " + mod);
      // /bla/blu.html
      mr = m.handle(mod, hreq);
    }
    ServletUtil.serveResource(hreq, hres, mr);
   
    if (isDebugLog) {
      long afterserved = System.currentTimeMillis();
      long syncIntroDiff = afterserved - debug_start;
      debugMsg.append("nanoseconds:").append(syncIntroDiff);
      Tracing.logDebug(debugMsg.toString(), MapperRegistry.class);
    }
  }

  /**
   * remove a registered mapper if it is no longer needed.
   *
   * @param mapper
   */
  public void deregister(Mapper mapper) {
    map.values().remove(mapper);
  }

}
TOP

Related Classes of org.olat.core.dispatcher.mapper.MapperRegistry

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.