Skip to content

Commit

Permalink
Moved and simplified CometDInput and CometDOutput.
Browse files Browse the repository at this point in the history
Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
  • Loading branch information
sbordet committed Oct 7, 2023
1 parent 54be665 commit 799f417
Show file tree
Hide file tree
Showing 27 changed files with 488 additions and 512 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
exports org.cometd.server.ext;
exports org.cometd.server.filter;
exports org.cometd.server.jmx;
exports org.cometd.server.spi;
exports org.cometd.server.transport;

requires transitive org.cometd.api.server;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright (c) 2008-2022 the original author or authors.
*
* 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 org.cometd.server;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;

/**
* <p>An abstraction over HTTP requests.</p>
* <p>This abstraction allows CometD to be independent
* from Servlet, or server-specific HTTP APIs.</p>
*/
public interface CometDRequest {
/**
* @return the HTTP method
*/
String getMethod();

/**
* @return the HTTP protocol version
*/
String getProtocol();

/**
* @param name the query parameter name
* @return the values of the given query parameter
*/
String[] getParameterValues(String name);

/**
* @return the charset of the request body
*/
String getCharacterEncoding();

/**
* @return the request cookies
*/
List<CometDCookie> getCookies();

/**
* @return the input to read the request body from
*/
Input getInput();

/**
* @param name the attribute name
* @return the value of the attribute with the given name,
* or {@code null} if not such attribute exists
*/
Object getAttribute(String name);

/**
* @param name the attribute name
* @param value the attribute value
*/
void setAttribute(String name, Object value);

/**
* @param name the cookie name
* @param value the cookie value
*/
record CometDCookie(String name, String value) {
}

/**
* <p>The source of the request body.</p>
*/
interface Input {
/**
* <p>Demands to invoke the given callback when request content bytes are available.</p>
*
* @param demandCallback the callback to invoke when request content bytes are available
*/
void demand(Runnable demandCallback);

/**
* <p>Reads request content bytes into a {@link Chunk}.</p>
* <p>The returned {@link Chunk} can be:</p>
* <ul>
* <li>{@code null}, if no request content bytes are available</li>
* <li>a possibly empty {@link Chunk} of request content bytes</li>
* </ul>
*
* @return a {@link Chunk} of request content bytes,
* or {@code null} if no request content bytes are available
* @throws IOException if the read fails
*/
Chunk read() throws IOException;

/**
* <p>Request content bytes with the indication of whether they are the last.</p>
*/
interface Chunk {
/**
* <p>A convenient {@link Chunk} constant indicating end-of-file.</p>
*/
Chunk EOF = new Chunk() {
private static final ByteBuffer EMPTY = ByteBuffer.allocate(0);

@Override
public ByteBuffer byteBuffer() {
return EMPTY;
}

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

@Override
public void release() {
}
};

/**
* @return the {@link ByteBuffer} containing the request content bytes
*/
ByteBuffer byteBuffer();

/**
* @return whether this {@link Chunk} is the last
*/
boolean isLast();

/**
* <p>Releases this {@link Chunk} so its {@link #byteBuffer()}
* can be recycled.</p>
*/
void release();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2008-2022 the original author or authors.
*
* 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 org.cometd.server;

import org.cometd.bayeux.Promise;

/**
* <p>An abstraction over HTTP responses.</p>
* <p>This abstraction allows CometD to be independent
* from Servlet, or server-specific HTTP APIs.</p>
*/
public interface CometDResponse {
/**
* <p>Adds an HTTP header to this response.</p>
*
* @param name the HTTP header name
* @param value the HTTP header value
*/
void addHeader(String name, String value);

/**
* @param contentType the content type of the response body
*/
void setContentType(String contentType);

/**
* @return the sink to write the response body to
*/
Output getOutput();

/**
* <p>The sink of the response body.</p>
*/
interface Output {
/**
* <p>Writes the given response bytes, notifying the
* given promise when the write operation is complete.</p>
*
* @param last whether the response bytes to write are the last
* @param bytes the response bytes to write
* @param promise the promise to notify when the write operation is complete
*/
void write(boolean last, byte[] bytes, Promise<Void> promise);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.cometd.server.spi;
package org.cometd.server;

public class HttpException extends Exception {
private final int code;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.concurrent.atomic.AtomicReference;

import org.cometd.bayeux.server.ServerMessage;
import org.cometd.server.spi.CometDRequest;
import org.cometd.server.CometDRequest;
import org.eclipse.jetty.util.thread.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Loading

0 comments on commit 799f417

Please sign in to comment.