Skip to content

Commit

Permalink
Separated the HTTP impl. internals out from the HTTP API.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jun 22, 2015
1 parent a049f1e commit 4779efb
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 90 deletions.
10 changes: 7 additions & 3 deletions rapidoid-app/src/main/java/org/rapidoid/app/AppHandler.java
Expand Up @@ -24,6 +24,7 @@
import org.rapidoid.annotation.Since;
import org.rapidoid.http.Handler;
import org.rapidoid.http.HttpExchange;
import org.rapidoid.http.HttpExchangeInternals;
import org.rapidoid.http.HttpNotFoundException;
import org.rapidoid.http.HttpProtocol;
import org.rapidoid.json.JSON;
Expand Down Expand Up @@ -52,7 +53,8 @@ public AppHandler(CustomizableClassLoader classLoader) {

@Override
public Object handle(final HttpExchange x) throws Exception {
x.setClassLoader(classLoader);
HttpExchangeInternals xi = (HttpExchangeInternals) x;
xi.setClassLoader(classLoader);

Object result;

Expand All @@ -78,15 +80,17 @@ public Object handle(final HttpExchange x) throws Exception {
}

static Object processReq(HttpExchange x) {
HttpExchangeInternals xi = (HttpExchangeInternals) x;

if (x.isPostReq()) {
String state = x.data("__state", null);
if (!U.isEmpty(state) && !state.equals("null")) {
byte[] bytes = JSON.parseBytes('"' + state + '"');
x.deserializeLocals(bytes);
xi.deserializeLocals(bytes);
}
}

final AppClasses appCls = Apps.getAppClasses(x, x.getClassLoader());
final AppClasses appCls = Apps.getAppClasses(x, xi.getClassLoader());

WebPojoDispatcher dispatcher = (WebPojoDispatcher) appCls.ctx.get(DISPATCHER);

Expand Down
Expand Up @@ -28,7 +28,6 @@

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.annotation.TransactionMode;
import org.rapidoid.ctx.AppExchange;
import org.rapidoid.mime.MediaType;

Expand Down Expand Up @@ -103,7 +102,7 @@ public interface HttpExchange extends AppExchange {

Map<String, Object> locals();

Map<String, Object> getSessionById(String sessionId);
String sessionId();

<T> T session(String name);

Expand All @@ -113,42 +112,20 @@ public interface HttpExchange extends AppExchange {

<T extends Serializable> T sessionGetOrCreate(String name, Class<T> valueClass, Object... constructorArgs);

String sessionId();

void closeSession();

void clearSession(String sessionId);

boolean hasSession();

boolean hasSession(String sessionId);

String pathSegment(int segmentIndex);

boolean isGetReq();

boolean isPostReq();

TransactionMode getTransactionMode();

void setTransactionMode(TransactionMode txMode);

void setClassLoader(ClassLoader classLoader);

ClassLoader getClassLoader();

/* RESPONSE: */

String constructUrl(String path);

byte[] sessionSerialize();

void sessionDeserialize(byte[] bytes);

byte[] serializeLocals();

void deserializeLocals(byte[] bytes);

<T> T extra(Object key);

void extra(Object key, Object value);
Expand Down Expand Up @@ -195,9 +172,7 @@ public interface HttpExchange extends AppExchange {

String redirectUrl();

boolean serveStatic();

/* BODY */
boolean serveStaticFile();

HttpExchange sendFile(File file);

Expand Down Expand Up @@ -225,8 +200,6 @@ public interface HttpExchange extends AppExchange {

HttpExchange writeJSON(Object value);

HttpExchange send();

// due to async() web handling option, it ain't over till the fat lady sings "done"
HttpExchange async();

Expand Down
36 changes: 11 additions & 25 deletions rapidoid-http/src/main/java/org/rapidoid/http/HttpExchangeImpl.java
Expand Up @@ -31,7 +31,6 @@

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.annotation.TransactionMode;
import org.rapidoid.cls.Cls;
import org.rapidoid.config.Conf;
import org.rapidoid.ctx.UserInfo;
Expand All @@ -56,8 +55,8 @@

@Authors("Nikolche Mihajlovski")
@Since("2.0.0")
public class HttpExchangeImpl extends DefaultExchange<HttpExchange> implements LowLevelHttpExchange, HttpInterception,
Constants {
public class HttpExchangeImpl extends DefaultExchange<HttpExchangeImpl> implements LowLevelHttpExchange,
HttpExchangeInternals, HttpInterception, Constants {

public static final String SESSION_COOKIE = "JSESSIONID";

Expand Down Expand Up @@ -87,8 +86,6 @@ public class HttpExchangeImpl extends DefaultExchange<HttpExchange> implements L
final BoolWrap isGet = new BoolWrap();
final BoolWrap isKeepAlive = new BoolWrap();

private TransactionMode txMode = null;

private boolean parsedParams;
private boolean parsedHeaders;
private boolean parsedBody;
Expand Down Expand Up @@ -157,7 +154,6 @@ public synchronized void reset() {

isGet.value = false;
isKeepAlive.value = false;
txMode = null;

extras = null;

Expand Down Expand Up @@ -315,7 +311,7 @@ public synchronized HttpExchangeImpl done() {
}

@Override
public synchronized HttpExchange send() {
public synchronized HttpExchangeImpl send() {
conn.send();
return this;
}
Expand Down Expand Up @@ -601,37 +597,37 @@ public synchronized void ensureHeadersComplete() {
}

@Override
public synchronized HttpExchange write(String s) {
public synchronized HttpExchangeImpl write(String s) {
ensureHeadersComplete();
return super.write(s);
}

@Override
public synchronized HttpExchange writeln(String s) {
public synchronized HttpExchangeImpl writeln(String s) {
ensureHeadersComplete();
return super.writeln(s);
}

@Override
public synchronized HttpExchange write(byte[] bytes) {
public synchronized HttpExchangeImpl write(byte[] bytes) {
ensureHeadersComplete();
return super.write(bytes);
}

@Override
public synchronized HttpExchange write(byte[] bytes, int offset, int length) {
public synchronized HttpExchangeImpl write(byte[] bytes, int offset, int length) {
ensureHeadersComplete();
return super.write(bytes, offset, length);
}

@Override
public synchronized HttpExchange write(ByteBuffer buf) {
public synchronized HttpExchangeImpl write(ByteBuffer buf) {
ensureHeadersComplete();
return super.write(buf);
}

@Override
public synchronized HttpExchange write(File file) {
public synchronized HttpExchangeImpl write(File file) {
if (!hasContentType()) {
download(file.getName());
}
Expand All @@ -641,7 +637,7 @@ public synchronized HttpExchange write(File file) {
}

@Override
public synchronized HttpExchange writeJSON(Object value) {
public synchronized HttpExchangeImpl writeJSON(Object value) {
if (!hasContentType()) {
json();
}
Expand Down Expand Up @@ -952,7 +948,7 @@ public synchronized HttpExchange authorize(Class<?> clazz) {
}

@Override
public synchronized boolean serveStatic() {
public synchronized boolean serveStaticFile() {
if (isGetReq()) {
String filename = path().substring(1);

Expand Down Expand Up @@ -1074,16 +1070,6 @@ public String realAddress() {
return header("X-Forwarded-For", address());
}

@Override
public synchronized TransactionMode getTransactionMode() {
return txMode;
}

@Override
public synchronized void setTransactionMode(TransactionMode txMode) {
this.txMode = txMode;
}

@Override
public synchronized void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
Expand Down
@@ -0,0 +1,51 @@
package org.rapidoid.http;

/*
* #%L
* rapidoid-http
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski
* %%
* 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.Map;

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

@Authors("Nikolche Mihajlovski")
@Since("2.0.0")
public interface HttpExchangeInternals extends AppExchange {

Map<String, Object> getSessionById(String sessionId);

void clearSession(String sessionId);

boolean hasSession(String sessionId);

void setClassLoader(ClassLoader classLoader);

ClassLoader getClassLoader();

byte[] sessionSerialize();

void sessionDeserialize(byte[] bytes);

byte[] serializeLocals();

void deserializeLocals(byte[] bytes);

}
Expand Up @@ -111,7 +111,7 @@ public void dispatch(HttpExchangeImpl x) {
Range path = x.path_().range();

if (x.isGetReq() && BytesUtil.find(buf.bytes(), path.start + 1, path.limit(), (byte) '.', true) >= 0) {
if (x.serveStatic()) {
if (x.serveStaticFile()) {
return;
}
}
Expand Down
Expand Up @@ -63,4 +63,6 @@ public interface LowLevelHttpExchange extends HttpExchange {

BinaryMultiData files_();

LowLevelHttpExchange send();

}
6 changes: 4 additions & 2 deletions rapidoid-pages/src/main/java/org/rapidoid/pages/Pages.java
Expand Up @@ -39,6 +39,7 @@
import org.rapidoid.html.Tags;
import org.rapidoid.http.HTTPServer;
import org.rapidoid.http.HttpExchange;
import org.rapidoid.http.HttpExchangeInternals;
import org.rapidoid.http.HttpNotFoundException;
import org.rapidoid.http.HttpSuccessException;
import org.rapidoid.json.JSON;
Expand Down Expand Up @@ -168,7 +169,7 @@ public static Object render(HttpExchange x, Object page) {

public static Object dispatch(HttpExchange x, WebPojoDispatcher serviceDispatcher, Map<String, Class<?>> pages) {

if (x.serveStatic()) {
if (x.serveStaticFile()) {
return x;
}

Expand Down Expand Up @@ -347,7 +348,8 @@ private static Object changes(HttpExchange x, String html) {
}

public static byte[] stateOf(HttpExchange x) {
return x.hasSession() ? x.serializeLocals() : null;
HttpExchangeInternals xi = (HttpExchangeInternals) x;
return x.hasSession() ? xi.serializeLocals() : null;
}

public static void callCmdHandler(HttpExchange x, Object target, Cmd cmd) {
Expand Down
Expand Up @@ -32,7 +32,6 @@
import org.rapidoid.annotation.PUT;
import org.rapidoid.annotation.RESTful;
import org.rapidoid.annotation.Since;
import org.rapidoid.annotation.TransactionMode;
import org.rapidoid.aop.AOP;
import org.rapidoid.arr.Arr;
import org.rapidoid.beany.Metadata;
Expand Down Expand Up @@ -158,10 +157,7 @@ private String reqName(Method method, String url) {
}

@Override
protected void preprocess(PojoRequest req, Method method, Object service, Object[] args) {
HttpExchange x = ((WebReq) req).getExchange();
x.setTransactionMode(TransactionMode.READ_ONLY);
}
protected void preprocess(PojoRequest req, Method method, Object service, Object[] args) {}

@Override
protected Object invoke(PojoRequest req, Method method, Object service, Object[] args) {
Expand Down

0 comments on commit 4779efb

Please sign in to comment.