forked from kamax-matrix/mxisd
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to log all requests and responses.
- Loading branch information
Showing
9 changed files
with
450 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/java/io/kamax/mxisd/http/undertow/conduit/ConduitWithDump.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.kamax.mxisd.http.undertow.conduit; | ||
|
||
public interface ConduitWithDump { | ||
String dump(); | ||
} |
107 changes: 107 additions & 0 deletions
107
src/main/java/io/kamax/mxisd/http/undertow/conduit/DebuggingStreamSinkConduit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2014 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package io.kamax.mxisd.http.undertow.conduit; | ||
|
||
import org.xnio.IoUtils; | ||
import org.xnio.channels.StreamSourceChannel; | ||
import org.xnio.conduits.AbstractStreamSinkConduit; | ||
import org.xnio.conduits.ConduitWritableByteChannel; | ||
import org.xnio.conduits.Conduits; | ||
import org.xnio.conduits.StreamSinkConduit; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
/** | ||
* Conduit that saves all the data that is written through it and can dump it to the console | ||
* <p> | ||
* Obviously this should not be used in production. | ||
* | ||
* @author Stuart Douglas | ||
*/ | ||
public class DebuggingStreamSinkConduit extends AbstractStreamSinkConduit<StreamSinkConduit> implements ConduitWithDump { | ||
|
||
private final List<byte[]> data = new CopyOnWriteArrayList<>(); | ||
|
||
/** | ||
* Construct a new instance. | ||
* | ||
* @param next the delegate conduit to set | ||
*/ | ||
public DebuggingStreamSinkConduit(StreamSinkConduit next) { | ||
super(next); | ||
} | ||
|
||
@Override | ||
public int write(ByteBuffer src) throws IOException { | ||
int pos = src.position(); | ||
int res = super.write(src); | ||
if (res > 0) { | ||
byte[] d = new byte[res]; | ||
for (int i = 0; i < res; ++i) { | ||
d[i] = src.get(i + pos); | ||
} | ||
data.add(d); | ||
} | ||
return res; | ||
} | ||
|
||
@Override | ||
public long write(ByteBuffer[] dsts, int offs, int len) throws IOException { | ||
for (int i = offs; i < len; ++i) { | ||
if (dsts[i].hasRemaining()) { | ||
return write(dsts[i]); | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public long transferFrom(final FileChannel src, final long position, final long count) throws IOException { | ||
return src.transferTo(position, count, new ConduitWritableByteChannel(this)); | ||
} | ||
|
||
@Override | ||
public long transferFrom(final StreamSourceChannel source, final long count, final ByteBuffer throughBuffer) throws IOException { | ||
return IoUtils.transfer(source, count, throughBuffer, new ConduitWritableByteChannel(this)); | ||
} | ||
|
||
@Override | ||
public int writeFinal(ByteBuffer src) throws IOException { | ||
return Conduits.writeFinalBasic(this, src); | ||
} | ||
|
||
@Override | ||
public long writeFinal(ByteBuffer[] srcs, int offset, int length) throws IOException { | ||
return Conduits.writeFinalBasic(this, srcs, offset, length); | ||
} | ||
|
||
@Override | ||
public String dump() { | ||
StringBuilder sb = new StringBuilder(); | ||
for (byte[] datum : data) { | ||
sb.append(new String(datum, StandardCharsets.UTF_8)); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
src/main/java/io/kamax/mxisd/http/undertow/conduit/DebuggingStreamSourceConduit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2014 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package io.kamax.mxisd.http.undertow.conduit; | ||
|
||
import org.xnio.IoUtils; | ||
import org.xnio.channels.StreamSinkChannel; | ||
import org.xnio.conduits.AbstractStreamSourceConduit; | ||
import org.xnio.conduits.ConduitReadableByteChannel; | ||
import org.xnio.conduits.StreamSourceConduit; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
/** | ||
* Conduit that saves all the data that is written through it and can dump it to the console | ||
* <p> | ||
* Obviously this should not be used in production. | ||
* | ||
* @author Stuart Douglas | ||
*/ | ||
public class DebuggingStreamSourceConduit extends AbstractStreamSourceConduit<StreamSourceConduit> implements ConduitWithDump { | ||
|
||
private final List<byte[]> data = new CopyOnWriteArrayList<>(); | ||
|
||
/** | ||
* Construct a new instance. | ||
* | ||
* @param next the delegate conduit to set | ||
*/ | ||
public DebuggingStreamSourceConduit(StreamSourceConduit next) { | ||
super(next); | ||
} | ||
|
||
public long transferTo(final long position, final long count, final FileChannel target) throws IOException { | ||
return target.transferFrom(new ConduitReadableByteChannel(this), position, count); | ||
} | ||
|
||
public long transferTo(final long count, final ByteBuffer throughBuffer, final StreamSinkChannel target) throws IOException { | ||
return IoUtils.transfer(new ConduitReadableByteChannel(this), count, throughBuffer, target); | ||
} | ||
|
||
@Override | ||
public int read(ByteBuffer dst) throws IOException { | ||
int pos = dst.position(); | ||
int res = super.read(dst); | ||
if (res > 0) { | ||
byte[] d = new byte[res]; | ||
for (int i = 0; i < res; ++i) { | ||
d[i] = dst.get(i + pos); | ||
} | ||
data.add(d); | ||
} | ||
return res; | ||
} | ||
|
||
@Override | ||
public long read(ByteBuffer[] dsts, int offs, int len) throws IOException { | ||
for (int i = offs; i < len; ++i) { | ||
if (dsts[i].hasRemaining()) { | ||
return read(dsts[i]); | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public String dump() { | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
for (byte[] datum : data) { | ||
sb.append(new String(datum, StandardCharsets.UTF_8)); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/io/kamax/mxisd/http/undertow/conduit/LazyConduitWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.kamax.mxisd.http.undertow.conduit; | ||
|
||
import io.undertow.server.ConduitWrapper; | ||
import io.undertow.server.HttpServerExchange; | ||
import io.undertow.util.ConduitFactory; | ||
import org.xnio.conduits.Conduit; | ||
|
||
public abstract class LazyConduitWrapper<T extends Conduit> implements ConduitWrapper<T> { | ||
|
||
private T conduit = null; | ||
|
||
protected abstract T create(ConduitFactory<T> factory, HttpServerExchange exchange); | ||
|
||
@Override | ||
public T wrap(ConduitFactory<T> factory, HttpServerExchange exchange) { | ||
conduit = create(factory, exchange); | ||
return conduit; | ||
} | ||
|
||
public T get() { | ||
return conduit; | ||
} | ||
} |
Oops, something went wrong.