/** NB The following code is shared across Google Maps, Mobile Google Maps and Virtual Earth */
  public void handleRequest(ConveyorTile tile) throws GeoWebCacheException {
    if (tile.getHint() != null) {
      // boolean requestTiled = true;

      if (tile.getHint().equals("not_cached,not_metatiled")) {
        // requestTiled = false;
      } else if (!tile.getHint().equals("not_cached")) {
        throw new GeoWebCacheException("Hint " + tile.getHint() + " is not known.");
      }

      TileLayer tl = tld.getTileLayer(tile.getLayerId());

      if (tl == null) {
        throw new GeoWebCacheException("Unknown layer " + tile.getLayerId());
      }

      if (!tl.isCacheBypassAllowed().booleanValue()) {
        throw new GeoWebCacheException(
            "Layer " + tile.getLayerId() + " is not configured to allow bypassing the cache.");
      }

      tile.setTileLayer(tl);
      tl.getNoncachedTile(tile);

      Service.writeTileResponse(tile, false);
    }
  }
Beispiel #2
0
  private void mockTileLayerDispatcher() throws Exception {
    when(tld.getConfiguration(same(tileLayer))).thenReturn(config);
    when(tld.getConfiguration(same(tileLayerGroup))).thenReturn(config);
    when(tld.getConfiguration(eq(tileLayer.getName()))).thenReturn(config);
    when(tld.getConfiguration(eq(tileLayerGroup.getName()))).thenReturn(config);

    when(tld.getTileLayer(eq(tileLayer.getName()))).thenReturn(tileLayer);
    when(tld.getTileLayer(eq(tileLayerGroup.getName()))).thenReturn(tileLayerGroup);

    when(tld.getLayerNames())
        .thenReturn(ImmutableSet.of(tileLayer.getName(), tileLayerGroup.getName()));
    Iterable<TileLayer> tileLayers =
        ImmutableList.of((TileLayer) tileLayer, (TileLayer) tileLayerGroup);
    when(tld.getLayerList()).thenReturn(tileLayers);

    when(tld.layerExists(eq(tileLayer.getName()))).thenReturn(true);
    when(tld.layerExists(eq(tileLayerGroup.getName()))).thenReturn(true);
  }
Beispiel #3
0
  @Test
  public void testTruncateLayerFully() throws Exception {

    when(tld.getTileLayer(eq(tileLayerGroup.getName())))
        .thenThrow(new GeoWebCacheException("fake"));

    mediator.truncate(tileLayerGroup.getName());
    verify(storageBroker, never()).deleteByGridSetId(anyString(), anyString());

    mediator.truncate(tileLayer.getName());
    verify(storageBroker, times(tileLayer.getGridSubsets().size()))
        .deleteByGridSetId(anyString(), anyString());
  }
Beispiel #4
0
  @Test
  public void testDispatchGetMapDoesntMatchTileCache() throws Exception {
    GetMapRequest request = new GetMapRequest();

    @SuppressWarnings("unchecked")
    Map<String, String> rawKvp = new CaseInsensitiveMap(new HashMap<String, String>());
    request.setRawKvp(rawKvp);

    rawKvp.put("layers", "more,than,one,layer");
    assertDispatchMismatch(request, "more than one layer requested");

    rawKvp.put("layers", "SomeNonCachedLayer");
    when(tld.getTileLayer(eq("SomeNonCachedLayer")))
        .thenThrow(new GeoWebCacheException("layer not found"));
    assertDispatchMismatch(request, "not a tile layer");

    rawKvp.put("layers", tileLayer.getName());

    request.setFormat("badFormat");
    assertDispatchMismatch(request, "not a GWC supported format");

    request.setFormat("image/gif");
    assertDispatchMismatch(request, "no tile cache for requested format");
    request.setFormat(tileLayer.getMimeTypes().get(0).getMimeType());

    request.setSRS("EPSG:4326");
    request.setBbox(new Envelope(10, 10, 20, 20));
    assertDispatchMismatch(request, "request does not align to grid");

    request.setSRS("EPSG:23036");
    assertDispatchMismatch(request, "no cache exists for requested CRS");

    request.setSRS("badCRS");
    assertDispatchMismatch(request, "exception occurred");
    request.setSRS("EPSG:4326");

    request.setWidth(128);
    request.setHeight(256);
    assertDispatchMismatch(request, "request does not align to grid");

    request.setWidth(256);
    request.setHeight(128);
    assertDispatchMismatch(request, "request does not align to grid");

    request.setSRS("EPSG:4326");
    request.setWidth(256);
    request.setHeight(256);
    assertDispatchMismatch(request, "request does not align to grid");
  }
