本文整理汇总了Java中fi.iki.elonen.NanoHTTPD.IHTTPSession类的典型用法代码示例。如果您正苦于以下问题:Java IHTTPSession类的具体用法?Java IHTTPSession怎么用?Java IHTTPSession使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IHTTPSession类属于fi.iki.elonen.NanoHTTPD包,在下文中一共展示了IHTTPSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doGet
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入依赖的package包/类
public Response doGet(IHTTPSession session) {
String uri = session.getUri();
String path = uri.substring(getRequestMappingURL().length());
File result = new File(basePath, path);
String requestCanonicalPath;
try {
requestCanonicalPath = result.getCanonicalPath();
} catch (IOException e1) {
return new503ErrorResponse();
}
//prevent escape from configured base directory
if (!requestCanonicalPath.startsWith(canonicalBasePath) || !result.exists()) {
return NanoHTTPD.newFixedLengthResponse(fi.iki.elonen.NanoHTTPD.Response.Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, "not found");
}
try {
return NanoHTTPD.newFixedLengthResponse(fi.iki.elonen.NanoHTTPD.Response.Status.OK, getMimeType(uri), new FileInputStream(result), result.length());
} catch (FileNotFoundException e) {
return new503ErrorResponse();
}
}
示例2: doPost
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入依赖的package包/类
@Override
public ModelAndView doPost(IHTTPSession session) {
JsonValue request = Json.parse(WebServer.getRequestBody(session));
if (!request.isObject()) {
return new BadRequest("expected object");
}
ValidationResult errors = new ValidationResult();
String username = ((JsonObject) request).getString("username", null);
if (username == null || username.trim().length() == 0) {
errors.put("username", "Cannot be empty");
}
if (!errors.isEmpty()) {
LOG.info("unable to save: " + errors);
return new BadRequest(errors);
}
auth.resetPassword(username);
return new Success();
}
示例3: doGet
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入依赖的package包/类
@Override
public ModelAndView doGet(IHTTPSession session) {
ModelAndView result = new ModelAndView();
JsonArray array = new JsonArray();
for (Entry<String, Metric> cur : ru.r2cloud.metrics.Metrics.REGISTRY.getMetrics().entrySet()) {
JsonObject curObject = new JsonObject();
curObject.add("id", cur.getKey());
curObject.add("url", "/admin/static/rrd/" + cur.getKey() + ".rrd");
if (cur.getValue() instanceof FormattedCounter) {
curObject.add("format", ((FormattedCounter) cur.getValue()).getFormat().toString());
}
if (cur.getValue() instanceof FormattedGauge<?>) {
curObject.add("format", ((FormattedGauge<?>) cur.getValue()).getFormat().toString());
}
array.add(curObject);
}
result.setData(array.toString());
return result;
}
示例4: doGet
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入依赖的package包/类
@Override
public ModelAndView doGet(IHTTPSession session) {
ModelAndView result = new ModelAndView();
JsonObject entity = new JsonObject();
Long lastUpdateMillis = config.getLong("satellites.tle.lastupdateAtMillis");
if (lastUpdateMillis != null) {
entity.add("lastUpdated", lastUpdateMillis);
}
JsonArray tle = new JsonArray();
for (Entry<String, ru.r2cloud.model.TLE> cur : service.findAll().entrySet()) {
JsonObject curTle = new JsonObject();
curTle.add("id", cur.getKey());
JsonArray curData = new JsonArray();
for (String curDataEntry : cur.getValue().getRaw()) {
curData.add(curDataEntry);
}
curTle.add("data", curData);
tle.add(curTle);
}
entity.add("tle", tle);
result.setData(entity.toString());
return result;
}
示例5: doGet
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入依赖的package包/类
@Override
public ModelAndView doGet(IHTTPSession session) {
ModelAndView result = new ModelAndView();
JsonArray array = (JsonArray) Json.array();
for (Airplane cur : dao.getAirplanes()) {
JsonArray positions = (JsonArray) Json.array();
if (cur.getPositions() != null) {
for (Position curPosition : cur.getPositions()) {
positions.add(Json.object().add("lng", curPosition.getLongitude()).add("lat", curPosition.getLatitude()));
}
}
array.add(Json.object().add("icao24", cur.getIcao24()).add("positions", positions));
}
result.setData(array.toString());
return result;
}
示例6: doGet
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入依赖的package包/类
@Override
public ModelAndView doGet(IHTTPSession session) {
ModelAndView result = new ModelAndView();
JsonObject entity = new JsonObject();
entity.add("domain", acmeClient.getSslDomain());
entity.add("enabled", acmeClient.isSSLEnabled());
entity.add("running", acmeClient.isRunning());
entity.add("agreeWithToC", acmeClient.isSSLEnabled() || acmeClient.isRunning());
JsonArray messages = new JsonArray();
for (String cur : acmeClient.getMessages()) {
JsonObject curObject = new JsonObject();
curObject.add("message", cur);
messages.add(curObject);
}
entity.add("log", messages);
result.setData(entity.toString());
return result;
}
示例7: doPost
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入依赖的package包/类
@Override
public ModelAndView doPost(IHTTPSession session) {
JsonValue request = Json.parse(WebServer.getRequestBody(session));
if (!request.isObject()) {
return new BadRequest("expected object");
}
ValidationResult errors = new ValidationResult();
Double lat = WebServer.getDouble(request, "lat");
Double lon = WebServer.getDouble(request, "lng");
if (lat == null) {
errors.put("lat", "Cannot be empty");
}
if (lon == null) {
errors.put("lng", "Cannot be empty");
}
if (!errors.isEmpty()) {
LOG.info("unable to save: " + errors);
return new BadRequest(errors);
}
autoUpdate.setEnabled(WebServer.getBoolean(request, "autoUpdate"));
config.setProperty("locaiton.lat", String.valueOf(lat));
config.setProperty("locaiton.lon", String.valueOf(lon));
config.update();
return new Success();
}
示例8: isAuthenticated
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入依赖的package包/类
public boolean isAuthenticated(IHTTPSession session) {
if (authenticatedToken == null) {
return false;
}
if (System.currentTimeMillis() - authenticatedAt > maxAgeMillis) {
if (LOG.isDebugEnabled()) {
LOG.debug("session expired");
}
authenticatedToken = null;
return false;
}
String values = session.getHeaders().get("authorization");
if (values == null) {
return false;
}
int index = values.indexOf(' ');
if (index == -1) {
return false;
}
return values.substring(index + 1).equals(authenticatedToken);
}
示例9: processRequest
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入依赖的package包/类
/**
* proesses the request from the given session.
*
* @param controller
* the controller to use.
* @param session
* the session to use.
* @return the response.
*/
private static String processRequest(final LocalControllerNode controller, final IHTTPSession session) {
try {
final long readTimeOut = controller.getLocalNodeData().getRpcClientTimeoutMillis();
final InputStream in = session.getInputStream();
if (LOG.isDebugEnabled()) {
LOG.debug("host:{};headers:{}", session.getRemoteHostName(), session.getHeaders());
}
final int contentLength = Integer.valueOf(session.getHeaders().get("content-length"));
final byte[] ba = new byte[contentLength];
InputStreamUtil.readUntilFull(readTimeOut, in, ba);
final String requestStr = new String(ba);
if (LOG.isDebugEnabled()) {
LOG.debug("host:{};request:{}", session.getRemoteHostName(), requestStr);
}
final JSONObject response = RpcServerUtil.process(controller, session.getUri(), requestStr);
final String responseStr = response.toString();
if (LOG.isDebugEnabled()) {
LOG.debug("host:{};response:{}", session.getRemoteHostName(), responseStr);
}
return responseStr;
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
示例10: get
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入依赖的package包/类
@Override
public Response get(UriResource uriResource, Map<String, String> urlParams, IHTTPSession session) {
Method method = session.getMethod();
String uri = session.getUri();
//Log.d(TAG, "Method: " + method + ", Url: " + uri);
try {
EpubFetcher fetcher = uriResource.initParameter(EpubFetcher.class);
int offset = uri.indexOf("/", 0);
int startIndex = uri.indexOf("/", offset + 1);
String filePath = uri.substring(startIndex + 1);
Link link = fetcher.publication.getResourceMimeType(filePath);
String mimeType = link.getTypeLink();
InputStream inputStream = fetcher.getDataInputStream(filePath);
response = serveResponse(session, inputStream, mimeType);
} catch (EpubFetcherException e) {
e.printStackTrace();
return NanoHTTPD.newFixedLengthResponse(Status.INTERNAL_ERROR, getMimeType(), ResponseStatus.FAILURE_RESPONSE);
}
return response;
}
示例11: invoke
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入依赖的package包/类
static void invoke(IHTTPSession session, NanoHTTPDHandler handler) {
Method method = session.getMethod();
String urlPath = session.getUri();
String queryParameters = session.getQueryParameterString();
if (method == Method.POST || method == Method.PUT) {
String body = null;
String contentType = session.getHeaders().get("content-type");
String requestLength = session.getHeaders().get("content-length");
if (requestLength != null) {
byte[] bytes = new byte[Integer.parseInt(requestLength)];
try {
IOUtils.readFully(session.getInputStream(), bytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
body = new String(bytes, UTF_8);
}
handler.handle(method, urlPath, queryParameters, contentType, body);
} else {
handler.handle(method, urlPath, queryParameters);
}
}
示例12: doGet
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入依赖的package包/类
@Override
public void doGet(IHTTPSession session, Response response) {
if (!webServer.getUser().isAdmin()) {
response.setStatus(Response.Status.FORBIDDEN);
return;
}
response.setMimeType("application/json");
String pkname = session.getParms().get("id");
if (StringUtils.isNotBlank(pkname)) {
User user = PersistenceManager.getDao(User.class).findOne(Long.valueOf(pkname));
if (user != null) {
response.setData(VntUtil.getInputStream(VntUtil.toJson(user)));
}
} else {
response.setData(VntUtil.getInputStream(VntUtil.toJson(PersistenceManager.getDao(User.class).findAll())));
}
}
示例13: doGet
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入依赖的package包/类
@Override
public void doGet(IHTTPSession session, Response response) {
String torrentString = null;
try {
torrentString = URLDecoder.decode(session.getParms().get("torrent"), "UTF-8");
torrentString = session.getParms().get("torrent");
log.info(torrentString);
}
catch (UnsupportedEncodingException e) {
log.error("Error decoding torrent param", e);
}
Torrent torrent = VntUtil.fromJson(torrentString, Torrent.class);
InputStream inputStream = null;
if (torrent != null) {
TrackerManager trackerManager = TrackerManagerFactory.getInstance(torrent.getTracker());
trackerManager.setUser(torrent.getUsername(), torrent.getPassword());
inputStream = trackerManager.download(torrent);
response.addHeader("Content-Disposition", "attachment; filename=\"" + torrent.getName() + ".torrent\"");
response.setMimeType("application/octet-stream; charset=UTF-8");
response.setChunkedTransfer(true);
}
response.setData(inputStream);
}
示例14: doGet
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入依赖的package包/类
@Override
public void doGet(IHTTPSession session, Response response) {
Boolean close = Boolean.valueOf(session.getParms().get("c"));
String seedbox = session.getParms().get("seedbox");
Torrent torrent = VntUtil.fromJson(session.getParms().get("torrent"), Torrent.class);
if (StringUtils.isNotBlank(seedbox) && torrent != null) {
TrackerManager trackerManager = TrackerManagerFactory.getInstance(torrent.getTracker());
trackerManager.setUser(torrent.getUsername(), torrent.getPassword());
Seedbox seedboxConfig = PersistenceManager.getDao(Seedbox.class).findOne(Long.valueOf(seedbox));
Wicker wicker = new WickerFactory(seedboxConfig.getWebClient().getSimpleName()).newInstance(seedboxConfig.getUrl(), seedboxConfig.getUsername(), seedboxConfig.getPassword());
wicker.uploadTorrent(trackerManager.download(torrent), seedboxConfig.getLabel());
}
if (close) {
response.setData(VntUtil.getInputStream("<script type='text/javascript'>alert('Torrent sent with success.');window.close();</script>"));
} else {
response.setMimeType("application/json; charset=UTF-8");
ReturnObject returnObject = new ReturnObject(true, null);
response.setData(VntUtil.getInputStream(VntUtil.toJson(returnObject)));
}
}
示例15: doGet
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入依赖的package包/类
@Override
public void doGet(IHTTPSession session, Response response) {
response.setMimeType("application/json");
String id = session.getParms().get("id");
boolean count = Boolean.valueOf(session.getParms().get("count"));
if (count) {
response.setData(VntUtil.getInputStream(VntUtil.toJson(new ReturnObject(true, webServer.getUser().getJobs().size()))));
} else if (StringUtils.isNotBlank(id)) {
Job job = PersistenceManager.getDao(Job.class).findOne(Long.valueOf(id));
if (job != null) {
response.setData(VntUtil.getInputStream(VntUtil.toJson(job)));
}
} else {
response.setData(VntUtil.getInputStream(VntUtil.toJson(webServer.getUser().getJobs())));
}
}