Skip to content

Commit

Permalink
Fallback when no request info can be retrieved.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Nov 24, 2015
1 parent d0d0cc9 commit 9985ad7
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 4 deletions.
Expand Up @@ -30,6 +30,8 @@
@Since("5.0.4")
public interface IReqInfo {

boolean exists();

boolean isGetReq();

String verb();
Expand Down
103 changes: 103 additions & 0 deletions rapidoid-gui/src/main/java/org/rapidoid/gui/reqinfo/NoReqInfo.java
@@ -0,0 +1,103 @@
package org.rapidoid.gui.reqinfo;

/*
* #%L
* rapidoid-gui
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors
* %%
* 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.
* #L%
*/

import java.util.Collections;
import java.util.Map;
import java.util.Set;

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;

@SuppressWarnings("unchecked")
@Authors("Nikolche Mihajlovski")
@Since("5.0.5")
public class NoReqInfo extends AbstractReqInfo {

@SuppressWarnings({ "rawtypes" })
private static final Map EMPTY = Collections.EMPTY_MAP;

@Override
public boolean isGetReq() {
return true;
}

@Override
public String verb() {
return null;
}

@Override
public String path() {
return null;
}

@Override
public String uri() {
return null;
}

@Override
public String host() {
return null;
}

@Override
public Map<String, Object> data() {
return EMPTY;
}

@Override
public Map<String, String> params() {
return EMPTY;
}

@Override
public Map<String, Object> posted() {
return EMPTY;
}

@Override
public Map<String, byte[]> files() {
return EMPTY;
}

@Override
public Map<String, String> headers() {
return EMPTY;
}

@Override
public Map<String, String> cookies() {
return EMPTY;
}

@Override
public String username() {
return null;
}

@Override
public Set<String> roles() {
return Collections.EMPTY_SET;
}

}
Expand Up @@ -23,13 +23,22 @@
import java.util.Map;
import java.util.Set;

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.ctx.Ctxs;
import org.rapidoid.ctx.UserInfo;
import org.rapidoid.http.Req;
import org.rapidoid.u.U;

@Authors("Nikolche Mihajlovski")
@Since("5.0.4")
public class RapidoidReqInfo extends AbstractReqInfo {

@Override
public boolean exists() {
return Ctxs.get() != null;
}

private Req req() {
return Ctxs.ctx().exchange();
}
Expand Down
Expand Up @@ -23,12 +23,13 @@
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.cls.Cls;
import org.rapidoid.u.U;

@Authors("Nikolche Mihajlovski")
@Since("5.0.4")
public class ReqInfo {

private static final NoReqInfo NO_REQ_INFO = new NoReqInfo();

private static final String RAPIDOID_CTX = "org.rapidoid.ctx.Ctx";

static volatile IReqInfo INFO;
Expand All @@ -38,15 +39,15 @@ public static IReqInfo get() {
INFO = createInfo();
}

return INFO;
return INFO.exists() ? INFO : NO_REQ_INFO;
}

private static RapidoidReqInfo createInfo() {
private static IReqInfo createInfo() {
if (Cls.exists(RAPIDOID_CTX)) {
return new RapidoidReqInfo();
}

throw U.rte("Cannot find request info provider!");
return NO_REQ_INFO;
}

}

0 comments on commit 9985ad7

Please sign in to comment.