Skip to content

Several fixes/improvements #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 7, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/ning/http/client/AsyncHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public Request build() {
if (i >= 0) {
url = url.substring(0, i);
}
signatureCalculator.calculateAndAddSignature(baseURL, request, this);
signatureCalculator.calculateAndAddSignature(url, request, this);
}
return super.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public long getContentLength() {
public long read(ByteBuffer buffer) throws IOException {

// To be safe.
chunk = new byte[buffer.capacity() - 10];
chunk = new byte[buffer.remaining() - 10];


int read = -1;
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/ning/http/client/ntlm/NTLMEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class NTLMEngine {
java.security.SecureRandom rnd = null;
try {
rnd = java.security.SecureRandom.getInstance("SHA1PRNG");
} catch (Exception e) {
} catch (Exception ignored) {
}
RND_GEN = rnd;
}
Expand Down Expand Up @@ -753,7 +753,7 @@ String getResponse() throws UnsupportedEncodingException {
} else {
resp = messageContents;
}
return new String(Base64.encode(resp));
return Base64.encode(resp);
}

}
Expand Down Expand Up @@ -996,8 +996,7 @@ String getResponse() throws UnsupportedEncodingException {
int domainOffset = ntRespOffset + ntRespLen;
int userOffset = domainOffset + domainLen;
int hostOffset = userOffset + userLen;
int sessionKeyOffset = hostOffset + hostLen;
int finalLength = sessionKeyOffset + 0;
int finalLength = hostOffset + hostLen;

// Start the response. Length includes signature and type
prepareResponse(finalLength, 3);
Expand Down Expand Up @@ -1127,7 +1126,6 @@ void update(byte[] input) {
int transferAmt = input.length - inputIndex;
System.arraycopy(input, inputIndex, dataBuffer, curBufferPos, transferAmt);
count += transferAmt;
curBufferPos += transferAmt;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ public class OAuthSignatureCalculator
implements SignatureCalculator {
public final static String HEADER_AUTHORIZATION = "Authorization";

private final String KEY_OAUTH_CONSUMER_KEY = "oauth_consumer_key";
private final String KEY_OAUTH_NONCE = "oauth_nonce";
private final String KEY_OAUTH_SIGNATURE = "oauth_signature";
private final String KEY_OAUTH_SIGNATURE_METHOD = "oauth_signature_method";
private final String KEY_OAUTH_TIMESTAMP = "oauth_timestamp";
private final String KEY_OAUTH_TOKEN = "oauth_token";
private final String KEY_OAUTH_VERSION = "oauth_version";
private static final String KEY_OAUTH_CONSUMER_KEY = "oauth_consumer_key";
private static final String KEY_OAUTH_NONCE = "oauth_nonce";
private static final String KEY_OAUTH_SIGNATURE = "oauth_signature";
private static final String KEY_OAUTH_SIGNATURE_METHOD = "oauth_signature_method";
private static final String KEY_OAUTH_TIMESTAMP = "oauth_timestamp";
private static final String KEY_OAUTH_TOKEN = "oauth_token";
private static final String KEY_OAUTH_VERSION = "oauth_version";

private final String OAUTH_VERSION_1_0 = "1.0";
private final String OAUTH_SIGNATURE_METHOD = "HMAC-SHA1";
private static final String OAUTH_VERSION_1_0 = "1.0";
private static final String OAUTH_SIGNATURE_METHOD = "HMAC-SHA1";

/**
* To generate Nonce, need some (pseudo)randomness; no need for
Expand Down Expand Up @@ -84,12 +84,12 @@ public OAuthSignatureCalculator(ConsumerKey consumerAuth, RequestToken userAuth)
//@Override // silly 1.5; doesn't allow this for interfaces

public void calculateAndAddSignature(String baseURL, Request request, RequestBuilderBase<?> requestBuilder) {
String method = request.getMethod().toString(); // POST etc
String method = request.getMethod(); // POST etc
String nonce = generateNonce();
long timestamp = System.currentTimeMillis() / 1000L;
String signature = calculateSignature(method, baseURL, timestamp, nonce, request.getParams(), request.getQueryParams());
String headerValue = constructAuthHeader(signature, nonce, timestamp);
requestBuilder = requestBuilder.setHeader(HEADER_AUTHORIZATION, headerValue);
requestBuilder.setHeader(HEADER_AUTHORIZATION, headerValue);
}

/**
Expand Down Expand Up @@ -260,5 +260,25 @@ public int compareTo(Parameter other) {
public String toString() {
return key + "=" + value;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Parameter parameter = (Parameter) o;

if (!key.equals(parameter.key)) return false;
if (!value.equals(parameter.value)) return false;

return true;
}

@Override
public int hashCode() {
int result = key.hashCode();
result = 31 * result + value.hashCode();
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2011 Sonatype, Inc. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.ning.http.client.providers.grizzly;

import com.ning.http.client.Body;
import com.ning.http.client.BodyGenerator;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.filterchain.FilterChainContext;
import org.glassfish.grizzly.http.HttpContent;
import org.glassfish.grizzly.http.HttpRequestPacket;

/**
* {@link BodyGenerator} which may return just part of the payload at the time
* handler is requesting it. If it happens - PartialBodyGenerator becomes responsible
* for finishing payload transferring asynchronously.
*
* @author The Grizzly Team
* @since 1.7.0
*/
public class FeedableBodyGenerator implements BodyGenerator {
private final Queue<BodyPart> queue = new ConcurrentLinkedQueue<BodyPart>();
private final AtomicInteger queueSize = new AtomicInteger();

private volatile HttpRequestPacket requestPacket;
private volatile FilterChainContext context;

@Override
public Body createBody() throws IOException {
return new EmptyBody();
}

public void feed(final Buffer buffer, final boolean isLast)
throws IOException {
queue.offer(new BodyPart(buffer, isLast));
queueSize.incrementAndGet();

if (context != null) {
flushQueue();
}
}

void initializeAsynchronousTransfer(final FilterChainContext context,
final HttpRequestPacket requestPacket) throws IOException {
this.context = context;
this.requestPacket = requestPacket;
flushQueue();
}

private void flushQueue() throws IOException {
if (queueSize.get() > 0) {
synchronized(this) {
while(queueSize.get() > 0) {
final BodyPart bodyPart = queue.poll();
queueSize.decrementAndGet();
final HttpContent content =
requestPacket.httpContentBuilder()
.content(bodyPart.buffer)
.last(bodyPart.isLast)
.build();
context.write(content, ((!requestPacket.isCommitted()) ?
context.getTransportContext().getCompletionHandler() :
null));

}
}
}
}

private final class EmptyBody implements Body {

@Override
public long getContentLength() {
return -1;
}

@Override
public long read(final ByteBuffer buffer) throws IOException {
return 0;
}

@Override
public void close() throws IOException {
context.completeAndRecycle();
context = null;
requestPacket = null;
}
}

private final static class BodyPart {
private final boolean isLast;
private final Buffer buffer;

public BodyPart(final Buffer buffer, final boolean isLast) {
this.buffer = buffer;
this.isLast = isLast;
}
}
}
Loading