Beispiel #5
0
  protected WMSTileFuser(TileLayerDispatcher tld, StorageBroker sb, HttpServletRequest servReq)
      throws GeoWebCacheException {
    this.sb = sb;

    String[] keys = {
      "layers", "format", "srs", "bbox", "width", "height", "transparent", "bgcolor"
    };

    Map<String, String> values =
        ServletUtils.selectedStringsFromMap(
            servReq.getParameterMap(), servReq.getCharacterEncoding(), keys);

    // TODO Parameter filters?

    String layerName = values.get("layers");
    layer = tld.getTileLayer(layerName);

    List<MimeType> ml = layer.getMimeTypes();
    Iterator<MimeType> iter = ml.iterator();
    while (iter.hasNext()) {
      MimeType mt = iter.next();
      if (mt.getInternalName().equalsIgnoreCase("png")) {
        this.srcFormat = (ImageMime) mt;
      }
    }

    gridSubset = layer.getGridSubsetForSRS(SRS.getSRS(values.get("srs")));

    outputFormat = (ImageMime) ImageMime.createFromFormat(values.get("format"));

    reqBounds = new BoundingBox(values.get("bbox"));

    reqWidth = Integer.valueOf(values.get("width"));

    reqHeight = Integer.valueOf(values.get("height"));

    // if(values[6] != null) {
    // this.reqTransparent = Boolean.valueOf(values[6]);
    // }

    // if(values[7] != null) {
    // this.reqBgColor = values[7];
    // }

    fullParameters =
        layer.getModifiableParameters(servReq.getParameterMap(), servReq.getCharacterEncoding());
  }
Beispiel #6
0
  private GeoServerTileLayer mockTileLayer(String layerName, List<String> gridSetNames)
      throws Exception {

    GeoServerTileLayer tileLayer = mock(GeoServerTileLayer.class);
    when(tld.layerExists(eq(layerName))).thenReturn(true);
    when(tld.getTileLayer(eq(layerName))).thenReturn(tileLayer);
    when(tileLayer.getName()).thenReturn(layerName);
    when(tileLayer.isEnabled()).thenReturn(true);

    final MimeType mimeType1 = MimeType.createFromFormat("image/png");
    final MimeType mimeType2 = MimeType.createFromFormat("image/jpeg");
    when(tileLayer.getMimeTypes()).thenReturn(ImmutableList.of(mimeType1, mimeType2));

    Map<String, GridSubset> subsets = Maps.newHashMap();
    Multimap<SRS, GridSubset> bySrs = LinkedHashMultimap.create();

    GridSetBroker broker = gridSetBroker;

    for (String gsetName : gridSetNames) {
      GridSet gridSet = broker.get(gsetName);
      XMLGridSubset xmlGridSubset = new XMLGridSubset();
      String gridSetName = gridSet.getName();
      xmlGridSubset.setGridSetName(gridSetName);
      GridSubset gridSubSet = xmlGridSubset.getGridSubSet(broker);
      subsets.put(gsetName, gridSubSet);
      bySrs.put(gridSet.getSrs(), gridSubSet);

      when(tileLayer.getGridSubset(eq(gsetName))).thenReturn(gridSubSet);
    }
    for (SRS srs : bySrs.keySet()) {
      List<GridSubset> list = ImmutableList.copyOf(bySrs.get(srs));
      when(tileLayer.getGridSubsetsForSRS(eq(srs))).thenReturn(list);
    }
    when(tileLayer.getGridSubsets()).thenReturn(subsets.keySet());

    // sanity check
    for (String gsetName : gridSetNames) {
      assertTrue(tileLayer.getGridSubsets().contains(gsetName));
      assertNotNull(tileLayer.getGridSubset(gsetName));
    }

    return tileLayer;
  }
Beispiel #7
0
  private void validateLayerQuota(DiskQuotaConfig quotaConfig, LayerQuota lq)
      throws ConfigurationException {
    String layer = lq.getLayer();
    try {
      tileLayerDispatcher.getTileLayer(layer);
    } catch (GeoWebCacheException e) {
      log.error(
          "LayerQuota configuration error: layer "
              + layer
              + " does not exist. Removing quota from runtime configuration.",
          e);
      quotaConfig.remove(lq);
    }

    final ExpirationPolicy expirationPolicyName = lq.getExpirationPolicyName();
    if (expirationPolicyName == null) {
      // if expiration policy is not defined, then there should be no quota defined either,
      // as it means the layer is managed by the global expiration policy, if any
      if (lq.getQuota() != null) {
        throw new ConfigurationException(
            "Layer "
                + lq.getLayer()
                + " has no expiration policy, but does have a quota defined. "
                + "Either both or neither should be present");
      }
      return;
    }

    Quota quota = lq.getQuota();
    try {
      validateQuota(quota);
    } catch (ConfigurationException e) {
      log.error(
          "LayerQuota configuration error for layer "
              + layer
              + ". Error message is: "
              + e.getMessage()
              + ". Quota removed from runtime configuration.");
      quotaConfig.remove(lq);
    }
  }
