Browse Source
Change-Id: I962461630384b76e7f387f4e1c1248833fbc4673 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>stable-4.0
Matthias Sohn
10 years ago
95 changed files with 1902 additions and 760 deletions
@ -0,0 +1,204 @@
|
||||
/* |
||||
* Copyright (C) 2010, Google Inc. |
||||
* Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com> |
||||
* Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.awtui; |
||||
|
||||
import java.awt.GridBagConstraints; |
||||
import java.awt.GridBagLayout; |
||||
import java.awt.Insets; |
||||
|
||||
import javax.swing.JLabel; |
||||
import javax.swing.JOptionPane; |
||||
import javax.swing.JPanel; |
||||
import javax.swing.JPasswordField; |
||||
import javax.swing.JTextField; |
||||
|
||||
import org.eclipse.jgit.errors.UnsupportedCredentialItem; |
||||
import org.eclipse.jgit.transport.CredentialItem; |
||||
import org.eclipse.jgit.transport.CredentialsProvider; |
||||
import org.eclipse.jgit.transport.URIish; |
||||
|
||||
/** Interacts with the user during authentication by using AWT/Swing dialogs. */ |
||||
public class AwtCredentialsProvider extends CredentialsProvider { |
||||
/** Install this implementation as the default. */ |
||||
public static void install() { |
||||
CredentialsProvider.setDefault(new AwtCredentialsProvider()); |
||||
} |
||||
|
||||
@Override |
||||
public boolean isInteractive() { |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public boolean supports(CredentialItem... items) { |
||||
for (CredentialItem i : items) { |
||||
if (i instanceof CredentialItem.StringType) |
||||
continue; |
||||
|
||||
else if (i instanceof CredentialItem.CharArrayType) |
||||
continue; |
||||
|
||||
else if (i instanceof CredentialItem.YesNoType) |
||||
continue; |
||||
|
||||
else if (i instanceof CredentialItem.InformationalMessage) |
||||
continue; |
||||
|
||||
else |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public boolean get(URIish uri, CredentialItem... items) |
||||
throws UnsupportedCredentialItem { |
||||
if (items.length == 0) { |
||||
return true; |
||||
|
||||
} else if (items.length == 1) { |
||||
final CredentialItem item = items[0]; |
||||
|
||||
if (item instanceof CredentialItem.InformationalMessage) { |
||||
JOptionPane.showMessageDialog(null, item.getPromptText(), |
||||
UIText.get().warning, JOptionPane.INFORMATION_MESSAGE); |
||||
return true; |
||||
|
||||
} else if (item instanceof CredentialItem.YesNoType) { |
||||
CredentialItem.YesNoType v = (CredentialItem.YesNoType) item; |
||||
int r = JOptionPane.showConfirmDialog(null, v.getPromptText(), |
||||
UIText.get().warning, JOptionPane.YES_NO_OPTION); |
||||
switch (r) { |
||||
case JOptionPane.YES_OPTION: |
||||
v.setValue(true); |
||||
return true; |
||||
|
||||
case JOptionPane.NO_OPTION: |
||||
v.setValue(false); |
||||
return true; |
||||
|
||||
case JOptionPane.CANCEL_OPTION: |
||||
case JOptionPane.CLOSED_OPTION: |
||||
default: |
||||
return false; |
||||
} |
||||
|
||||
} else { |
||||
return interactive(uri, items); |
||||
} |
||||
|
||||
} else { |
||||
return interactive(uri, items); |
||||
} |
||||
} |
||||
|
||||
private static boolean interactive(URIish uri, CredentialItem[] items) { |
||||
final GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1, |
||||
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, |
||||
new Insets(0, 0, 0, 0), 0, 0); |
||||
final JPanel panel = new JPanel(); |
||||
panel.setLayout(new GridBagLayout()); |
||||
|
||||
final JTextField[] texts = new JTextField[items.length]; |
||||
for (int i = 0; i < items.length; i++) { |
||||
CredentialItem item = items[i]; |
||||
|
||||
if (item instanceof CredentialItem.StringType |
||||
|| item instanceof CredentialItem.CharArrayType) { |
||||
gbc.fill = GridBagConstraints.NONE; |
||||
gbc.gridwidth = GridBagConstraints.RELATIVE; |
||||
gbc.gridx = 0; |
||||
panel.add(new JLabel(item.getPromptText()), gbc); |
||||
|
||||
gbc.fill = GridBagConstraints.HORIZONTAL; |
||||
gbc.gridwidth = GridBagConstraints.RELATIVE; |
||||
gbc.gridx = 1; |
||||
if (item.isValueSecure()) |
||||
texts[i] = new JPasswordField(20); |
||||
else |
||||
texts[i] = new JTextField(20); |
||||
panel.add(texts[i], gbc); |
||||
gbc.gridy++; |
||||
|
||||
} else if (item instanceof CredentialItem.InformationalMessage) { |
||||
gbc.fill = GridBagConstraints.NONE; |
||||
gbc.gridwidth = GridBagConstraints.REMAINDER; |
||||
gbc.gridx = 0; |
||||
panel.add(new JLabel(item.getPromptText()), gbc); |
||||
gbc.gridy++; |
||||
|
||||
} else { |
||||
throw new UnsupportedCredentialItem(uri, item.getPromptText()); |
||||
} |
||||
} |
||||
|
||||
if (JOptionPane.showConfirmDialog(null, panel, |
||||
UIText.get().authenticationRequired, |
||||
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) != JOptionPane.OK_OPTION) |
||||
return false; // cancel
|
||||
|
||||
for (int i = 0; i < items.length; i++) { |
||||
CredentialItem item = items[i]; |
||||
JTextField f = texts[i]; |
||||
|
||||
if (item instanceof CredentialItem.StringType) { |
||||
CredentialItem.StringType v = (CredentialItem.StringType) item; |
||||
if (f instanceof JPasswordField) |
||||
v.setValue(new String(((JPasswordField) f).getPassword())); |
||||
else |
||||
v.setValue(f.getText()); |
||||
|
||||
} else if (item instanceof CredentialItem.CharArrayType) { |
||||
CredentialItem.CharArrayType v = (CredentialItem.CharArrayType) item; |
||||
if (f instanceof JPasswordField) |
||||
v.setValueNoCopy(((JPasswordField) f).getPassword()); |
||||
else |
||||
v.setValueNoCopy(f.getText().toCharArray()); |
||||
} |
||||
} |
||||
return true; |
||||
} |
||||
} |
@ -0,0 +1,95 @@
|
||||
/* |
||||
* Copyright (C) 2014, André de Oliveira <andre.oliveira@liferay.com> |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
package org.eclipse.jgit.merge; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.OutputStream; |
||||
|
||||
/** |
||||
* An output stream which is aware of newlines and can be asked to begin a new |
||||
* line if not already in one. |
||||
*/ |
||||
class EolAwareOutputStream extends OutputStream { |
||||
private final OutputStream out; |
||||
|
||||
private boolean bol = true; |
||||
|
||||
/** |
||||
* Initialize a new EOL aware stream. |
||||
* |
||||
* @param out |
||||
* stream to output all writes to. |
||||
*/ |
||||
EolAwareOutputStream(OutputStream out) { |
||||
this.out = out; |
||||
} |
||||
|
||||
/** |
||||
* Begin a new line if not already in one. |
||||
* |
||||
* @exception IOException |
||||
* if an I/O error occurs. |
||||
*/ |
||||
void beginln() throws IOException { |
||||
if (!bol) |
||||
write('\n'); |
||||
} |
||||
|
||||
/** @return true if a new line has just begun. */ |
||||
boolean isBeginln() { |
||||
return bol; |
||||
} |
||||
|
||||
@Override |
||||
public void write(int val) throws IOException { |
||||
out.write(val); |
||||
bol = (val == '\n'); |
||||
} |
||||
|
||||
@Override |
||||
public void write(byte[] buf, int pos, int cnt) throws IOException { |
||||
if (cnt > 0) { |
||||
out.write(buf, pos, cnt); |
||||
bol = (buf[pos + (cnt - 1)] == '\n'); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,146 @@
|
||||
/* |
||||
* Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com> |
||||
* Copyright (C) 2014, André de Oliveira <andre.oliveira@liferay.com> |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.merge; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.OutputStream; |
||||
import java.util.List; |
||||
|
||||
import org.eclipse.jgit.diff.RawText; |
||||
import org.eclipse.jgit.merge.MergeChunk.ConflictState; |
||||
|
||||
class MergeFormatterPass { |
||||
|
||||
private final EolAwareOutputStream out; |
||||
|
||||
private final MergeResult<RawText> res; |
||||
|
||||
private final List<String> seqName; |
||||
|
||||
private final String charsetName; |
||||
|
||||
private final boolean threeWayMerge; |
||||
|
||||
private String lastConflictingName; // is set to non-null whenever we are in
|
||||
// a conflict
|
||||
|
||||
MergeFormatterPass(OutputStream out, MergeResult<RawText> res, List<String> seqName, |
||||
String charsetName) { |
||||
this.out = new EolAwareOutputStream(out); |
||||
this.res = res; |
||||
this.seqName = seqName; |
||||
this.charsetName = charsetName; |
||||
this.threeWayMerge = (res.getSequences().size() == 3); |
||||
} |
||||
|
||||
void formatMerge() throws IOException { |
||||
boolean missingNewlineAtEnd = false; |
||||
for (MergeChunk chunk : res) { |
||||
RawText seq = res.getSequences().get(chunk.getSequenceIndex()); |
||||
writeConflictMetadata(chunk); |
||||
// the lines with conflict-metadata are written. Now write the chunk
|
||||
for (int i = chunk.getBegin(); i < chunk.getEnd(); i++) |
||||
writeLine(seq, i); |
||||
missingNewlineAtEnd = seq.isMissingNewlineAtEnd(); |
||||
} |
||||
// one possible leftover: if the merge result ended with a conflict we
|
||||
// have to close the last conflict here
|
||||
if (lastConflictingName != null) |
||||
writeConflictEnd(); |
||||
if (!missingNewlineAtEnd) |
||||
out.beginln(); |
||||
} |
||||
|
||||
private void writeConflictMetadata(MergeChunk chunk) throws IOException { |
||||
if (lastConflictingName != null |
||||
&& chunk.getConflictState() != ConflictState.NEXT_CONFLICTING_RANGE) { |
||||
// found the end of an conflict
|
||||
writeConflictEnd(); |
||||
} |
||||
if (chunk.getConflictState() == ConflictState.FIRST_CONFLICTING_RANGE) { |
||||
// found the start of an conflict
|
||||
writeConflictStart(chunk); |
||||
} else if (chunk.getConflictState() == ConflictState.NEXT_CONFLICTING_RANGE) { |
||||
// found another conflicting chunk
|
||||
writeConflictChange(chunk); |
||||
} |
||||
} |
||||
|
||||
private void writeConflictEnd() throws IOException { |
||||
writeln(">>>>>>> " + lastConflictingName); //$NON-NLS-1$
|
||||
lastConflictingName = null; |
||||
} |
||||
|
||||
private void writeConflictStart(MergeChunk chunk) throws IOException { |
||||
lastConflictingName = seqName.get(chunk.getSequenceIndex()); |
||||
writeln("<<<<<<< " + lastConflictingName); //$NON-NLS-1$
|
||||
} |
||||
|
||||
private void writeConflictChange(MergeChunk chunk) throws IOException { |
||||
/* |
||||
* In case of a non-three-way merge I'll add the name of the conflicting |
||||
* chunk behind the equal signs. I also append the name of the last |
||||
* conflicting chunk after the ending greater-than signs. If somebody |
||||
* knows a better notation to present non-three-way merges - feel free |
||||
* to correct here. |
||||
*/ |
||||
lastConflictingName = seqName.get(chunk.getSequenceIndex()); |
||||
writeln(threeWayMerge ? "=======" : "======= " //$NON-NLS-1$ //$NON-NLS-2$
|
||||
+ lastConflictingName); |
||||
} |
||||
|
||||
private void writeln(String s) throws IOException { |
||||
out.beginln(); |
||||
out.write((s + "\n").getBytes(charsetName)); //$NON-NLS-1$
|
||||
} |
||||
|
||||
private void writeLine(RawText seq, int i) throws IOException { |
||||
out.beginln(); |
||||
seq.writeLine(out, i); |
||||
// still BOL? It was a blank line. But writeLine won't lf, so we do.
|
||||
if (out.isBeginln()) |
||||
out.write('\n'); |
||||
} |
||||
} |
@ -0,0 +1,80 @@
|
||||
/* |
||||
* Copyright (C) 2015, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.transport; |
||||
|
||||
/** |
||||
* Internal API to to assist {@code org.eclipse.jgit.http.server}. |
||||
* <p> |
||||
* <b>Do not call.</b> |
||||
* |
||||
* @since 4.0 |
||||
*/ |
||||
public class InternalHttpServerGlue { |
||||
/** |
||||
* Apply a default user agent for a request. |
||||
* |
||||
* @param up |
||||
* current UploadPack instance. |
||||
* @param agent |
||||
* user agent string from the HTTP headers. |
||||
*/ |
||||
public static void setPeerUserAgent(UploadPack up, String agent) { |
||||
up.userAgent = agent; |
||||
} |
||||
|
||||
/** |
||||
* Apply a default user agent for a request. |
||||
* |
||||
* @param rp |
||||
* current ReceivePack instance. |
||||
* @param agent |
||||
* user agent string from the HTTP headers. |
||||
*/ |
||||
public static void setPeerUserAgent(ReceivePack rp, String agent) { |
||||
rp.userAgent = agent; |
||||
} |
||||
|
||||
private InternalHttpServerGlue() { |
||||
} |
||||
} |
@ -0,0 +1,147 @@
|
||||
/* |
||||
* Copyright (C) 2015, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.transport; |
||||
|
||||
import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT; |
||||
|
||||
import java.util.Set; |
||||
|
||||
import org.eclipse.jgit.util.StringUtils; |
||||
|
||||
/** |
||||
* User agent to be reported by this JGit client and server on the network. |
||||
* <p> |
||||
* On HTTP transports this user agent string is always supplied by the JGit |
||||
* client in the {@code User-Agent} HTTP header. |
||||
* <p> |
||||
* On native transports this user agent string is always sent when JGit is a |
||||
* server. When JGit is a client the user agent string will be supplied to the |
||||
* remote server only if the remote server advertises its own agent identity. |
||||
* |
||||
* @since 4.0 |
||||
*/ |
||||
public class UserAgent { |
||||
private static volatile String userAgent = computeUserAgent(); |
||||
|
||||
private static String computeUserAgent() { |
||||
return clean("JGit/" + computeVersion()); //$NON-NLS-1$
|
||||
} |
||||
|
||||
private static String computeVersion() { |
||||
Package pkg = UserAgent.class.getPackage(); |
||||
if (pkg != null) { |
||||
String ver = pkg.getImplementationVersion(); |
||||
if (!StringUtils.isEmptyOrNull(ver)) { |
||||
return ver; |
||||
} |
||||
} |
||||
return "unknown"; //$NON-NLS-1$
|
||||
} |
||||
|
||||
private static String clean(String s) { |
||||
s = s.trim(); |
||||
StringBuilder b = new StringBuilder(s.length()); |
||||
for (int i = 0; i < s.length(); i++) { |
||||
char c = s.charAt(i); |
||||
if (c <= 32 || c >= 127) { |
||||
if (b.length() > 0 && b.charAt(b.length() - 1) == '.') |
||||
continue; |
||||
c = '.'; |
||||
} |
||||
b.append(c); |
||||
} |
||||
return b.length() > 0 ? b.toString() : null; |
||||
} |
||||
|
||||
/** |
||||
* Get the user agent string advertised by JGit. |
||||
* |
||||
* @return a string similar to {@code "JGit/4.0"}; null if the agent has |
||||
* been cleared and should not be shared with a peer. |
||||
*/ |
||||
public static String get() { |
||||
return userAgent; |
||||
} |
||||
|
||||
/** |
||||
* Change the user agent string advertised by JGit. |
||||
* <p> |
||||
* The new string should start with {@code "JGit/"} (for example |
||||
* {@code "JGit/4.0"}) to advertise the implementation as JGit based. |
||||
* <p> |
||||
* Spaces and other whitespace should be avoided as these will be |
||||
* automatically converted to {@code "."}. |
||||
* <p> |
||||
* User agent strings are restricted to printable ASCII. |
||||
* |
||||
* @param agent |
||||
* new user agent string for this running JGit library. Setting |
||||
* to null or empty string will avoid sending any identification |
||||
* to the peer. |
||||
*/ |
||||
public static void set(String agent) { |
||||
userAgent = StringUtils.isEmptyOrNull(agent) ? null : clean(agent); |
||||
} |
||||
|
||||
static String getAgent(Set<String> options, String transportAgent) { |
||||
if (options == null || options.isEmpty()) { |
||||
return transportAgent; |
||||
} |
||||
for (String o : options) { |
||||
if (o.startsWith(OPTION_AGENT) |
||||
&& o.length() > OPTION_AGENT.length() |
||||
&& o.charAt(OPTION_AGENT.length()) == '=') { |
||||
return o.substring(OPTION_AGENT.length() + 1); |
||||
} |
||||
} |
||||
return transportAgent; |
||||
} |
||||
|
||||
static boolean hasAgent(Set<String> options) { |
||||
return getAgent(options, null) != null; |
||||
} |
||||
|
||||
private UserAgent() { |
||||
} |
||||
} |
Loading…
Reference in new issue