1 /*
2 * Copyright (C) 2010, Google Inc.
3 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
4 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
5 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
6 *
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Distribution License v. 1.0 which is available at
9 * https://www.eclipse.org/org/documents/edl-v10.php.
10 *
11 * SPDX-License-Identifier: BSD-3-Clause
12 */
13
14 package org.eclipse.jgit.transport;
15
16 import java.io.StringWriter;
17 import java.io.Writer;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Map;
21
22 import org.eclipse.jgit.errors.TransportException;
23 import org.eclipse.jgit.internal.JGitText;
24 import org.eclipse.jgit.lib.Ref;
25
26 /**
27 * Base helper class for implementing operations connections.
28 *
29 * @see BasePackConnection
30 * @see BaseFetchConnection
31 */
32 public abstract class BaseConnection implements Connection {
33 private Map<String, Ref> advertisedRefs = Collections.emptyMap();
34
35 private String peerUserAgent;
36
37 private boolean startedOperation;
38
39 private Writer messageWriter;
40
41 /** {@inheritDoc} */
42 @Override
43 public Map<String, Ref> getRefsMap() {
44 return advertisedRefs;
45 }
46
47 /** {@inheritDoc} */
48 @Override
49 public final Collection<Ref> getRefs() {
50 return advertisedRefs.values();
51 }
52
53 /** {@inheritDoc} */
54 @Override
55 public final Ref getRef(String name) {
56 return advertisedRefs.get(name);
57 }
58
59 /** {@inheritDoc} */
60 @Override
61 public String getMessages() {
62 return messageWriter != null ? messageWriter.toString() : ""; //$NON-NLS-1$
63 }
64
65 /**
66 * {@inheritDoc}
67 *
68 * User agent advertised by the remote server.
69 * @since 4.0
70 */
71 @Override
72 public String getPeerUserAgent() {
73 return peerUserAgent;
74 }
75
76 /**
77 * Remember the remote peer's agent.
78 *
79 * @param agent
80 * remote peer agent string.
81 * @since 4.0
82 */
83 protected void setPeerUserAgent(String agent) {
84 peerUserAgent = agent;
85 }
86
87 /** {@inheritDoc} */
88 @Override
89 public abstract void close();
90
91 /**
92 * Denote the list of refs available on the remote repository.
93 * <p>
94 * Implementors should invoke this method once they have obtained the refs
95 * that are available from the remote repository.
96 *
97 * @param all
98 * the complete list of refs the remote has to offer. This map
99 * will be wrapped in an unmodifiable way to protect it, but it
100 * does not get copied.
101 */
102 protected void available(Map<String, Ref> all) {
103 advertisedRefs = Collections.unmodifiableMap(all);
104 }
105
106 /**
107 * Helper method for ensuring one-operation per connection. Check whether
108 * operation was already marked as started, and mark it as started.
109 *
110 * @throws org.eclipse.jgit.errors.TransportException
111 * if operation was already marked as started.
112 */
113 protected void markStartedOperation() throws TransportException {
114 if (startedOperation)
115 throw new TransportException(
116 JGitText.get().onlyOneOperationCallPerConnectionIsSupported);
117 startedOperation = true;
118 }
119
120 /**
121 * Get the writer that buffers messages from the remote side.
122 *
123 * @return writer to store messages from the remote.
124 */
125 protected Writer getMessageWriter() {
126 if (messageWriter == null)
127 setMessageWriter(new StringWriter());
128 return messageWriter;
129 }
130
131 /**
132 * Set the writer that buffers messages from the remote side.
133 *
134 * @param writer
135 * the writer that messages will be delivered to. The writer's
136 * {@code toString()} method should be overridden to return the
137 * complete contents.
138 */
139 protected void setMessageWriter(Writer writer) {
140 if (messageWriter != null)
141 throw new IllegalStateException(JGitText.get().writerAlreadyInitialized);
142 messageWriter = writer;
143 }
144 }