Beispiel #8
0
  public static void makeMap(
      TileLayerDispatcher tileLayerDispatcher,
      GridSetBroker gridSetBroker,
      String action,
      HttpServletRequest request,
      HttpServletResponse response)
      throws GeoWebCacheException {

    String page = null;

    // Do we have a layer, or should we make a list?
    if (action != null) {
      String layerName = ServletUtils.URLDecode(action, request.getCharacterEncoding());

      TileLayer layer = tileLayerDispatcher.getTileLayer(layerName);

      String rawGridSet = request.getParameter("gridSet");
      String gridSetStr = null;
      if (rawGridSet != null)
        gridSetStr = ServletUtils.URLDecode(rawGridSet, request.getCharacterEncoding());

      if (gridSetStr == null) {
        gridSetStr = request.getParameter("srs");

        if (gridSetStr == null) {
          gridSetStr = layer.getGridSubsets().iterator().next();
        }
      }

      String formatStr = request.getParameter("format");

      if (formatStr != null) {
        if (!layer.supportsFormat(formatStr)) {
          throw new GeoWebCacheException("Unknow or unsupported format " + formatStr);
        }
      } else {
        formatStr = layer.getDefaultMimeType().getFormat();
      }

      if (request.getPathInfo().startsWith("/demo")) {
        // Running in GeoServer
        page = generateHTML(layer, gridSetStr, formatStr, true);
      } else {
        page = generateHTML(layer, gridSetStr, formatStr, false);
      }

    } else {
      if (request.getRequestURI().endsWith("/")) {
        try {
          String reqUri = request.getRequestURI();
          response.sendRedirect(
              response.encodeRedirectURL(reqUri.substring(0, reqUri.length() - 1)));
        } catch (IOException e) {
          e.printStackTrace();
        }
        return;
      } else {
        page = generateHTML(tileLayerDispatcher, gridSetBroker);
      }
    }
    response.setContentType("text/html");
    response.setCharacterEncoding("UTF-8");

    try {
      response.getOutputStream().write(page.getBytes());
    } catch (IOException ioe) {
      throw new GeoWebCacheException("failed to render HTML");
    }
  }
Beispiel #9
0
  private static String tableRows(
      TileLayerDispatcher tileLayerDispatcher, GridSetBroker gridSetBroker)
      throws GeoWebCacheException {
    StringBuffer buf = new StringBuffer();

    Set<String> layerList = new TreeSet<String>(tileLayerDispatcher.getLayerNames());
    for (String layerName : layerList) {
      TileLayer layer = tileLayerDispatcher.getTileLayer(layerName);
      buf.append(
          "<tr><td style=\"min-width: 100px;\"><strong>" + layer.getName() + "</strong><br />\n");
      buf.append("<a href=\"rest/seed/" + layer.getName() + "\">Seed this layer</a>\n");
      buf.append("</td><td>" + layer.isEnabled() + "</td>");
      buf.append("<td><table width=\"100%\">");

      int count = 0;
      for (String gridSetId : layer.getGridSubsets()) {
        GridSubset gridSubset = layer.getGridSubset(gridSetId);
        String gridSetName = gridSubset.getName();
        if (gridSetName.length() > 20) {
          gridSetName = gridSetName.substring(0, 20) + "...";
        }
        buf.append("<tr><td style=\"width: 170px;\">").append(gridSetName);

        buf.append("</td><td>OpenLayers: [");
        Iterator<MimeType> mimeIter = layer.getMimeTypes().iterator();
        boolean prependComma = false;
        while (mimeIter.hasNext()) {
          MimeType mime = mimeIter.next();
          if (mime instanceof ImageMime) {
            if (prependComma) {
              buf.append(", ");
            } else {
              prependComma = true;
            }
            buf.append(generateDemoUrl(layer.getName(), gridSubset.getName(), (ImageMime) mime));
          }
        }
        buf.append("]</td><td>\n");

        if (gridSubset.getName().equals(gridSetBroker.WORLD_EPSG4326.getName())) {
          buf.append(" &nbsp; KML: [");
          String prefix = "";
          prependComma = false;
          Iterator<MimeType> kmlIter = layer.getMimeTypes().iterator();
          while (kmlIter.hasNext()) {
            MimeType mime = kmlIter.next();
            if (mime instanceof ImageMime || mime == XMLMime.kml) {
              if (prependComma) {
                buf.append(", ");
              } else {
                prependComma = true;
              }
              buf.append(
                  "<a href=\""
                      + prefix
                      + "service/kml/"
                      + layer.getName()
                      + "."
                      + mime.getFileExtension()
                      + ".kml\">"
                      + mime.getFileExtension()
                      + "</a>");
            } else if (mime == XMLMime.kmz) {
              if (prependComma) {
                buf.append(", ");
              } else {
                prependComma = true;
              }
              buf.append(
                  "<a href=\"" + prefix + "service/kml/" + layer.getName() + ".kml.kmz\">kmz</a>");
            }
          }
          buf.append("]");
        } else {
          // No Google Earth support
        }
        buf.append("</td></tr>");
        count++;
      }

      // if(count == 0) {
      // buf.append("<tr><td colspan=\"2\"><i>None</i></td></tr>\n");
      // }

      buf.append("</table></td>\n");
      buf.append("</tr>\n");
    }

    return buf.toString();
  }